/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 * @generated-id: 74859f750b28
 */
class InvariantError extends Error {
    constructor(message) {
        super(message);
        this.name = "InvariantError";
    }
}
export function abortSignalAny(signals) {
    const controller = new AbortController();
    const result = controller.signal;
    if (!signals.length) {
        return controller.signal;
    }
    if (signals.length === 1) {
        return signals[0] || controller.signal;
    }
    for (const signal of signals) {
        if (signal.aborted) {
            return signal;
        }
    }
    function abort() {
        controller.abort(this.reason);
        clean();
    }
    const signalRefs = [];
    function clean() {
        for (const signalRef of signalRefs) {
            const signal = signalRef.deref();
            if (signal) {
                signal.removeEventListener("abort", abort);
            }
        }
    }
    for (const signal of signals) {
        signalRefs.push(new WeakRef(signal));
        signal.addEventListener("abort", abort);
    }
    return result;
}
export function allRequired(v) {
    if (Object.values(v).every((x) => x == null)) {
        return void 0;
    }
    return v;
}
export function combineSignals(...signals) {
    const filtered = [];
    for (const signal of signals) {
        if (signal) {
            filtered.push(signal);
        }
    }
    switch (filtered.length) {
        case 0:
        case 1:
            return filtered[0] || null;
        default:
            if ("any" in AbortSignal && typeof AbortSignal.any === "function") {
                return AbortSignal.any(filtered);
            }
            return abortSignalAny(filtered);
    }
}
export function compactMap(values) {
    const out = {};
    for (const [k, v] of Object.entries(values)) {
        if (typeof v !== "undefined") {
            out[k] = v;
        }
    }
    return out;
}
export function invariant(condition, message) {
    if (!condition) {
        throw new InvariantError(message);
    }
}
//# sourceMappingURL=primitives.js.map