export function stripRequestControls(options) {
    if (!options) {
        return undefined;
    }
    const rest = { ...options };
    delete rest.timeout;
    delete rest.maxRetries;
    delete rest.retry;
    return rest;
}
export function mergeRequestOptions(base, override) {
    if (!base) {
        return override;
    }
    if (!override) {
        return base;
    }
    const mergeableOverride = override;
    return {
        ...base,
        ...override,
        headers: mergeHeaders(base.headers, mergeableOverride.headers),
        signal: mergeableOverride.signal ?? base.signal,
        timeout: mergeableOverride.timeout ?? base.timeout,
        maxRetries: mergeableOverride.maxRetries ?? base.maxRetries,
        retry: mergeableOverride.retry ?? base.retry,
    };
}
export function mergeScopedRequestOptions(base, override) {
    return mergeRequestOptions(base, override) ?? override;
}
export function mergeHeaders(base, override) {
    if (!base && !override) {
        return undefined;
    }
    const headers = new Headers();
    let hasEntries = false;
    applyHeaders(headers, base, () => {
        hasEntries = true;
    });
    applyHeaders(headers, override, () => {
        hasEntries = true;
    });
    return hasEntries ? headers : undefined;
}
function applyHeaders(target, source, onSet) {
    if (!source) {
        return;
    }
    if (source instanceof Headers) {
        source.forEach((value, key) => {
            target.set(key, value);
            onSet();
        });
        return;
    }
    if (Array.isArray(source)) {
        for (const [key, value] of source) {
            if (!key) {
                continue;
            }
            if (value === null || value === undefined) {
                target.delete(key);
                continue;
            }
            target.set(key, String(value));
            onSet();
        }
        return;
    }
    for (const [key, value] of Object.entries(source)) {
        if (value === null || value === undefined) {
            target.delete(key);
            continue;
        }
        target.set(key, Array.isArray(value) ? value.map((item) => String(item)).join(', ') : String(value));
        onSet();
    }
}
//# sourceMappingURL=request-options.js.map