import { DARK_THEME, normalizeVSCodeTheme } from '../theme/theme-utils';
import { AbstractStore, EntitySetting, ioc, Themes, ThemeStore } from '@journeyapps-labs/reactor-mod';
import * as _ from 'lodash';
import * as monaco from 'monaco-editor';
import { StoredThemesSettings } from '../settings/StoredThemesSettings';
import * as uuid from 'uuid';
import { EditorEntities } from '../entities/EditorEntities';
import { MonacoSystemThemeStore } from './MonacoSystemThemeStore';
export class EditorThemeControl extends EntitySetting {
    constructor(options, store) {
        super(options);
        this.store = store;
    }
    async reset() {
        // this ensures that resets cause the editor theme to follow the selected IDE theme
        const selectedTheme = await ioc.get(ThemeStore).selectedTheme.waitForReady();
        this.setItem(await this.store.getMonacoThemeForReactorTheme(selectedTheme.entity.key));
    }
}
export class MonacoThemeStore extends AbstractStore {
    constructor(systemThemeStore) {
        super({
            name: 'MONACO_THEME_STORE'
        });
        this.systemThemeStore = systemThemeStore || new MonacoSystemThemeStore();
        const systemThemes = this.systemThemeStore.getSystemThemes();
        const defaultTheme = systemThemes[Themes.REACTOR];
        this.selectedTheme = this.addControl(new EditorThemeControl({
            type: EditorEntities.THEME,
            defaultEntity: defaultTheme,
            category: 'User',
            key: 'selected-editor-theme',
            name: 'Selected code theme',
            description: 'This is the syntax-color theme in the IDE',
            changed: (item) => {
                if (!!item) {
                    monaco.editor.defineTheme(DARK_THEME, item.theme);
                    monaco.editor.setTheme(DARK_THEME);
                }
            }
        }, this));
        this.storedThemes = this.addControl(new StoredThemesSettings());
        monaco.editor.defineTheme(DARK_THEME, defaultTheme.theme);
        monaco.editor.setTheme(DARK_THEME);
    }
    clone() {
        const editorTheme = {
            theme: this.selectedTheme.entity.theme,
            key: uuid.v4(),
            label: `${this.selectedTheme.entity.label} copy`,
            system: false,
            compatibility: this.selectedTheme.entity.compatibility
        };
        this.storedThemes.themes.push(editorTheme);
        this.storedThemes.save();
        this.selectedTheme.setItem(editorTheme);
        return editorTheme;
    }
    patchTheme(editorTheme) {
        const index = _.findIndex(this.storedThemes.themes, { key: editorTheme.key });
        this.storedThemes.themes[index] = editorTheme;
        this.storedThemes.save();
        this.selectedTheme.setItem(editorTheme);
        this.selectedTheme.save();
        monaco.editor.defineTheme(DARK_THEME, editorTheme.theme);
        monaco.editor.setTheme(DARK_THEME);
    }
    deleteTheme(editorTheme) {
        if (this.selectedTheme.entity.key === editorTheme.key) {
            this.selectedTheme.setItem(_.values(this.getSystemThemes())[0]);
        }
        // delete theme
        const index = _.findIndex(this.storedThemes.themes, { key: editorTheme.key });
        this.storedThemes.themes.splice(index, 1);
        this.storedThemes.save();
    }
    addTheme(theme) {
        const editorTheme = {
            theme: normalizeVSCodeTheme(theme),
            key: uuid.v4(),
            label: theme.name,
            system: false,
            compatibility: false
        };
        this.storedThemes.themes.push(editorTheme);
        this.storedThemes.save();
        this.selectedTheme.setItem(editorTheme);
    }
    getMonacoThemeForReactorTheme(theme) {
        return this.systemThemeStore.getMonacoThemeForReactorTheme(theme);
    }
    getSystemThemes() {
        return this.systemThemeStore.getSystemThemes();
    }
    async getTheme(key) {
        await this.storedThemes.waitForReady();
        return this.getThemes()[key] || null;
    }
    getThemes() {
        return Object.assign(Object.assign({}, this.getSystemThemes()), _.mapKeys(this.storedThemes.themes, (t) => t.key));
    }
}
//# sourceMappingURL=MonacoThemeStore.js.map