import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo } from 'react';
import { useDeepCompare } from '@darajs/ui-utils';
import {} from '../../api/http';
const requestExtrasCtx = createContext({ options: {} });
export default requestExtrasCtx;
/**
 * Get request extras to be passed into request function.
 */
export function useRequestExtras() {
    const { options } = useContext(requestExtrasCtx);
    const extras = useMemo(() => {
        return options;
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [useDeepCompare(options)]);
    return extras;
}
/**
 * Request extras provider that sets the default options for all requests.
 *
 * @param props
 * @param props.children Child components
 * @param props.options RequestInit object to provide
 */
export function RequestExtrasProvider({ children, options, }) {
    return _jsx(requestExtrasCtx.Provider, { value: { options }, children: children });
}
/**
 * Merge second request extras into first.
 *
 * Handles merging header objects rather than overwriting.
 *
 * @param a First request init
 * @param b Second request init
 */
function mergeRequestInits(a, b) {
    const mergedHeaders = new Headers(a.headers);
    const bHeaders = new Headers(b.headers);
    bHeaders.forEach((value, key) => {
        mergedHeaders.set(key, value);
    });
    return Object.assign(Object.assign(Object.assign({}, a), b), { headers: mergedHeaders });
}
/**
 * Request extras provider which merges the provided options with the parent options.
 *
 * @param props
 * @param props.children Child components
 * @param props.options RequestInit object to merge with parent options
 */
export function PartialRequestExtrasProvider({ children, options, }) {
    const { options: parentOptions } = useContext(requestExtrasCtx);
    const mergedInits = useMemo(() => mergeRequestInits(parentOptions, options), [parentOptions, options]);
    return _jsx(requestExtrasCtx.Provider, { value: { options: mergedInits }, children: children });
}
//# sourceMappingURL=request-extras-context.js.map