"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThemeApiImpl = void 0;
const tslib_1 = require("tslib");
const ThemeRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/ThemeRedux"));
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ApiBase_1 = require("./ApiBase");
const themes_1 = require("../../themes");
const ThemeInternalApi_1 = require("../Internal/ThemeInternalApi");
const AdaptableHelper_1 = tslib_1.__importDefault(require("../../Utilities/Helpers/AdaptableHelper"));
class ThemeApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new ThemeInternalApi_1.ThemeInternalApi(_adaptable);
    }
    getThemeState() {
        return this.getAdaptableState().Theme;
    }
    loadTheme(theme) {
        this.dispatchAction(ThemeRedux.ThemeSelect(theme));
    }
    loadLightTheme() {
        this.loadTheme('light');
    }
    loadDarkTheme() {
        this.loadTheme('dark');
    }
    getCurrentTheme() {
        return this.getAdaptableState().Theme.CurrentTheme;
    }
    getCurrentThemeObject() {
        const currentTheme = this.getCurrentTheme();
        return this.getThemes().find((themeObj) => themeObj.Name === currentTheme);
    }
    setSystemThemes(systemThemes) {
        this.dispatchAction(ThemeRedux.ThemeSetSystemThemes(systemThemes));
    }
    setUserThemes(userThemes) {
        this.dispatchAction(ThemeRedux.ThemeSetUserThemes(userThemes));
    }
    getSystemThemes() {
        var _a;
        const themes = (_a = this.getAdaptableState().Theme.SystemThemes) !== null && _a !== void 0 ? _a : [];
        return themes.map((theme) => {
            var _a, _b;
            if (typeof theme === 'string') {
                const description = (_b = (_a = themes_1.StaticThemes.find((staticTheme) => staticTheme.Name === theme)) === null || _a === void 0 ? void 0 : _a.Description) !== null && _b !== void 0 ? _b : theme;
                return {
                    Name: theme,
                    Description: description,
                };
            }
            return theme;
        });
    }
    applyCurrentTheme() {
        const currentTheme = this.getThemes().filter((theme) => typeof theme === 'string'
            ? theme === this.getCurrentTheme()
            : theme.Name === this.getCurrentTheme())[0];
        if (!currentTheme) {
            return;
        }
        this._adaptable.applyAdaptableTheme(currentTheme);
        let themeChangedInfo = Object.assign(Object.assign({}, this.getAdaptableInternalApi().buildBaseContext()), { trigger: 'ThemeSelected', theme: currentTheme });
        this.getEventApi().emit('ThemeChanged', themeChangedInfo);
    }
    getUserThemes() {
        var _a;
        return (_a = this.getAdaptableState().Theme.UserThemes) !== null && _a !== void 0 ? _a : [];
    }
    getThemes() {
        return [...this.getSystemThemes(), ...this.getUserThemes()];
    }
    getThemeByName(themeName) {
        return this.getThemes().find((theme) => theme.Name === themeName);
    }
    openThemeSettingsPanel() {
        this.showModulePopup(ModuleConstants.ThemeModuleId);
    }
    getAgGridCurrentThemeName() {
        return this._adaptable.getAgGridCurrentThemeName();
    }
    editTheme(theme) {
        this.dispatchAction(ThemeRedux.ThemeEdit(theme));
        let themeChangedInfo = Object.assign(Object.assign({}, this.getAdaptableInternalApi().buildBaseContext()), { trigger: 'ThemeEdited', theme: theme });
        this.getEventApi().emit('ThemeChanged', themeChangedInfo);
        const currentTheme = this.getCurrentTheme();
        if (currentTheme === theme.Name) {
            this._adaptable.applyAdaptableTheme(theme);
        }
    }
    addUserTheme(theme) {
        AdaptableHelper_1.default.addUuidAndSource(theme);
        this.dispatchAction(ThemeRedux.ThemeAdd(theme));
    }
    deleteUserTheme(theme) {
        this.dispatchAction(ThemeRedux.ThemeDelete(theme));
    }
}
exports.ThemeApiImpl = ThemeApiImpl;
