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AI,WAAW,GAAQ,EAAE,CAAC;IAC1B,IAAI,OAAO,GAAG,EAAE,CAAC;IAEjB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC7B,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YAC1B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;gBAChC,WAAW,GAAG,EAAE,CAAC;aACjB;YAED,WAAW,GAAG,CAAC,GAAG,WAAW,EAAE,GAAG,MAAM,CAAC,CAAC;SAC1C;aAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;YAC5B,KAAK,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;gBAChD,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,WAAW,EAAE;oBAC1C,KAAK,GAAG,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;iBAC3C;gBAED,WAAW,GAAG,EAAC,GAAG,WAAW,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAC,CAAC;aAC7C;YAED,IAAI,QAAQ,CAAE,MAAc,CAAC,OAAO,CAAC,EAAE;gBACtC,OAAO,GAAG,YAAY,CAAC,OAAO,EAAG,MAAc,CAAC,OAAO,CAAC,CAAC;gBACzD,WAAW,CAAC,OAAO,GAAG,OAAO,CAAC;aAC9B;SACD;KACD;IAED,OAAO,WAAW,CAAC;AACpB,CAAC,CAAC","sourcesContent":["import type {KyHeadersInit, Options} from '../types/options.js';\nimport {isObject} from './is.js';\n\nexport const validateAndMerge = (...sources: Array | undefined>): Partial => {\n\tfor (const source of sources) {\n\t\tif ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {\n\t\t\tthrow new TypeError('The `options` argument must be an object');\n\t\t}\n\t}\n\n\treturn deepMerge({}, ...sources);\n};\n\nexport const mergeHeaders = (source1: KyHeadersInit = {}, source2: KyHeadersInit = {}) => {\n\tconst result = new globalThis.Headers(source1 as HeadersInit);\n\tconst isHeadersInstance = source2 instanceof globalThis.Headers;\n\tconst source = new globalThis.Headers(source2 as HeadersInit);\n\n\tfor (const [key, value] of source.entries()) {\n\t\tif ((isHeadersInstance && value === 'undefined') || value === undefined) {\n\t\t\tresult.delete(key);\n\t\t} else {\n\t\t\tresult.set(key, value);\n\t\t}\n\t}\n\n\treturn result;\n};\n\n// TODO: Make this strongly-typed (no `any`).\nexport const deepMerge = (...sources: Array | undefined>): T => {\n\tlet returnValue: any = {};\n\tlet headers = {};\n\n\tfor (const source of sources) {\n\t\tif (Array.isArray(source)) {\n\t\t\tif (!Array.isArray(returnValue)) {\n\t\t\t\treturnValue = [];\n\t\t\t}\n\n\t\t\treturnValue = [...returnValue, ...source];\n\t\t} else if (isObject(source)) {\n\t\t\tfor (let [key, value] of Object.entries(source)) {\n\t\t\t\tif (isObject(value) && key in returnValue) {\n\t\t\t\t\tvalue = deepMerge(returnValue[key], value);\n\t\t\t\t}\n\n\t\t\t\treturnValue = {...returnValue, [key]: value};\n\t\t\t}\n\n\t\t\tif (isObject((source as any).headers)) {\n\t\t\t\theaders = mergeHeaders(headers, (source as any).headers);\n\t\t\t\treturnValue.headers = headers;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn returnValue;\n};\n"]}