/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
import { BlocklySourceEditor } from '../source-editor/blockly.js';
import { transformLegacyApp } from '../legacy/loader.js';
import Toolbox from '../editor/toolbox.js';
import { DefaultEditorProfile } from '../editor/profile.js';
import { EditorPartsManager } from '../parts/editor.js';
/**
 * Fake editor to load and render the javascript code from legacy blocks
 */
class FakeEditor {
    constructor(output) {
        this.output = output;
        // Create a Blockly source editor to transform the blocks
        this.sourceEditor = new BlocklySourceEditor(this);
        this.profile = new DefaultEditorProfile();
        this.profile.onInstall(this);
        this.parts = new EditorPartsManager(this);
        this.profile.parts.forEach(p => this.parts.registerAPI(p));
        // The editor has to be injected for the blockly instance to work.
        // We do not support headless editors.
        this.sourceEditor.domNode.style.display = 'none';
        document.body.appendChild(this.sourceEditor.domNode);
        this.toolbox = new Toolbox();
        this.toolbox.onInstall(this);
        this.toolbox.setEntries(this.profile.toolbox || []);
        this.toolbox.onInject();
    }
    load(data) {
        return new Promise((resolve) => {
            const safe = transformLegacyApp(data, this.output);
            this.parts.onImport(safe);
            this.sourceEditor.onDidCodeChange((code) => {
                safe.code = code;
                resolve(safe);
            });
            this.sourceEditor.setSource(safe.source);
        });
    }
    get injected() {
        return true;
    }
    get config() {
        return {};
    }
    get workspaceView() {
        return {
            partsControls: {
                addEntry() {
                    return {
                        onDidChangeName() { }
                    };
                },
            }
        };
    }
    onDidInject() { }
    exposeMethod() { }
    dispose() {
        this.sourceEditor.domNode.remove();
        this.toolbox.onDispose();
    }
}
export function transformLegacyCreation(data, output) {
    if (!data.code.snapshot) {
        return Promise.resolve(data);
    }
    const editor = new FakeEditor(output);
    return editor.load(data)
        .then((data) => {
        editor.dispose();
        return data;
    });
}
//# sourceMappingURL=legacy.js.map