"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Semifunctional_1 = require("../../../../../Class/Semifunctional");
const Socket_1 = require("../../../../../wrap/Socket");
const _ids_1 = require("../../../../_ids");
class Socket extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['url'],
            fo: ['channel'],
            i: ['close'],
            o: [],
        }, {}, system, _ids_1.ID_SOCKET);
        this._web_socket = null;
        this._plunk = () => {
            this._web_socket.onopen = null;
            this._web_socket.onmessage = null;
            this._web_socket.onerror = null;
            this._web_socket.onclose = null;
            this._web_socket = null;
        };
    }
    f({ url }, done) {
        if (this._web_socket) {
            this._web_socket.close();
        }
        this._web_socket = new WebSocket(url);
        this._web_socket.onopen = () => {
            // console.log('Socket', 'onopen')
        };
        this._web_socket.onmessage = (message) => {
            // console.log('Socket', 'onmessage', message)
            this.emit('message', message.data);
        };
        this._web_socket.onerror = (event) => {
            // console.log('Socket', 'onerror')
            this.err('error'); // TODO
        };
        this._web_socket.onclose = (event) => {
            const { code, reason } = event;
            // console.log('Socket', 'onclose', code, reason)
            this._plunk();
        };
        const channel = (0, Socket_1.wrapWebSocket)(this._web_socket, this.__system);
        done({
            channel,
        });
    }
    d() {
        this._plunk();
    }
    onIterDataInputData(name, data) {
        // if (name === 'close') {
        this._close();
        this._done();
        // }
    }
    _close() {
        if (this._web_socket) {
            if (this._web_socket.readyState === WebSocket.OPEN) {
                this._web_socket.close();
            }
        }
    }
    send(data) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this._web_socket) {
                if (this._web_socket.readyState === WebSocket.OPEN) {
                    this._web_socket.send(data);
                }
            }
        });
    }
}
exports.default = Socket;
//# sourceMappingURL=index.js.map