import{CodeBuilder as _}from"../../helpers/code-builder.js";import{buildUrlExpression as P,printUrlVariablesDeclarations as j}from"../../helpers/code-helpers.js";import{Lang as O}from"../../helpers/constants.js";import{HTTPSnippet as C}from"../../index.js";import{normalizeMimeType as E}from"../../../../utils.js";import{MediaTypes as f}from"../../../../../constants.js";import{postDataToRListBody as R,objectToRString as D}from"./utils.js";import{buildRDeviceAuthorizationCodeSnippet as U}from"../../helpers/device-auth-snippets.js";const p=1,A=function(a,x,{target:g,client:y}){const o=Object.assign({indent:"  ",showBoilerplate:!0,printBody:!0},x),t=new _({indentation:o.indent,variablesPrefix:o.variablesPrefix,lang:O.R}),k=a.method.toUpperCase(),d=a.securityOAuth2ExtraCalls?.[0];if(o.showBoilerplate&&t.push("library(httr)").blank(),d){const e="oauth2_",n=new C(d).convert(g,y,{...o,variablesPrefix:e,showBoilerplate:!1}),i=`${e}data_req`;t.push(n),d.flowType==="deviceAuthorization"?U(t,{tokenUrl:d.tokenUrl??"YOUR_TOKEN_URL",clientId:d.clientId??"YOUR_CLIENT_ID",level:0,responseVariable:i}):(t.push(`access_token = content(${i}, "parsed")$access_token`),t.blank()),a.allHeaders.Authorization='paste("Bearer", access_token)'}let l,s="";if(a?.postData)switch(E(a?.postData?.mimeType)){case f.URL_ENCODED:{if(s=t.var("body"),a.postData.params.length){l="form";const e=R(a.postData.params,o);t.push(`${s} <- list(${e})`).blank()}else a.postData.text&&(l="raw",t.push(`${s} <- "${a.postData.text}"`).blank());break}case f.JSON:{l="json",s=t.var("body");const e=D(a.postData.jsonObj,o);t.push(`${s} = '${e}'`).blank();break}case f.MULTIPART:{if(l="multipart",s=t.var("body"),a.postData.params&&a.postData.params.length>0){const e=[];a.postData.params.forEach(i=>{i.fileName?e.push(`${o.indent}${i.name} = upload_file("${i.fileName}")`):e.push(`${o.indent}${i.name} = "${i.value}"`)});const n=`
`+e.join(`,
`)+`
`;t.push(`${s} <- list(${n})`).blank()}else if(a.postData.text){const e=a.postData.text,n=e.match(/^--([^\r\n]+)/);if(n){const i=n[1],w=e.split(new RegExp(`--${i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}(?:--)?`,"g")).filter(r=>r.trim().length>0),h=[];if(w.forEach(r=>{const c=r.match(/Content-Disposition:\s*form-data;\s*name="([^"]+)"(?:;\s*filename="([^"]+)")?/i);if(c){const b=c[1],m=c[2],u=r.match(/\r?\n\r?\n([\s\S]*?)(?:\r?\n)?$/),T=(u?u[1].trim():"").replace(/\\/g,"\\\\").replace(/"/g,'\\"');m?h.push(`${o.indent}${b} = upload_file("${m}")`):h.push(`${o.indent}${b} = "${T}"`)}}),h.length>0){const r=`
`+h.join(`,
`)+`
`;t.push(`${s} <- list(${r})`).blank()}else{l="raw";const r=a.postData.text.replace(/\\/g,"\\\\").replace(/"/g,'\\"');t.push(`${s} <- "${r}"`).blank()}}else{l="raw";const i=a.postData.text.replace(/\\/g,"\\\\").replace(/"/g,'\\"');t.push(`${s} <- "${i}"`).blank()}}break}default:a.postData?.text&&(s=t.var("body"),l="raw",t.push(`${s} = ${D(a.postData?.text)}`).blank())}j(a,t),Object.keys({...a.pathParameters,...a.serverVariables}).length&&t.blank().push("# create you own operator").push('"%&%" <- function(x, y)paste0(x,y)').blank();const v=P(a,t);t.push(`url = ${v}`).blank();const $=t.var("data_req");if(t.push(`${$} <- ${k}(`),t.push(p,"url,"),a.basicAuth){const{username:e,password:n}=a.basicAuth;t.push(p,`authenticate("${e}", "${n}")`)}if(Object.keys(a.allHeaders).length){const e=`${Object.keys(a.allHeaders).map(n=>`"${n}" = ${a.allHeaders[n].startsWith("paste")?a.allHeaders[n]:`"${a.allHeaders[n]}"`}`).join(", ")}`;e&&t.push(p,`add_headers(${e}),`)}return s&&(t.push(p,`body = ${s},`),t.push(p,`encode = "${l}",`)),t.push(p,"verbose()"),t.push(")"),o.printBody&&t.blank().push(`content(${$}${l==="raw"?', "text"':""})`),t.join()};var z=A;export{z as default};
