"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AsyncWorker_1 = require("../../../../AsyncWorker");
const Functional_1 = require("../../../../Class/Functional");
const worker_1 = require("../../../../client/platform/web/port/worker");
const RemoteClient_1 = require("../../../../RemoteClient");
const wrap_1 = require("../../../../wrap");
const _ids_1 = require("../../../_ids");
class Worker_ extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['init'],
            o: ['system'],
        }, {
            output: {
                system: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_WORKER);
    }
    f({ init }, done, fail) {
        const { specs, api: { worker: { start }, }, } = this.__system;
        let worker;
        try {
            worker = start();
        }
        catch (err) {
            fail(err.message);
            return;
        }
        const port = (0, worker_1.workerPort)(worker);
        const client = new RemoteClient_1.RemoteClient(port);
        const remote = client.port();
        const $system = (0, AsyncWorker_1.AsyncWorker)(remote, ['S']);
        const system = (0, wrap_1.$wrap)(this.__system, $system);
        done({
            system,
        });
    }
    d() {
        if (this._worker) {
            this._worker.terminate();
            this._worker = undefined;
        }
    }
}
exports.default = Worker_;
//# sourceMappingURL=index.js.map