var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { createInstance } from 'localforage';
export class LocalstorageService {
    constructor(app, storageConfigs) {
        this.app = app;
        this.localforage = createInstance(!!storageConfigs ? storageConfigs : {
            name: 'SHEETBASE_STORAGE',
        });
    }
    instance(storageConfigs) {
        return new LocalstorageService(this.app, storageConfigs);
    }
    set(key, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.localforage.setItem(key, data);
        });
    }
    get(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.localforage.getItem(key);
        });
    }
    iterate(handler) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.localforage.iterate(handler);
        });
    }
    iterateKeys(handler) {
        return __awaiter(this, void 0, void 0, function* () {
            const keys = yield this.keys();
            for (let i = 0; i < keys.length; i++) {
                yield handler(keys[i], i);
            }
        });
    }
    remove(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.localforage.removeItem(key);
        });
    }
    removeBulk(keys) {
        return __awaiter(this, void 0, void 0, function* () {
            for (let i = 0; i < keys.length; i++) {
                this.remove(keys[i]);
            }
        });
    }
    removeByPrefix(prefix) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.iterateKeys((key) => __awaiter(this, void 0, void 0, function* () {
                if (key.substr(0, prefix.length) === prefix) {
                    yield this.remove(key);
                }
            }));
        });
    }
    removeBySuffix(suffix) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.iterateKeys((key) => __awaiter(this, void 0, void 0, function* () {
                if (key.substr(-suffix.length) === suffix) {
                    yield this.remove(key);
                }
            }));
        });
    }
    clear() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.localforage.clear();
        });
    }
    keys() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.localforage.keys();
        });
    }
}
