import{Dn as e,Ht as t,Ja as n,Qa as r,Wt as i,Ya as a,an as o,dr as s,er as c,in as l,io as u,nr as d,rn as f,tr as p}from"./index.dkY5s53S.js";import{t as m}from"./createDownloadLinkElement.sTIOLR-H.js";import{t as h}from"./iconPosition.CY47f4C7.js";var g=u(r(),1);function _(r){let{disabled:u,element:_,widgetMgr:v,endpoints:y,fragmentId:b}=r,{help:x,label:S,icon:C,ignoreRerun:w,type:T,url:E,deferredFileId:D}=_,O=_.shortcut?_.shortcut:void 0,[k,A]=(0,g.useState)(!1),[j,M]=(0,g.useState)(null),{enforceDownloadInNewTab:N=!1}=(0,g.useContext)(s),{backendOperationClient:P}=(0,g.useContext)(i),F=p.SECONDARY;T===`primary`?F=p.PRIMARY:T===`tertiary`&&(F=p.TERTIARY);let I=(0,g.useMemo)(()=>y.buildDownloadUrl(E),[y,E]);(0,g.useEffect)(()=>{D?.length||y.checkSourceUrlResponse(I,`Download Button`)},[I,y,D]);let L=(0,g.useCallback)(async()=>{if(!P||!D){M(`Deferred download not properly configured`);return}A(!0),M(null);try{let e=await P.requestDeferredFile(D),t=y.buildDownloadUrl(e.url);y.checkSourceUrlResponse(t,`Download Button`),m({filename:``,url:t,enforceDownloadInNewTab:N}).click()}catch(e){M(e instanceof Error?e.message:`Download failed`)}finally{A(!1)}},[P,D,y,N]),R=(0,g.useCallback)(()=>{u||(w||v.setTriggerValue(_,{fromUi:!0},b),D?.length?L():m({filename:``,url:I,enforceDownloadInNewTab:N}).click())},[u,w,v,_,b,D,L,I,N]);l({shortcut:O,disabled:u,onActivate:R});let{clear:z,restart:B}=e(()=>{M(null)},5e3,{autoStart:!1});return(0,g.useEffect)(()=>{j?B():z()},[z,j,B]),a(`div`,{className:`stDownloadButton`,"data-testid":`stDownloadButton`,children:[n(o,{help:x,containerWidth:!0,children:n(c,{kind:F,size:d.SMALL,disabled:u||k,onClick:R,containerWidth:!0,children:n(f,{icon:k?`spinner`:C,iconPosition:h(_.iconPosition),label:S,shortcut:O})})}),j&&n(t,{"data-testid":`stDownloadButtonError`,children:j})]})}var v=(0,g.memo)(_);export{v as default};