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 _PortEnvelopeRecipient_port, _PortEnvelopeRecipient_dispatcher;
import { isDescendantHostname } from '@devvit/shared-types/HostnameUtil.js';
import { CentralDispatcher } from './dispatcher/CentralDispatcher.js';
/** Integrates with and names an EnvelopePort. */
export class PortEnvelopeRecipient {
    constructor(hostname, port) {
        _PortEnvelopeRecipient_port.set(this, void 0);
        _PortEnvelopeRecipient_dispatcher.set(this, void 0);
        this.hostname = hostname;
        __classPrivateFieldSet(this, _PortEnvelopeRecipient_port, port, "f");
    }
    post(_caller, envelope) {
        __classPrivateFieldGet(this, _PortEnvelopeRecipient_port, "f").postMessage(envelope);
    }
    dispose() {
        __classPrivateFieldGet(this, _PortEnvelopeRecipient_port, "f").terminate?.();
        return Promise.resolve();
    }
    onBind(dispatcher) {
        __classPrivateFieldSet(this, _PortEnvelopeRecipient_dispatcher, dispatcher, "f");
        __classPrivateFieldGet(this, _PortEnvelopeRecipient_port, "f").onmessage = (event) => this.onMessage(event);
    }
    onUnbind(_dispatcher) {
        __classPrivateFieldSet(this, _PortEnvelopeRecipient_dispatcher, undefined, "f");
        __classPrivateFieldGet(this, _PortEnvelopeRecipient_port, "f").onmessage = null;
    }
    onMessage(event) {
        if (__classPrivateFieldGet(this, _PortEnvelopeRecipient_dispatcher, "f") == null)
            throw Error('No dispatcher bound.');
        const envelope = event.data;
        if (!isDescendantHostname(envelope.src, this.hostname) &&
            this.hostname !== CentralDispatcher.UNRESOLVED_HOSTNAMES) {
            throw new Error(`invalid src: ${envelope.src}, expected ${this.hostname}`);
        }
        __classPrivateFieldGet(this, _PortEnvelopeRecipient_dispatcher, "f").post(this, envelope);
    }
}
_PortEnvelopeRecipient_port = new WeakMap(), _PortEnvelopeRecipient_dispatcher = new WeakMap();
