"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Element_1 = require("../../../../../Class/Element");
const fromBundle_1 = require("../../../../../spec/fromBundle");
const fromSpec_1 = require("../../../../../spec/fromSpec");
const util_1 = require("../../../../../spec/util");
const Async_1 = require("../../../../../types/interface/async/Async");
const UCGEE_1 = require("../../../../../types/interface/UCGEE");
const Object_1 = require("../../../../../wrap/Object");
const _ids_1 = require("../../../../_ids");
const globalComponent_1 = require("../../../../globalComponent");
class Editor extends Element_1.Element_ {
    constructor(system) {
        super({
            i: [
                'graph',
                'style',
                'disabled',
                'fullwindow',
                'frame',
                'zoom',
                'controls',
                'config',
                'attr',
                'value',
            ],
            o: ['graph', 'state', 'value'],
        }, {
            input: {
                graph: {
                    ref: true,
                },
                frame: {
                    ref: true,
                },
            },
            output: {
                graph: {
                    ref: true,
                },
                state: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_EDITOR);
        this.__ = ['U', 'C', 'V', 'J', 'G', 'EE'];
        this._fallback = () => {
            const { specs, classes } = this.__system;
            const id = (0, util_1.newSpecId)(specs);
            const spec = this.__system.newSpec((0, util_1.emptySpec)({ id }));
            this.__system.lockSpec(id);
            const Class = (0, fromSpec_1.fromSpec)(spec, specs, classes, {});
            const fallback_graph = new Class(this.__system);
            this._fallback_graph = fallback_graph;
            this._fallback_graph.play();
        };
        this._fallback();
        this._input.graph.push(this._fallback_graph);
        this.register();
        void (async () => {
            const component = (await (0, globalComponent_1.firstGlobalComponentPromise)(this.__system, this.__global_id));
            const state = (0, Object_1.wrapObject)({
                get bundle() {
                    return component.getBundle();
                },
                get zoom() {
                    return component.getZoom();
                },
                set zoom(zoom) {
                    component.setZoom(zoom);
                },
                get rect() {
                    return component.getRect();
                },
                get selected() {
                    return component.getSelectedNodes();
                },
                set selected(selected) {
                    component.setSelectedNodes(selected);
                },
                get path() {
                    return component.getSubgraphPath();
                },
                set path(path) {
                    component.setSubgraphPath(path);
                },
            }, this.__system);
            this._output.state.push(state);
        })();
    }
    onRefInputInvalid(name) {
        // console.log('Editor', 'onRefInputInvalid', name)
        if (name === 'graph') {
            //
        }
    }
    onRefInputData(name, data) {
        super.onRefInputData(name, data);
        // console.log('Editor', 'onRefInputData', name, data)
        if (name === 'graph') {
            if (this._input.graph.embodied()) {
                data = (0, Async_1.Async)(data, UCGEE_1.UCGEE, this.__system.async);
            }
            this._graph = data;
            data.addListener('destroy', (path) => {
                if (path.length > 0) {
                    return;
                }
                if (this._graph === this._fallback_graph) {
                    this._fallback();
                }
            });
            if (this._input.graph.embodied()) {
                data.$play();
            }
            this._output.graph.push(data);
        }
    }
    onRefInputDrop(name) {
        super.onRefInputDrop(name);
        // console.log('Editor', 'onRefInputDrop', name)
        if (name === 'graph') {
            this._graph = this._fallback_graph;
            this._input.graph.push(this._fallback_graph);
            this._output.graph.push(this._fallback_graph);
        }
    }
    onRefOutputData(name, data) {
        // console.log('Editor', 'onRefOutputData', name, data)
    }
    snapshotSelf(opt = {}) {
        const bundle = this._fallback_graph.getUnitBundleSpec(opt);
        return {
            ...super.snapshotSelf(),
            _fallback_graph: bundle,
        };
    }
    restoreSelf(state) {
        const { _fallback_graph, ...rest } = state;
        super.restoreSelf(rest);
        const Class = (0, fromBundle_1.fromBundle)(_fallback_graph, this.__system.specs, {});
        const graph = new Class(this.__system);
        this._fallback_graph = graph;
    }
}
exports.default = Editor;
//# sourceMappingURL=index.js.map