"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Peer_1 = require("../../../../../api/peer/Peer");
const AsyncWorker_1 = require("../../../../../AsyncWorker");
const STRING_1 = require("../../../../../constant/STRING");
const Primitive_1 = require("../../../../../Primitive");
const RemotePort_1 = require("../../../../../RemotePort");
const evaluate_1 = require("../../../../../spec/evaluate");
const stringify_1 = require("../../../../../spec/stringify");
const UCGEE_1 = require("../../../../../types/interface/UCGEE");
const wrap_1 = require("../../../../../wrap");
const _ids_1 = require("../../../../_ids");
class PeerGraph extends Primitive_1.Primitive {
    constructor(system) {
        super({
            i: ['opt', 'offer', 'close'],
            o: ['graph', 'answer'],
        }, {
            output: {
                graph: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_PEER_GRAPH);
        this.__ = ['U'];
        this._peer_connected = false;
        this._offer_sent = false;
        this._accept_offer = async (offer) => {
            await this._peer.acceptOffer(offer);
            const answer = await this._peer.answer();
            this._output.answer.push(answer);
            this._offer_sent = true;
        };
        this.__forward_empty = () => {
            this._forward_all_empty();
            this._backward('offer');
            this._backward('close');
            this._backward('opt');
            this._forwarding_empty = true;
            if (this.hasErr()) {
                this.takeErr();
            }
            this._disconnect();
            this._forwarding_empty = false;
        };
        this._disconnect = () => {
            // console.log('PeerGraph', '_close')
            this._backwarding = true;
            this._output.graph.pull();
            if (this._remote_port) {
                this._remote_port.close();
                this._remote_port = undefined;
            }
            if (this._peer) {
                // this._peer.close()
                this._peer = null;
            }
            this._backwarding = false;
        };
        this.addListener('destroy', () => {
            this.__forward_empty();
        });
        this.addListener('take_err', () => {
            //
        });
        this.addListener('take_caught_err', () => {
            //
        });
    }
    async onDataInputData(name, data) {
        // console.log('PeerGraph', 'onDataInputData', name, data)
        if (name === 'opt') {
            this._peer = new Peer_1.Peer(this.__system, false, data);
            const port = {
                send: (data) => {
                    // console.log('PeerGraph', 'postMessage', data)
                    const message = (0, stringify_1.stringify)(data);
                    this._peer.send(message);
                },
                onmessage(data) { },
                onerror() {
                    // console.log('onerror')
                },
            };
            this._peer.addListener('message', (message) => {
                // console.log('PeerGraph', 'message', message)
                const specs = this.__system.specs;
                const classes = this.__system.classes;
                const data = (0, evaluate_1.evaluate)(message, specs, classes);
                const { type, data: _data } = data;
                switch (type) {
                    case STRING_1.EXEC:
                        {
                            port.onmessage({ data: _data });
                        }
                        break;
                    case STRING_1.INIT:
                        {
                            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._disconnect();
                        }
                        break;
                }
            });
            this._peer.addListener('connect', () => {
                // console.log('PeerGraph', 'peer', 'connect')
                this._peer_connected = true;
            });
            this._peer.addListener('close', () => {
                // console.log('PeerGraph', 'peer', 'close')
                this._peer_connected = false;
            });
            const offer = this._input.offer.peak();
            if (offer !== undefined) {
                this._accept_offer(offer);
            }
        }
        else if (name === 'offer') {
            if (this._peer) {
                const offer = data;
                this._accept_offer(offer);
            }
        }
        else if (name === 'close') {
            this.__forward_empty();
        }
    }
    onDataInputDrop(name) {
        if (name === 'opt') {
            if (!this._backwarding) {
                this.__forward_empty();
            }
        }
    }
}
exports.default = PeerGraph;
//# sourceMappingURL=index.js.map