import{format as n}from"util";import{CodeBuilder as D}from"../../helpers/code-builder.js";import o from"../../helpers/shell.js";import{addIndentation as x,buildUrlExpression as R}from"../../helpers/code-helpers.js";import{normalizeMimeType as U}from"../../../../utils.js";import{HTTPSnippet as w}from"../../index.js";import{MediaTypes as c}from"../../../../../constants.js";import{buildShellDeviceAuthorizationCodeSnippet as N}from"../../helpers/device-auth-snippets.js";const g=function(e,T,{target:E,client:C}){const t=Object.assign({indent:"  ",short:!1,binary:!1},T),s=new D({indentation:t.indent,lineJoin:t.indent?` \\
`+t.indent:" "}),r=e.securityOAuth2ExtraCalls?.[0];let l="",h="";if(r&&(l=new w(r).convert(E,C,{...t,withComments:!1}),r.flowType==="deviceAuthorization")){l="OAUTH2_RESPONSE=$("+l.replace(/^curl -i/,"curl -s")+")";const a=new D({indentation:t.indent});N(a,{tokenUrl:r.tokenUrl??"YOUR_TOKEN_URL",clientId:r.clientId??"YOUR_CLIENT_ID",level:0,short:t.short}),h=a.join()}s.push(0,"curl -i %s %s",t.short?"-X":"--request",e.method.toUpperCase()),e.basicAuth&&s.push(n("-u %s:%s",e.basicAuth.username,e.basicAuth.password));const A=R(e,s);if(s.push(n("%s%s",t.short?"":"--url ",o.quote(A))),e.httpVersion==="HTTP/1.0"&&s.push(t.short?"-0":"--http1.0"),Object.keys(e.headersObj).sort().forEach(function(a){let i=e.headersObj[a];r?.flowType==="deviceAuthorization"&&a.toLowerCase()==="authorization"&&i==="Bearer <YOUR_TOKEN_HERE>"&&(i="Bearer $ACCESS_TOKEN");const f=n("%s: %s",a,i);s.push("%s %s",t.short?"-H":"--header",o.quote(f))}),e.allHeaders.cookie&&s.push("%s %s",t.short?"-b":"--cookie",o.quote(e.allHeaders.cookie)),e.postData)switch(U(e.postData.mimeType)){case c.MULTIPART:if(e.postData?.params?.length>0)e.postData.params.map(function(a){let i;a.fileName?i=n("%s=@%s",a.name,a.fileName):i=n("%s=%s",a.name,a.value),s.push("%s %s",t.short?"-F":"--form",o.quote(i))});else if(e.postData.text){const a=e.postData.text,i=a.match(/^--([^\r\n]+)/);if(i){const f=i[1];a.split(new RegExp(`--${f.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}(?:--)?`,"g")).filter(d=>d.trim().length>0).forEach(d=>{const m=d.match(/Content-Disposition:\s*form-data;\s*name="([^"]+)"(?:;\s*filename="([^"]+)")?/i);if(m){const b=m[1],y=m[2],O=d.match(/\r?\n\r?\n([\s\S]*?)(?:\r?\n)?$/),q=O?O[1].trim():"";let u;y?u=n("%s=@%s",b,y):u=n("%s=%s",b,q);const v=n("'%s'",u.replace(/'/g,"'\\''"));s.push("%s %s",t.short?"-F":"--form",v)}})}else s.push("%s %s",t.binary?"--data-binary":t.short?"-d":"--data",o.quote(e.postData.text))}break;case c.URL_ENCODED:e.postData?.params?.length>0?e.postData.params.map(function(a){s.push("%s %s",t.binary?"--data-binary":t.short?"-d":"--data",o.quote(n("%s=%s",a.name,a.value)))}):s.push("%s %s",t.binary?"--data-binary":t.short?"-d":"--data",o.quote(e.postData.text));break;case c.JSON:e.postData.jsonObj&&s.push("%s %s",t.binary?"--data-binary":t.short?"-d":"--data",x(o.quote(JSON.stringify(e.postData.jsonObj,null,t.indent)),{level:1,indent:t.indent,firstLine:!1}));break;default:e.postData.text&&s.push("%s %s",t.binary?"--data-binary":t.short?"-d":"--data",x(o.quote(e.postData.text),{level:1,indent:t.indent,firstLine:!1}))}let p="";return t.withComments&&(p=`# Requires curl >= 7.2
`,r?.flowType==="deviceAuthorization"&&(p+=`# Requires jq for JSON parsing
`),p+=`
`),p+(l?l+`
`:"")+(h?h+`
`:"")+s.join()},M={key:"curl",title:"curl",link:"http://curl.haxx.se/",description:"cURL is a command line tool and library for transferring data with URL syntax"};var $=g;export{$ as default,M as info};
