/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
import { subscribeDOM } from '@kano/common/index.js';
import { TelemetryClient } from '@kano/telemetry/index.js';
import { Plugin } from '../plugin.js';
import { ToolbarEntryPosition } from '../../../elements/kc-workspace-toolbar/entry.js';
/**
 * Manages the actions in the toolbar avilable in the workspace
 */
export class WorkspaceToolbar extends Plugin {
    constructor() {
        super();
        this.subscriptions = [];
        this.defaultEntries = new Map();
        this._telemetry = new TelemetryClient({ scope: 'workspace_toolbar' });
    }
    /**
     * @param {Editor} editor The editor this plugin is attached to
     */
    onInstall(editor) {
        /** @type {Editor} */
        this.editor = editor;
        this.editor.telemetry.mount(this._telemetry);
    }
    onInject() {
        if (!this.editor || !this.editor.workspaceView) {
            return;
        }
        const editorShadowRoot = this.editor.root;
        if (editorShadowRoot) {
            this.toolbar = editorShadowRoot.querySelector('kc-workspace-toolbar');
        }
        if (!this.toolbar) {
            return;
        }
        this.editor.output.onDidRunningStateChange(() => this.updateRunningState(), this, this.subscriptions);
        this.editor.output.onDidFullscreenChange(() => this.updateFullscreen(), this, this.subscriptions);
        let entry = this.addSettingsEntry({
            title: 'Reset Workspace',
            ironIcon: 'kc-ui:reset',
        });
        entry.onDidActivate(() => this.reset());
        this.defaultEntries.set('reset', entry);
        entry = this.addSettingsEntry({
            title: 'Export',
            ironIcon: 'kc-ui:export',
        });
        entry.onDidActivate(() => this.export());
        this.defaultEntries.set('export', entry);
        entry = this.addSettingsEntry({
            title: 'Import',
            ironIcon: 'kc-ui:import',
        });
        entry.onDidActivate(() => this.import());
        this.defaultEntries.set('import', entry);
        entry = this.addEntry({
            id: 'restart',
            position: ToolbarEntryPosition.RIGHT,
            title: 'Restart',
            ironIcon: 'kc-ui:reset',
        });
        entry.onDidActivate(() => this.restart());
        this.defaultEntries.set('restart', entry);
        entry = this.addEntry({
            id: 'fullscreen',
            position: ToolbarEntryPosition.RIGHT,
            title: 'Fullscreen',
            ironIcon: 'kc-ui:maximize',
        });
        entry.onDidActivate(() => this.toggleFullscreen());
        this.defaultEntries.set('fullscreen', entry);
        this.subscriptions.push(subscribeDOM(this.toolbar, 'run-clicked', this.toggleRun.bind(this)));
    }
    disable(id) {
        const entry = this.defaultEntries.get(id);
        if (!entry) {
            return;
        }
        entry.dispose();
    }
    updateRunningState() {
        if (!this.toolbar || !this.editor) {
            return;
        }
        this.toolbar.running = this.editor.output.getRunningState();
    }
    updateFullscreen() {
        if (!this.toolbar || !this.editor) {
            return;
        }
        this.toolbar.fullscreen = this.editor.output.getFullscreen();
        const fullscreenEntry = this.defaultEntries.get('fullscreen');
        if (!fullscreenEntry) {
            return;
        }
        fullscreenEntry.updateIronIcon(`kc-ui:${this.toolbar.fullscreen ? 'minimize' : 'maximize'}`);
    }
    restart() {
        if (!this.editor) {
            return;
        }
        this.editor.output.restart();
        this._telemetry.trackEvent({ name: 'restart_clicked' });
    }
    toggleRun() {
        if (!this.editor) {
            return;
        }
        this.editor.output.toggleRunningState();
        this._telemetry.trackEvent({ name: 'run_clicked' });
    }
    toggleFullscreen() {
        if (!this.editor) {
            return;
        }
        this.editor.output.toggleFullscreen();
        this._telemetry.trackEvent({ name: 'fullscreen_clicked', properties: { value: this.editor.output.getFullscreen() } });
    }
    reset() {
        if (!this.editor) {
            return;
        }
        if (!this.resetDialog) {
            this.resetDialog = this.editor.dialogs.registerConfirm({
                heading: 'Reset',
                text: 'You\'ll lose any unsaved changes',
            });
            this.resetDialog.onDidConfirm(() => {
                if (!this.editor) {
                    return;
                }
                this.editor.reset();
            });
        }
        this.resetDialog.open();
        this._telemetry.trackEvent({ name: 'reset_clicked' });
    }
    export(fileName) {
        if (!this.editor) {
            return;
        }
        this.editor.exportToDisk(fileName);
        this._telemetry.trackEvent({ name: 'export_clicked' });
    }
    import() {
        if (!this.editor) {
            return;
        }
        this.editor.importFromDisk();
        this._telemetry.trackEvent({ name: 'import_clicked' });
    }
    addEntry(...args) {
        if (!this.toolbar) {
            return WorkspaceToolbar.createDisposableNoop();
        }
        return this.toolbar.addEntry(...args);
    }
    addSettingsEntry(...args) {
        if (!this.toolbar) {
            return WorkspaceToolbar.createDisposableNoop();
        }
        return this.toolbar.addSettingsEntry(...args);
    }
    static createDisposableNoop() {
        return {
            onDidActivate() { },
            updateTitle() { },
            updateIcon() { },
            updateIronIcon() { },
            dispose() { },
        };
    }
    onDispose() {
        this.subscriptions.forEach(d => d.dispose());
        this.subscriptions.length = 0;
    }
}
export default WorkspaceToolbar;
//# sourceMappingURL=toolbar.js.map