"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@apple/cktool.core"),t=require("http"),n=require("https"),r=require("path"),o=require("url");function a(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var s=a(t),i=a(n),l=a(r);function c(e,t){return function(e,t){if(t.get)return t.get.call(e);return t.value}(e,d(e,t,"get"))}function u(e,t,n){return function(e,t,n){if(t.set)t.set.call(e,n);else{if(!t.writable)throw new TypeError("attempted to set read only private field");t.value=n}}(e,d(e,t,"set"),n),n}function d(e,t,n){if(!t.has(e))throw new TypeError("attempted to "+n+" private field on non-instance");return t.get(e)}function f(e,t,n){!function(e,t){if(t.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")}(e,t),t.set(e,n)}if(!globalThis.ReadableStream)try{const e=require("node:process"),{emitWarning:t}=e;try{e.emitWarning=()=>{},Object.assign(globalThis,require("node:stream/web")),e.emitWarning=t}catch(n){throw e.emitWarning=t,n}}catch(e){Object.assign(globalThis,require("web-streams-polyfill/dist/ponyfill.es2018.js"))}try{const{Blob:e}=require("buffer");e&&!e.prototype.stream&&(e.prototype.stream=function(e){let t=0;const n=this;return new ReadableStream({type:"bytes",async pull(e){const r=n.slice(t,Math.min(n.size,t+65536)),o=await r.arrayBuffer();t+=o.byteLength,e.enqueue(new Uint8Array(o)),t===n.size&&e.close()}})})}catch(e){}var p,h,y,g;let b,m;async function*w(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];for(const n of e)if("stream"in n)yield*n.stream();else if(ArrayBuffer.isView(n))if(t){let e=n.byteOffset;const t=n.byteOffset+n.byteLength;for(;e!==t;){const r=Math.min(t-e,65536),o=n.buffer.slice(e,e+r);e+=o.byteLength,yield new Uint8Array(o)}}else yield n;else{let e=0,t=n;for(;e!==t.size;){const n=t.slice(e,Math.min(t.size,e+65536)),r=await n.arrayBuffer();e+=r.byteLength,yield new Uint8Array(r)}}}const v=(p=new WeakMap,h=new WeakMap,y=new WeakMap,g=new WeakMap,b=Symbol.toStringTag,m=Symbol.hasInstance,class e{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(f(this,p,{writable:!0,value:[]}),f(this,h,{writable:!0,value:""}),f(this,y,{writable:!0,value:0}),f(this,g,{writable:!0,value:"transparent"}),"object"!=typeof t||null===t)throw new TypeError("Failed to construct 'Blob': The provided value cannot be converted to a sequence.");if("function"!=typeof t[Symbol.iterator])throw new TypeError("Failed to construct 'Blob': The object must have a callable @@iterator property.");if("object"!=typeof n&&"function"!=typeof n)throw new TypeError("Failed to construct 'Blob': parameter 2 cannot convert to dictionary.");null===n&&(n={});const r=new TextEncoder;for(const n of t){let t;t=ArrayBuffer.isView(n)?new Uint8Array(n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength)):n instanceof ArrayBuffer?new Uint8Array(n.slice(0)):n instanceof e?n:r.encode(`${n}`);const o=ArrayBuffer.isView(t)?t.byteLength:t.size;o&&(u(this,y,c(this,y)+o),c(this,p).push(t))}u(this,g,`${void 0===n.endings?"transparent":n.endings}`);const o=void 0===n.type?"":String(n.type);u(this,h,/^[\x20-\x7E]*$/.test(o)?o:"")}get size(){return c(this,y)}get type(){return c(this,h)}async text(){const e=new TextDecoder;let t="";for await(const n of w(c(this,p),!1))t+=e.decode(n,{stream:!0});return t+=e.decode(),t}async arrayBuffer(){const e=new Uint8Array(this.size);let t=0;for await(const n of w(c(this,p),!1))e.set(n,t),t+=n.length;return e.buffer}stream(){const e=w(c(this,p),!0);return new globalThis.ReadableStream({type:"bytes",async pull(t){const n=await e.next();n.done?t.close():t.enqueue(n.value)},async cancel(){await e.return()}})}slice(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.size,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"";const{size:o}=this;let a=t<0?Math.max(o+t,0):Math.min(t,o),s=n<0?Math.max(o+n,0):Math.min(n,o);const i=Math.max(s-a,0),l=c(this,p),d=[];let f=0;for(const e of l){if(f>=i)break;const t=ArrayBuffer.isView(e)?e.byteLength:e.size;if(a&&t<=a)a-=t,s-=t;else{let n;ArrayBuffer.isView(e)?(n=e.subarray(a,Math.min(t,s)),f+=n.byteLength):(n=e.slice(a,Math.min(t,s)),f+=n.size),s-=t,d.push(n),a=0}}const h=new e([],{type:String(r).toLowerCase()});return u(h,y,i),u(h,p,d),h}get[b](){return"Blob"}static[m](e){return e&&"object"==typeof e&&"function"==typeof e.constructor&&("function"==typeof e.stream||"function"==typeof e.arrayBuffer)&&/^(Blob|File)$/.test(e[Symbol.toStringTag])}});Object.defineProperties(v.prototype,{size:{enumerable:!0},type:{enumerable:!0},slice:{enumerable:!0}});const $=v;var S,T;let q,A;const R=(S=new WeakMap,T=new WeakMap,q=Symbol.toStringTag,A=Symbol.hasInstance,class extends ${constructor(e,t){let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(arguments.length<2)throw new TypeError(`Failed to construct 'File': 2 arguments required, but only ${arguments.length} present.`);super(e,n),f(this,S,{writable:!0,value:0}),f(this,T,{writable:!0,value:""}),null===n&&(n={});const r=void 0===n.lastModified?Date.now():Number(n.lastModified);Number.isNaN(r)||u(this,S,r),u(this,T,String(t))}get name(){return c(this,T)}get lastModified(){return c(this,S)}get[q](){return"File"}static[A](e){return!!e&&e instanceof $&&/^(File)$/.test(e[Symbol.toStringTag])}});class B{constructor(){var e,t,n;n=[],(t="data")in(e=this)?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n}append(e,t){let n;n="string"==typeof t||t instanceof $?t:Buffer.from(t),this.data.push([e,n])}}const E=()=>new B,j=e=>e instanceof $,M="----WebKitFormBoundary7MA4YWxkTrZu0gW",x=t=>e.callbagMap((e=>Buffer.from(e).toString("utf8")))((t=>e.callbagCreateListenable(((e,n,r)=>{t.arrayBuffer().then((t=>{e(t),r()}),(e=>{n(e)}))})))(t)),D=t=>e.callbagPipe((t=>{const n=t.data.map((t=>{let[n,r]=t;if("string"==typeof r)return e.callbagJust(`--${M}\r\nContent-Disposition: form-data; name="${n}"; \r\n\r\n${r}\r\n`);if(r instanceof Buffer){const t=r.toString("utf8");return e.callbagJust(`--${M}\r\nContent-Disposition: form-data; name="${n}"; filename="unknown"\r\n\r\n${t}\r\n`)}return r instanceof $?e.callbagPipe(x(r),e.callbagMap((e=>{var t;let o="";return o+=`--${M}\r\n`,o+=`Content-Disposition: form-data; name="${n}"; filename="${null!==(t=r.name)&&void 0!==t?t:"unknown"}"\r\n`,void 0!==r.type&&(o+=`Content-Type: "${r.type}"\r\n\r\n`),o+=`${e}\r\n`,o}))):e.callbagThrowError(new Error("NodeFormData has unexpected data and cannot be processed"))}));return e.callbagConcat(...n)})(t),e.callbagReduce(((e,t)=>e+t),""),e.callbagMap((e=>e.length>0?e+`--${M}--`:""))),F=require("safe-buffer").Buffer,C=e=>F.from(e,"binary").toString("utf8");class k extends e.FetchError{constructor(e,t,n){super(e),this.requestError=t,this.request=n}}class L extends e.FetchError{constructor(e,t,n){super(e),this.requestError=t,this.formData=n}}const O=t=>{let{security:n,securitySchemes:r,configuration:a,method:c,path:u,headers:d,body:f,queryParams:p,overrideHeaders:h}=t;a.logger&&a.logger.debug(`nodeRequest$: Request auth: ${n?a.jsonStringify(n):""}`);const{serverUrl:y,logger:g}=a,b=e=>{null==g||g.debug(e)},m=t=>function(){if((arguments.length<=0?void 0:arguments[0])!==e.START)return;const m=arguments.length<=1?void 0:arguments[1];let w=!1,v=!1;const{hostname:$,port:S,protocol:T,pathname:q}=new URL(y),A=T.substring(0,T.length-1),R="http"===A?s:i;let B;const E={...d};if(b(`nodeRequest$: Validated request security: ${n?a.jsonStringify(n):""}`),void 0!==n&&void 0!==r){b("nodeRequest$: Adding auth headers");let t="";for(const o of Object.keys(n)){b(`nodeRequest$: Looking at ${o}`);const a=r[o];if(void 0===a)return v=!1,w=!0,m(e.START,(()=>{})),void m(e.END,new e.InvalidSecuritySchemeError(o,r));{const e=a.in;void 0!==e&&(b(`nodeRequest$: ${a.name} in ${e}`),"cookie"===e?(t.length>0&&(t+=";"),b(`nodeRequest$: appending cookie ${a.name} = ${n[o]}`),t+=`${a.name}=${n[o]}`):(b(`nodeRequest$: Adding ${a.name} = ${n[o]}`),E[a.name]=n[o]))}}t.length>0&&(b(`nodeRequest$: Adding Cookie = ${t}`),E.Cookie=t)}else b("nodeRequest$: No auth headers added");const j=function(){if((arguments.length<=0?void 0:arguments[0])===e.DATA){if(v||w)return;v=!0;const n={};if(p)for(const e of Object.keys(p)){const t=p[e];null!=t&&(n[e]=t.toString())}const r=new o.URLSearchParams(n).toString(),s={method:c,protocol:A+":",hostname:$,port:S,path:l.join(q,u)+(0===r.length?"":"?"+r),headers:{...E,...h}},i=`${s.protocol}//${$}${S?":"+S:""}${s.path}`;b(`nodeRequest$: ${i}`),B=R.request(s,(t=>{const n=e.createResponseStreamHeaders({contentDisposition:t.headers["content-disposition"],contentLength:t.headers["content-length"],contentType:t.headers["content-type"],mapBinaryStringToString:C});m(e.DATA,n),void 0!==t.statusCode&&m(e.DATA,e.createResponseStreamHttpStatus(t.statusCode)),g&&(g.debug(`nodeRequest$: statusCode: ${t.statusCode}`),g.debug("nodeRequest$: Response headers"),g.debug(a.jsonStringify(t.headers)));const r=[];t.on("data",(e=>{r.push(e)})),t.on("end",(()=>{var t,o;b("nodeRequest$: end");const s=null==n||null===(t=n.contentType)||void 0===t?void 0:t.type,i="attachment"===(null==n||null===(o=n.contentDisposition)||void 0===o?void 0:o.type);if(b(`nodeRequest$: received contentType: ${s}, isAttachment: ${i}`),r.length>0){const t=Buffer.concat(r);if(void 0!==s&&e.isTextualMimeType(s)&&!i){var l;const r=a.decodeArrayBufferString(t,null==n||null===(l=n.contentType)||void 0===l?void 0:l.parameters.charset);b("nodeRequest$: emitting string"),m(e.DATA,e.createResponseStreamString(r))}else b("nodeRequest$: emitting ArrayBuffer"),m(e.DATA,e.createResponseStreamArrayBuffer(t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength)))}else b("nodeRequest$: no chunks, emitting empty response"),m(e.DATA,e.createResponseStreamEmptyResponse());v=!1,w=!0,m(e.END)}))})).on("error",(t=>{v=!1,w=!0,m(e.END,new k(t.message,t,B))}));let d=!1,y=!1;for(const e in s.headers){const t=s.headers[e],n="content-type"===e.toLowerCase();d=d||n;const r=n&&"multipart/form-data"===t.toLowerCase();y=y||r,r||B.setHeader(e,t)}g&&(g.debug("nodeRequest$: Request headers"),g.debug(a.jsonStringify(s.headers))),f?(b("nodeRequest$: There is a body to send"),d||y||B.setHeader("Content-Type","application/json;charset=UTF-8"),y?(b("nodeRequest$: Sending as multipart/form-data"),B.setHeader("Content-Type",`multipart/form-data; boundary=${M}`),B.write(t),B.end()):(b("nodeRequest$: Sending as application/json"),B.write(t),B.end())):B.end()}else(arguments.length<=0?void 0:arguments[0])===e.END&&(B&&(B.destroy(),B=void 0),v=!1,w=!0)};m(e.START,j)};if(f){b("nodeRequest$: There is a body to send");let t=!1;if(d)for(const e of Object.keys(d)){const n=d[e],r="content-type"===e.toLowerCase()&&"multipart/form-data"===n.toLowerCase();t=t||r}if(t){b("nodeRequest$: Sending as multipart/form-data");const t=f;return e.callbagCreateListenable(((n,r,o)=>{e.callbagSubscribe(D(t),{next:t=>{e.callbagSubscribe(m(t),{next:e=>n(e),error:e=>{r(e)},complete:()=>o()})},error:e=>{r(new L(e.message,e,t))}})}))}return b("nodeRequest$: Sending as application/json"),m(a.jsonStringify(f))}return m()};function N(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"utf-8";return Buffer.from(e).toString(t)}const z=$,W=R;e.setAsBlobFn((t=>e.Validated.condNEA(t instanceof $,t,new e.ValueNotBlobError(t)))),e.setBase64StringToByteArrayFn((t=>{if(0===t.trim().length)return e.validNEA(new Uint8Array);const n=Buffer.from(t,"base64"),r=new Uint8Array(n.length);for(let e=0,t=n.length;e{const n=Buffer.from(t);return e.validNEA(n.toString("base64"))})),e.setDecodeAsBlobFn((t=>{let{buffer:n,contentType:r,contentDisposition:o}=t;if("attachment"===(null==o?void 0:o.type)){var a,s;const t=null!==(a=null===(s=o.parameters)||void 0===s?void 0:s.filename)&&void 0!==a?a:"unknown";return e.validNEA(new R([n],t,{type:null==r?void 0:r.type}))}return e.validNEA(new $([n],{type:null==r?void 0:r.type}))})),exports.Blob=z,exports.File=W,exports.NodeFetchError=k,exports.NodeFormDataError=L,exports.createConfiguration=t=>new e.Configuration({requestFn:O,createFormDataFn:E,isBlobFn:j,decodeArrayBufferString:N,...t||{}}),exports.nodeRequest$=O;