"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bundle_1 = require("../../../../../bundle");
const Holder_1 = require("../../../../../Class/Holder");
const util_1 = require("../../../../../spec/util");
const Async_1 = require("../../../../../types/interface/async/Async");
const UCGEE_1 = require("../../../../../types/interface/UCGEE");
const clone_1 = require("../../../../../util/clone");
const weakMerge_1 = require("../../../../../weakMerge");
const wrap_1 = require("../../../../../wrap");
const _ids_1 = require("../../../../_ids");
class Start extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['graph', 'system', 'opt'],
            fo: ['graph'],
            i: [],
            o: [],
        }, {
            input: {
                system: {
                    ref: true,
                },
            },
            output: {
                graph: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_START_0);
    }
    f({ graph: Graph, system, opt }, done) {
        system = (0, Async_1.Async)(system, ['S'], this.__system.async);
        const { paused } = opt || {};
        let { __bundle } = Graph;
        __bundle = (0, clone_1.clone)(__bundle);
        const { unit } = __bundle;
        const { id, input = {} } = unit;
        const specs_ = (0, weakMerge_1.weakMerge)(__bundle.specs, this.__system.specs);
        const spec = (0, util_1.getSpec)(specs_, id);
        const bundle = (0, bundle_1.bundleSpec)(spec, specs_, false);
        const $graph = system.$start({ bundle, __: UCGEE_1.UCGEE });
        for (const pinId in input) {
            const pin = input[pinId];
            if (pin.data !== undefined) {
                $graph.$setPinData({
                    type: 'input',
                    pinId,
                    data: pin.data,
                    lastData: undefined,
                });
            }
        }
        const graph = (0, wrap_1.$wrap)(this.__system, $graph, UCGEE_1.UCGEE);
        if (!paused) {
            graph.$play({});
        }
        this._graph = graph;
        done({ graph });
    }
    d() {
        if (this._graph) {
            this._graph.$destroy({});
            this._graph = undefined;
        }
    }
}
exports.default = Start;
//# sourceMappingURL=index.js.map