import{o as e}from"./rolldown-runtime.DAXXjFlN.js";import{r as t}from"./emotion-is-prop-valid.esm.CygBv61C.js";import{a as n,i as r}from"./emotion-styled.browser.esm.C7ab1lZK.js";import{i}from"./UriUtil.Baw2_TkK.js";import{n as a,t as o}from"./styled-components.BQZommyC.js";import{t as s}from"./BaseButton.CD99a2NM.js";import{t as c}from"./useTimeout.CUauXJkz.js";import{t as l}from"./BaseButtonTooltip.9ZCtkLEj.js";import{i as u,t as d}from"./DynamicButtonLabel.BZWdvyzO.js";import{t as f}from"./BackendOperationContext.DtKBGW8l.js";import{n as p}from"./styled-components.DHhUOqJd.js";import{t as m}from"./createDownloadLinkElement.DlKYa9YF.js";import{t as h}from"./iconPosition.C4GDw890.js";var g=e(t(),1);function _(e){let{disabled:t,element:_,widgetMgr:v,endpoints:y,fragmentId:b}=e,{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)(i),{backendOperationClient:P}=(0,g.useContext)(f),F=o.SECONDARY;T===`primary`?F=o.PRIMARY:T===`tertiary`&&(F=o.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){let t=e instanceof Error?e.message:`Download failed`;M(t)}finally{A(!1)}},[P,D,y,N]),R=(0,g.useCallback)(()=>{t||(w||v.setTriggerValue(_,{fromUi:!0},b),D?.length?L():m({filename:``,url:I,enforceDownloadInNewTab:N}).click())},[t,w,v,_,b,D,L,I,N]);u({shortcut:O,disabled:t,onActivate:R});let{clear:z,restart:B}=c(()=>{M(null)},5e3,{autoStart:!1});return(0,g.useEffect)(()=>{j?B():z()},[z,j,B]),n(`div`,{className:`stDownloadButton`,"data-testid":`stDownloadButton`,children:[r(l,{help:x,containerWidth:!0,children:r(s,{kind:F,size:a.SMALL,disabled:t||k,onClick:R,containerWidth:!0,children:r(d,{icon:k?`spinner`:C,iconPosition:h(_.iconPosition),label:S,shortcut:O})})}),j&&r(p,{"data-testid":`stDownloadButtonError`,children:j})]})}var v=(0,g.memo)(_);export{v as default};