var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _WorkerEnvelopeRecipient_instances, _WorkerEnvelopeRecipient_logger, _WorkerEnvelopeRecipient_recipient, _WorkerEnvelopeRecipient_worker, _WorkerEnvelopeRecipient_registerLogger;
// upstream needs `exports.type` DX-268
// @ts-expect-error
import Worker from '@devvit/web-worker';
import { Log } from '../../lib/loggers/Log.js';
import { PortEnvelopeRecipient } from '../envelope/PortEnvelopeRecipient.js';
export class WorkerEnvelopeRecipient {
    constructor(hostname, bootstrapPath, logger) {
        _WorkerEnvelopeRecipient_instances.add(this);
        _WorkerEnvelopeRecipient_logger.set(this, void 0);
        _WorkerEnvelopeRecipient_recipient.set(this, void 0);
        _WorkerEnvelopeRecipient_worker.set(this, void 0);
        __classPrivateFieldSet(this, _WorkerEnvelopeRecipient_logger, logger, "f");
        __classPrivateFieldSet(this, _WorkerEnvelopeRecipient_worker, new Worker(bootstrapPath, { name: hostname }), "f");
        __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_instances, "m", _WorkerEnvelopeRecipient_registerLogger).call(this, 'add', __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_worker, "f"), __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_logger, "f"));
        __classPrivateFieldSet(this, _WorkerEnvelopeRecipient_recipient, new PortEnvelopeRecipient(hostname, __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_worker, "f")), "f");
    }
    get hostname() {
        return __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_recipient, "f").hostname;
    }
    post(caller, envelope) {
        __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_recipient, "f").post(caller, envelope);
    }
    async dispose() {
        await __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_recipient, "f").dispose();
        __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_instances, "m", _WorkerEnvelopeRecipient_registerLogger).call(this, 'remove', __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_worker, "f"), __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_logger, "f"));
        Log.verbose(`${__classPrivateFieldGet(this, _WorkerEnvelopeRecipient_recipient, "f").hostname} worker terminated.`);
    }
    onBind(dispatcher) {
        __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_recipient, "f").onBind(dispatcher);
    }
    onUnbind(dispatcher) {
        __classPrivateFieldGet(this, _WorkerEnvelopeRecipient_recipient, "f").onUnbind(dispatcher);
    }
}
_WorkerEnvelopeRecipient_logger = new WeakMap(), _WorkerEnvelopeRecipient_recipient = new WeakMap(), _WorkerEnvelopeRecipient_worker = new WeakMap(), _WorkerEnvelopeRecipient_instances = new WeakSet(), _WorkerEnvelopeRecipient_registerLogger = function _WorkerEnvelopeRecipient_registerLogger(register, worker, logger) {
    for (const type of ['error', 'messageerror', 'unhandledrejection'])
        worker[`${register}EventListener`](type, logger);
};
