"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AsyncWorker_1 = require("../../../../AsyncWorker");
const Holder_1 = require("../../../../Class/Holder");
const RemotePort_1 = require("../../../../RemotePort");
const STRING_1 = require("../../../../constant/STRING");
const UCGEE_1 = require("../../../../types/interface/UCGEE");
const wrap_1 = require("../../../../wrap");
const _ids_1 = require("../../../_ids");
class Remote extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['opt'],
            fo: [],
            i: ['message'],
            o: ['message', 'graph'],
        }, {
            output: {
                graph: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_REMOTE_GRAPH, 'close');
        this.addListener('take_err', () => {
            //
        });
        this.addListener('take_caught_err', () => {
            //
        });
    }
    f({ opt }, done) {
        //
    }
    d() {
        if (this._remote_port) {
            this._remote_port.close();
            this._remote_port = undefined;
        }
    }
    async onIterDataInputData(name, _data) {
        // console.log('Remote', 'onIterDataInputData', name, message)
        super.onIterDataInputData(name, _data);
        if (name === 'message') {
            const { type, data } = _data;
            switch (type) {
                case STRING_1.EXEC:
                    {
                        this._port.onmessage({ data });
                    }
                    break;
                case STRING_1.INIT:
                    {
                        const port = {
                            send: (data) => {
                                this._output.message.push(data);
                            },
                            onmessage(data) {
                                // TODO
                            },
                            onerror() {
                                // TODO
                            },
                        };
                        this._port = port;
                        const remote_port = new RemotePort_1.RemotePort(port);
                        this._remote_port = remote_port;
                        const $graph = (0, AsyncWorker_1.AsyncWorker)(remote_port, UCGEE_1.UCGEE);
                        const graph = (0, wrap_1.$wrap)(this.__system, $graph, UCGEE_1.UCGEE);
                        this._output.graph.push(graph);
                    }
                    break;
                case STRING_1.TERMINATE:
                    {
                        this.d();
                    }
                    break;
            }
            this._backward('message');
        }
    }
}
exports.default = Remote;
//# sourceMappingURL=index.js.map