"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.retrieveSelectionSetFromLocalStorage = exports.persistSelectionSetToLocalStorage = exports.clearSessionStore = void 0;
var tslib_1 = require("tslib");
var string_1 = require("../utils/string");
/**
 * session-store.ts
 *
 * A thin-wrapper layer over local storage for storing any data related to viewer sessions
 */
/**
 * @since 0.12
 */
function clearSessionStore() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var key;
        return tslib_1.__generator(this, function (_a) {
            try {
                for (key in window.localStorage) {
                    if ((0, string_1.strStartsWith)(key, "selection_")) {
                        window.localStorage.removeItem(key);
                    }
                }
            }
            catch (e) {
            }
            return [2 /*return*/];
        });
    });
}
exports.clearSessionStore = clearSessionStore;
function encodeKey(sessionId, mapName) {
    return "selection_".concat(sessionId, "_").concat(mapName);
}
function persistSelectionSetToLocalStorage(sessionId, mapName, resp) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var key, value;
        return tslib_1.__generator(this, function (_a) {
            key = encodeKey(sessionId, mapName);
            value = JSON.stringify(resp);
            try {
                window.localStorage.setItem(key, value);
            }
            catch (e) {
            }
            return [2 /*return*/];
        });
    });
}
exports.persistSelectionSetToLocalStorage = persistSelectionSetToLocalStorage;
function retrieveSelectionSetFromLocalStorage(sessionId, mapName) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var key, _a, content;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = encodeKey;
                    return [4 /*yield*/, sessionId.getValueAsync()];
                case 1:
                    key = _a.apply(void 0, [_b.sent(), mapName]);
                    content = window.localStorage.getItem(key);
                    if (content) {
                        return [2 /*return*/, JSON.parse(content)];
                    }
                    return [2 /*return*/];
            }
        });
    });
}
exports.retrieveSelectionSetFromLocalStorage = retrieveSelectionSetFromLocalStorage;
//# sourceMappingURL=session-store.js.map