"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearCache = exports.getCached = exports.cache = exports.MAX_CACHED_NUM = exports.BUILDER_DSL = void 0;
const index_1 = require("@/utils/index");
exports.BUILDER_DSL = 'BUILDER.DSL';
exports.MAX_CACHED_NUM = 15;
/**
 * cache content
 * @param content page content;
 */
const cache = (content) => {
    const key = content.contentId;
    const caches = (0, exports.getCached)(key);
    // remove first
    if (caches.length > exports.MAX_CACHED_NUM) {
        caches.shift();
    }
    caches.push(content);
    const cached = { id: key, contents: caches };
    // console.log('[ CACHED ]', cached);
    localStorage.setItem(exports.BUILDER_DSL, (0, index_1.encrypt)(JSON.stringify(cached)));
    return caches.length;
};
exports.cache = cache;
/**
 * get cached by key(content id)
 * @returns contents
 */
const getCached = (key) => {
    try {
        const cachedStr = localStorage.getItem(exports.BUILDER_DSL);
        const cached = cachedStr ? JSON.parse((0, index_1.decrypt)(cachedStr)) : {};
        // console.log('[ GET CACHED ]', key, cached);
        if (cached.id === key) {
            return cached.contents;
        }
        return [];
    }
    catch (error) {
        console.error('[ GET CACHED FAILED ]', error);
        return [];
    }
};
exports.getCached = getCached;
/**
 * clear cache by key
 */
const clearCache = () => {
    // console.log('[ CLEAR CACHE ]');
    localStorage.setItem(exports.BUILDER_DSL, (0, index_1.encrypt)(JSON.stringify({})));
};
exports.clearCache = clearCache;
