var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import dayjs from 'dayjs';
import { generateUUID } from 'utils';
export var EmptyAppSchema = {
    kind: 'Application',
    version: 'widget/v1',
    metadata: { name: 'subscan widget', description: 'subscan widget' },
    spec: {
        components: [],
    },
};
var LocalStorageManager = /** @class */ (function () {
    function LocalStorageManager() {
    }
    LocalStorageManager.prototype.getWidgetsFromLS = function () {
        try {
            var widgetsFromLS = localStorage.getItem(LocalStorageManager.WidgetsLSKey);
            if (widgetsFromLS) {
                return JSON.parse(widgetsFromLS);
            }
            return [];
        }
        catch (error) {
            console.error('empty widgets in localstorage');
            return [];
        }
    };
    LocalStorageManager.prototype.getAppFromLS = function (id) {
        try {
            var appFromLS = localStorage.getItem("widget:".concat(id));
            if (appFromLS) {
                return JSON.parse(appFromLS);
            }
            return EmptyAppSchema;
        }
        catch (error) {
            return EmptyAppSchema;
        }
    };
    LocalStorageManager.prototype.getModulesFromLS = function () {
        try {
            var modulesFromLS = localStorage.getItem(LocalStorageManager.ModulesLSKey);
            if (modulesFromLS) {
                return JSON.parse(modulesFromLS);
            }
            return [];
        }
        catch (error) {
            return [];
        }
    };
    LocalStorageManager.prototype.newWidgetInLs = function (widget, schema) {
        var widgets = this.getWidgetsFromLS();
        var newWidget = __assign({ id: generateUUID(), created_at: dayjs().unix(), updated_at: dayjs().unix() }, widget);
        widgets.push(newWidget);
        localStorage.setItem(LocalStorageManager.WidgetsLSKey, JSON.stringify(widgets));
        this.saveAppInLS(newWidget.id, schema || EmptyAppSchema);
    };
    LocalStorageManager.prototype.duplicateWidgetInLs = function (id) {
        var widgets = this.getWidgetsFromLS();
        var index = widgets.findIndex(function (widget) { return widget.id === id; });
        var newWidget = {
            id: generateUUID(),
            created_at: dayjs().unix(),
            updated_at: dayjs().unix(),
            name: "".concat(widgets[index].name, " Copy"),
        };
        widgets.push(newWidget);
        localStorage.setItem(LocalStorageManager.WidgetsLSKey, JSON.stringify(widgets));
        this.saveAppInLS(newWidget.id, this.getAppFromLS(id));
    };
    LocalStorageManager.prototype.saveWidgetInLs = function (id, app) {
        var widgets = this.getWidgetsFromLS();
        var index = widgets.findIndex(function (widget) { return widget.id === id; });
        widgets[index].updated_at = dayjs().unix();
        localStorage.setItem(LocalStorageManager.WidgetsLSKey, JSON.stringify(widgets));
        this.saveAppInLS(id, app);
    };
    LocalStorageManager.prototype.updateWidgetNameInLs = function (id, name) {
        var widgets = this.getWidgetsFromLS();
        var index = widgets.findIndex(function (widget) { return widget.id === id; });
        widgets[index].name = name;
        localStorage.setItem(LocalStorageManager.WidgetsLSKey, JSON.stringify(widgets));
    };
    LocalStorageManager.prototype.deleteWidget = function (id) {
        var widgets = this.getWidgetsFromLS();
        var rest = widgets.filter(function (widget) { return id !== widget.id; });
        localStorage.setItem(LocalStorageManager.WidgetsLSKey, JSON.stringify(rest));
        localStorage.removeItem(id);
    };
    LocalStorageManager.prototype.saveAppInLS = function (id, app) {
        localStorage.setItem("widget:".concat(id), JSON.stringify(app));
    };
    LocalStorageManager.prototype.saveModulesInLS = function (modules) {
        localStorage.setItem(LocalStorageManager.ModulesLSKey, JSON.stringify(modules));
    };
    LocalStorageManager.WidgetsLSKey = 'subscan-widget-schema';
    LocalStorageManager.AppLSKey = 'subscan-widget-schema';
    LocalStorageManager.ModulesLSKey = 'subscan-widget-modules';
    return LocalStorageManager;
}());
export { LocalStorageManager };
