import { FeatureBlend } from "../../FeatureBlend.js";
import { MacroEditorBase } from "../Editor/MacroEditorBase.js";
import { AssemblerView } from "./AssemblerView.js";
export default class MacroView extends AssemblerView {
    constructor() {
        super(...arguments);
        this.m_editorViews = [];
    }
    get editorModels() {
        if (this.m_editorModels == null) {
            this.m_editorModels = this.source.getEditors();
        }
        return this.m_editorModels;
    }
    get editorViews() {
        return this.controls.getByType(MacroEditorBase).toArray();
    }
    template() {
        return `<div class="-fb-layout-group -fb-layout-group-macro"></div>`;
    }
    onActivate(e) {
        super.onActivate(e);
        this.createEditors();
        Jade.Application
            .instance
            .contexts
            .current
            .events
            .subscribe("Window:Resized", () => {
            this.triggerEditorViewsResize();
        });
    }
    onInitialView() {
        this.addEditorsToControlCollection();
        this.triggerEditorViewsResize(50);
    }
    onFocusedView() {
        this.editorViews.forEach((view, index) => {
            view.focus();
        });
        this.triggerEditorViewsResize(50);
    }
    onBlurView() {
        this.editorViews.forEach((view, index) => {
            view.blur();
        });
    }
    addEditorsToControlCollection() {
        this.m_editorViews.forEach(view => {
            this.controls.add(view);
        });
    }
    createEditors() {
        this.editorModels.forEach((editor) => {
            this.createEditor(editor, FeatureBlend.getEditorType(editor.script));
        });
    }
    createEditor(model, type) {
        if (type == null) {
            return;
        }
        let editor = new type({
            source: model,
            fill: this.editorModels.length === 1
        });
        if (editor == null) {
            return;
        }
        this.m_editorViews.push(editor);
    }
    triggerEditorViewsResize(delay = 0) {
        setTimeout(() => {
            this.editorViews.forEach(view => {
                view.resize();
            });
        }, delay);
    }
}
