// storage.js

class StorageService {
    constructor(storage) {
        this.storage = storage;
    }

    setItem(key, value, expire = null) {
        const obj = {
            data: value,
        };
        if (expire) {
            const expireTime = new Date().getTime() + expire * 1000;
            obj.expire = expireTime;
        }
        this.storage.setItem(key, JSON.stringify(obj));
    }

    getItem(key) {
        const itemStr = this.storage.getItem(key);
        if (!itemStr) {
            return null;
        }
        const item = JSON.parse(itemStr);
        if (item.expire && new Date().getTime() > item.expire) {
            this.storage.removeItem(key);
            return null;
        }
        return item.data;
    }

    removeItem(key) {
        this.storage.removeItem(key);
    }

    clear() {
        this.storage.clear();
    }
}

const localStorageService = new StorageService(window.localStorage);

export default localStorageService;
