import{r as d,L as A,bF as h,cb as C,j as f,bH as R,bX as T,bE as E,bI as v}from"./index-DCNLpqIv.js";import{c as I}from"./createDownloadLinkElement-ZaXNnPK4.js";const L=/%([0-9A-Fa-f]{2})/g,$=/[^\x20-\x7e\xa0-\xff]/g,k=/\\([\u0000-\u007f])/g,w=/;[\x09\x20]*([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*=[\x09\x20]*("(?:[\x20!\x23-\x5b\x5d-\x7e\x80-\xff]|\\[\x20-\x7e])*"|[!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*/g,z=/^([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[A-Za-z]{2,3}(?:-[A-Za-z]{3}){0,3}|[A-Za-z]{4,8}|)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/,U=/^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/,D=r=>String(r).replace($,"?");class j{constructor(e,t){this.type=e,this.parameters=t}}const Z=(r,e)=>String.fromCharCode(Number.parseInt(e,16));function _(r){const e=z.exec(r);if(!e)throw new TypeError("invalid extended field value");const t=e[1].toLowerCase(),a=e[2];let n;switch(t){case"iso-8859-1":n=D(a.replace(L,Z));break;case"utf-8":try{n=decodeURIComponent(a)}catch{throw new TypeError("invalid encoded utf-8")}break;default:throw new TypeError("unsupported charset in extended field")}return n}function N(r){let e=U.exec(r);if(!e)throw new TypeError("invalid type format");let t=e[0].length;const a=e[1].toLowerCase();let n;const l=[],i={};let o;for(t=w.lastIndex=e[0].slice(-1)===";"?t-1:t;e=w.exec(r);){if(e.index!==t)throw new TypeError("invalid parameter format");if(t+=e[0].length,n=e[1].toLowerCase(),o=e[2],l.indexOf(n)!==-1)throw new TypeError("invalid duplicate parameter");if(l.push(n),n.indexOf("*")+1===n.length){n=n.slice(0,-1),o=_(o),i[n]=o;continue}typeof i[n]!="string"&&(o[0]==='"'&&(o=o.slice(1,o.length-1).replace(k,"$1")),i[n]=o)}if(t!==-1&&t!==r.length)throw new TypeError("invalid parameter format");return new j(a,i)}function S(r){const e=N(r);if(e.type!=="attachment")return;const t=e.parameters.filename;if(typeof t=="string")return t}function B(r,e){r.sendHttpRequest({method:"GET",path:`${e}?title=${encodeURIComponent(document.title)}`,headers:{},body:""}).then(({statusCode:t,headers:a,body:n})=>{if(t!==200)return;const l=a.get("Content-Disposition"),i=(l&&S(l))??"",o=a.get("Content-Type"),u=new Blob([n],o?{type:o}:void 0),c=URL.createObjectURL(u),s=document.createElement("a");s.setAttribute("href",c),s.setAttribute("target","_blank"),s.setAttribute("download",i),s.click(),URL.revokeObjectURL(c),s.remove()})}function O(r){const{disabled:e,element:t,widgetMgr:a,endpoints:n,fragmentId:l}=r,{help:i,label:o,icon:u,ignoreRerun:c,type:s,url:m}=t,{libConfig:{enforceDownloadInNewTab:b=!1}}=d.useContext(A);let p=h.SECONDARY;s==="primary"?p=h.PRIMARY:s==="tertiary"&&(p=h.TERTIARY);const x=d.useMemo(()=>n.buildDownloadUrl(m),[n,m]);d.useEffect(()=>{n.checkSourceUrlResponse(x,"Download Button")},[x,n]);const g=C(),y=()=>{if(c||a.setTriggerValue(t,{fromUi:!0},l),t.url.startsWith("/media")){B(g,t.url);return}I({filename:"",url:x,enforceDownloadInNewTab:b}).click()};return f.jsx("div",{className:"stDownloadButton","data-testid":"stDownloadButton",children:f.jsx(R,{help:i,containerWidth:!0,children:f.jsx(T,{kind:p,size:E.SMALL,disabled:e,onClick:y,containerWidth:!0,children:f.jsx(v,{icon:u,label:o})})})})}const M=d.memo(O);export{M as default};
