import { Storage as YStorage } from "@matdata/yasgui-utils";
export var storageNamespace = "triply";
function getDefaults() {
    return {
        endpointHistory: [],
        tabs: [],
        active: undefined,
        tabConfig: {},
        lastClosedTab: undefined,
        prefixes: "",
        autoCaptureEnabled: true,
        customEndpointButtons: [],
        endpointConfigs: [],
        workspaces: [],
        activeWorkspaceId: undefined,
    };
}
export default class PersistentConfig {
    constructor(yasgui) {
        this.yasgui = yasgui;
        this.storageId = this.yasgui.getStorageId(this.yasgui.config.persistenceLabelConfig);
        this.storage = new YStorage(storageNamespace);
        this.fromStorage();
        this.registerListeners();
    }
    setActive(id) {
        this.persistedJson.active = id;
        this.toStorage();
    }
    getActiveId() {
        return this.persistedJson.active;
    }
    addToTabList(tabId, index) {
        if (index !== undefined && this.persistedJson.tabs.length > index) {
            this.persistedJson.tabs.splice(index, 0, tabId);
        }
        else {
            this.persistedJson.tabs.push(tabId);
        }
        this.toStorage();
    }
    setTabOrder(tabs) {
        this.persistedJson.tabs = tabs;
        this.toStorage();
    }
    getEndpointHistory() {
        return this.persistedJson.endpointHistory;
    }
    retrieveLastClosedTab() {
        const tabCopy = this.persistedJson.lastClosedTab;
        if (tabCopy === undefined)
            return tabCopy;
        this.persistedJson.lastClosedTab = undefined;
        return tabCopy;
    }
    hasLastClosedTab() {
        return !!this.persistedJson.lastClosedTab;
    }
    deleteTab(tabId) {
        const i = this.persistedJson.tabs.indexOf(tabId);
        if (i > -1) {
            this.persistedJson.tabs.splice(i, 1);
        }
        if (this.tabIsActive(tabId)) {
            this.persistedJson.active = undefined;
        }
        this.persistedJson.lastClosedTab = { index: i, tab: this.persistedJson.tabConfig[tabId] };
        delete this.persistedJson.tabConfig[tabId];
        this.toStorage();
    }
    registerListeners() {
        this.yasgui.on("tabChange", (_yasgui, tab) => {
            this.persistedJson.tabConfig[tab.getId()] = tab.getPersistedJson();
            this.toStorage();
        });
        this.yasgui.on("endpointHistoryChange", (_yasgui, history) => {
            this.persistedJson.endpointHistory = history;
            this.toStorage();
        });
    }
    toStorage() {
        this.storage.set(this.storageId, this.persistedJson, this.yasgui.config.persistencyExpire, this.handleLocalStorageQuotaFull);
    }
    fromStorage() {
        this.persistedJson = this.storage.get(this.storageId) || getDefaults();
        if (!this.persistedJson.endpointHistory) {
            this.persistedJson.endpointHistory = [];
        }
        return this.persistedJson;
    }
    handleLocalStorageQuotaFull(_e) {
        console.warn("Localstorage quota exceeded. Clearing all YASGUI configurations");
        PersistentConfig.clear();
    }
    getTabs() {
        return this.persistedJson.tabs;
    }
    getTab(tabId) {
        return this.persistedJson.tabConfig[tabId];
    }
    setTab(tabId, tabConfig) {
        this.persistedJson.tabs.push(tabId);
        this.persistedJson.tabConfig[tabId] = tabConfig;
        this.persistedJson.active = tabId;
    }
    tabIsActive(tabId) {
        return tabId === this.persistedJson.active;
    }
    currentId() {
        return this.persistedJson.active;
    }
    getPrefixes() {
        return this.persistedJson.prefixes || "";
    }
    setPrefixes(prefixes) {
        this.persistedJson.prefixes = prefixes;
        this.toStorage();
    }
    getAutoCaptureEnabled() {
        return this.persistedJson.autoCaptureEnabled !== false;
    }
    setAutoCaptureEnabled(enabled) {
        this.persistedJson.autoCaptureEnabled = enabled;
        this.toStorage();
    }
    getCustomEndpointButtons() {
        return this.persistedJson.customEndpointButtons || [];
    }
    setCustomEndpointButtons(buttons) {
        this.persistedJson.customEndpointButtons = buttons;
        this.toStorage();
    }
    getShowSnippetsBar() {
        return this.persistedJson.showSnippetsBar;
    }
    setShowSnippetsBar(show) {
        this.persistedJson.showSnippetsBar = show;
        this.toStorage();
    }
    getEndpointConfigs() {
        return this.persistedJson.endpointConfigs || [];
    }
    setEndpointConfigs(configs) {
        this.persistedJson.endpointConfigs = configs;
        this.toStorage();
    }
    addOrUpdateEndpoint(endpoint, updates) {
        const configs = this.getEndpointConfigs();
        const existingIndex = configs.findIndex((c) => c.endpoint === endpoint);
        if (existingIndex >= 0) {
            const merged = Object.assign(Object.assign({}, configs[existingIndex]), updates);
            if ("authentication" in updates && updates.authentication === undefined) {
                delete merged.authentication;
            }
            configs[existingIndex] = merged;
        }
        else {
            configs.push(Object.assign({ endpoint }, updates));
        }
        this.setEndpointConfigs(configs);
    }
    getEndpointConfig(endpoint) {
        const configs = this.getEndpointConfigs();
        return configs.find((c) => c.endpoint === endpoint);
    }
    deleteEndpointConfig(endpoint) {
        const configs = this.getEndpointConfigs();
        const filtered = configs.filter((c) => c.endpoint !== endpoint);
        this.setEndpointConfigs(filtered);
    }
    getWorkspaces() {
        return this.persistedJson.workspaces || [];
    }
    getWorkspace(workspaceId) {
        return this.getWorkspaces().find((w) => w.id === workspaceId);
    }
    setWorkspaces(workspaces) {
        this.persistedJson.workspaces = workspaces;
        this.toStorage();
    }
    addOrUpdateWorkspace(workspace) {
        const workspaces = this.getWorkspaces();
        const existingIndex = workspaces.findIndex((w) => w.id === workspace.id);
        const now = new Date().toISOString();
        if (existingIndex >= 0) {
            const existing = workspaces[existingIndex];
            workspaces[existingIndex] = Object.assign(Object.assign(Object.assign({}, existing), workspace), { createdAt: existing.createdAt || workspace.createdAt || now, updatedAt: now });
        }
        else {
            workspaces.push(Object.assign(Object.assign({}, workspace), { createdAt: workspace.createdAt || now, updatedAt: now }));
        }
        this.setWorkspaces(workspaces);
    }
    deleteWorkspace(workspaceId) {
        const workspaces = this.getWorkspaces();
        const filtered = workspaces.filter((w) => w.id !== workspaceId);
        this.setWorkspaces(filtered);
        if (this.getActiveWorkspaceId() === workspaceId) {
            this.setActiveWorkspaceId(undefined);
        }
    }
    getActiveWorkspaceId() {
        return this.persistedJson.activeWorkspaceId;
    }
    setActiveWorkspaceId(workspaceId) {
        this.persistedJson.activeWorkspaceId = workspaceId;
        this.toStorage();
    }
    static clear() {
        const storage = new YStorage(storageNamespace);
        storage.removeNamespace();
    }
    getPersistedConfig() {
        return this.persistedJson;
    }
    updatePersistedConfig(config) {
        Object.assign(this.persistedJson, config);
        this.toStorage();
    }
    getDisabledDevButtons() {
        return this.persistedJson.disabledDevButtons || [];
    }
    setDisabledDevButtons(endpoints) {
        this.persistedJson.disabledDevButtons = endpoints;
        this.toStorage();
    }
    toggleDevButton(endpoint, enabled) {
        const disabled = this.getDisabledDevButtons();
        if (enabled) {
            const filtered = disabled.filter((e) => e !== endpoint);
            this.setDisabledDevButtons(filtered);
        }
        else {
            if (!disabled.includes(endpoint)) {
                disabled.push(endpoint);
                this.setDisabledDevButtons(disabled);
            }
        }
    }
}
//# sourceMappingURL=PersistentConfig.js.map