"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Peer_1 = require("../../../../../api/peer/Peer");
const Semifunctional_1 = require("../../../../../Class/Semifunctional");
const makeUnitRemoteRef_1 = require("../../../../../client/makeUnitRemoteRef");
const STRING_1 = require("../../../../../constant/STRING");
const evaluate_1 = require("../../../../../spec/evaluate");
const stringify_1 = require("../../../../../spec/stringify");
const UCGEE_1 = require("../../../../../types/interface/UCGEE");
const _ids_1 = require("../../../../_ids");
class PeerShareGraph extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['graph', 'opt'],
            fo: ['offer'],
            i: ['answer', 'close'],
            o: [],
        }, {
            input: {
                graph: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_PEER_SHARE_GRAPH);
        this.__ = ['U'];
        this._connected = false;
        this._disconnect = () => {
            this._send_terminate();
            this._peer = null;
        };
        this._close = () => {
            this._ref = null;
        };
        this._send = (data) => {
            const message = (0, stringify_1.stringify)(data);
            this._peer.send(message);
        };
        this._send_init = () => {
            this._send({ type: STRING_1.INIT });
        };
        this._send_exec = (data) => {
            this._send({ type: STRING_1.EXEC, data });
        };
        this._send_terminate = () => {
            if (this._connected) {
                this._send({ type: STRING_1.TERMINATE });
            }
        };
    }
    f({ graph, opt }, done) {
        const peer = new Peer_1.Peer(this.__system, true, opt);
        this._connected = false;
        peer.addListener('connect', () => {
            // console.log('PeerShareGraph', 'connect')
            this._connected = true;
            if (this._input.graph.active()) {
                this._send_init();
            }
        });
        peer.addListener('close', () => {
            // console.log('PeerShareGraph', 'close')
            this._connected = false;
        });
        peer.addListener('message', (message) => {
            // console.log('PeerShareGraph', 'message', message)
            if (this._ref) {
                const specs = this.__system.specs;
                const classes = this.__system.classes;
                const data = (0, evaluate_1.evaluate)(message, specs, classes);
                this._ref.exec(data);
            }
        });
        this._peer = peer;
        (async () => {
            const offer = await peer.offer();
            done({
                offer,
            });
        })();
        const ref = (0, makeUnitRemoteRef_1.makeUnitRemoteRef)(graph, UCGEE_1.UCGEE, (data) => {
            this._send_exec(data);
        });
        this._ref = ref;
        if (this._connected) {
            this._send_init();
        }
        if (this._input.answer.active()) {
            ;
            (async () => {
                await this._peer.acceptAnswer(this._input.answer.peak());
            })();
        }
    }
    d() {
        if (this._connected) {
            this._disconnect();
            this._close();
        }
    }
    async onIterDataInputData(name, data) {
        if (name === 'answer') {
            if (this._peer) {
                await this._peer.acceptAnswer(data);
            }
        }
        else if (name === 'close') {
            this._disconnect();
        }
    }
    async onIterDataInputDrop(name) {
        if (name === 'answer') {
            this._disconnect();
        }
    }
}
exports.default = PeerShareGraph;
//# sourceMappingURL=index.js.map