function appendHeaders(target, headers) {
    if (!headers)
        return;
    if (headers instanceof Headers) {
        headers.forEach((value, key) => {
            target.set(key, value);
        });
        return;
    }
    if (Array.isArray(headers)) {
        for (const [key, value] of headers) {
            target.set(key, value);
        }
        return;
    }
    for (const [key, value] of Object.entries(headers)) {
        if (value !== undefined) {
            target.set(key, value);
        }
    }
}
export function mergeHeaders(...sources) {
    const headers = new Headers();
    for (const source of sources) {
        appendHeaders(headers, source);
    }
    return headers;
}
export async function resolveAuthToken(authToken, getAuthToken) {
    if (getAuthToken) {
        const resolved = await getAuthToken();
        return resolved ?? null;
    }
    return authToken ?? null;
}
export async function resolveHeaders(headers, getHeaders) {
    const resolved = getHeaders ? await getHeaders() : undefined;
    return mergeHeaders(headers, resolved);
}
