import{r as c,L as A,ai as h,j as u,ak as C,aQ as E,al as R,am as T,a_ as B}from"./index-D3uELfdo.js";import{a as v}from"./createDownloadLinkElement-CbRjLJ8e-NbV49OP9.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!#$&+.^_`|~-])+)$/,I=/^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/,U=r=>String(r).replace($,"?");class j{constructor(e,t){this.type=e,this.parameters=t}}const D=(r,e)=>String.fromCharCode(Number.parseInt(e,16));function Z(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=U(a.replace(L,D));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 S(r){let e=I.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=Z(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 _(r){const e=S(r);if(e.type!=="attachment")return;const t=e.parameters.filename;if(typeof t=="string")return t}function N(r,e){return 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&&_(l))??"",o=a.get("Content-Type"),f=new Blob([n],o?{type:o}:void 0),d=URL.createObjectURL(f),s=document.createElement("a");s.setAttribute("href",d),s.setAttribute("target","_blank"),s.setAttribute("download",i),s.click(),URL.revokeObjectURL(d),s.remove()})}function O(){const r=B();return c.useCallback(e=>N(r,e),[r])}function M(r){const{disabled:e,element:t,widgetMgr:a,endpoints:n,fragmentId:l}=r,{help:i,label:o,icon:f,ignoreRerun:d,type:s,url:m}=t,{libConfig:{enforceDownloadInNewTab:b=!1}}=c.useContext(A);let p=h.SECONDARY;s==="primary"?p=h.PRIMARY:s==="tertiary"&&(p=h.TERTIARY);const x=c.useMemo(()=>n.buildDownloadUrl(m),[n,m]);c.useEffect(()=>{n.checkSourceUrlResponse(x,"Download Button")},[x,n]);const g=O(),y=()=>{if(d||a.setTriggerValue(t,{fromUi:!0},l),t.url.startsWith("/media")){g(t.url);return}v({filename:"",url:x,enforceDownloadInNewTab:b}).click()};return u.jsx("div",{className:"stDownloadButton","data-testid":"stDownloadButton",children:u.jsx(C,{help:i,containerWidth:!0,children:u.jsx(E,{kind:p,size:R.SMALL,disabled:e,onClick:y,containerWidth:!0,children:u.jsx(T,{icon:f,label:o})})})})}const Y=c.memo(M);export{Y as default};
