import{r as e,j as t}from"./vendor-react-Dp9pL_Wn.js";import{bb as n,bc as s}from"./c6eedc03a-CsEFTfjy.js";import{M as i}from"./vendor-intl-D2wGIp4M.js";import{bl as r,bm as a,bn as o,bo as c,bp as l,bq as u}from"./index-CBEPphRm.js";import{g as d,B as f}from"./vendor-ant-cds-bBedrtIN.js";const h=/\.(html?|svg|pdf|docx?|pptx?|xlsx?)$/i,p=/\.(docx?|pptx?|xlsx?)$/i,x=/\.(pdf|docx?|pptx?|xlsx?)$/i,m=-1e4,v={x:m,y:0,width:1,height:1};function w(e){return h.test(e)}function g(e){return p.test(e)}const b={};function j(e){delete b[e]}function y(t,n){const[s,i]=e.useState(b[t]);return e.useEffect(()=>{void 0!==b[t]&&i(b[t]);let e=!1;return Promise.resolve(n()??!1).then(n=>{b[t]=n,e||i(n)},()=>{e||i(b[t]??!1)}),()=>{e=!0}},[]),s}function R(){return y("preview",()=>s?.isEnabled?.())}function M(){return y("epitaxy",()=>s?.isEpitaxyPreviewEnabled?.())}function k(){return y("open",()=>s?.isOpenInDefaultAppEnabled?.())}function C({sessionId:i,filePath:d,cacheBuster:f,noCloudFallback:h,show:x,onFallback:w,onOpenFile:g,onRendered:b}){const y=e.useId(),R=e.useRef(null),[M,k]=e.useState(null),C=e.useCallback(e=>{R.current=e,k(e)},[]),[F,P]=e.useState(!0),[_,I]=e.useState(null),[N,O]=e.useState(0);r(M),a(),o();const z=n(e=>e.overflowMenuOpen),B=c(),S=z||B,A=l(),$=e.useRef(S||A),q=e.useRef(null),L=e.useRef(h);e.useEffect(()=>{L.current=h},[h]),e.useEffect(()=>{const e=R.current,{show:t,hide:n,isVmReady:r}=s??{},a=x??(t?(e,n,s,i)=>t(e,encodeURIComponent(n.replace(/^computer:\/\//,"")),s,i):void 0),o=e=>{L.current?(n?.(y),I(e)):w?.()};if(!e||!a)return void o("ipc_unavailable");P(!0),I(null);const c=performance.now();let l=!1,u=!1,f=!0;const h=t=>{if(l||u)return;const n=f;f=!1;const s=e.getBoundingClientRect(),r=t??($.current?v:{x:s.x,y:s.y,width:s.width,height:s.height});a(i,d,r,y).then(e=>{if(l)return;const{ok:t,declineReason:s}="boolean"==typeof e?{ok:e}:e;n&&(P(!1),b?.({ok:t,durationMs:performance.now()-c,reason:t?void 0:"declined",declineReason:s})),t||("gate_disabled"===s&&j(x?"epitaxy":"preview"),u=!0,o(s??"declined"))}).catch(()=>{l||(n&&b?.({ok:!1,durationMs:performance.now()-c,reason:"ipc_error"}),P(!1),u=!0,o("ipc_error"))})},m=()=>h();let g;const M=()=>{q.current=h,h(),g=new ResizeObserver(m),g.observe(e),window.addEventListener("resize",m)};return r&&p.test(d)?r().then(e=>{if(!l){if(e)return M();P(!1),b?.({ok:!1,durationMs:performance.now()-c,reason:"declined",declineReason:"vm_unavailable"}),o("vm_unavailable")}}).catch(()=>{l||M()}):M(),()=>{l=!0,q.current=null,g?.disconnect(),window.removeEventListener("resize",m),n?.(y)}},[i,d,f,N]);const[V,D]=e.useState(null),K=e.useRef(A);e.useEffect(()=>{const e=K.current;K.current=A;const t=$.current;if($.current=S||A,!S&&!A)return void q.current?.();const{parkAndCapture:n}=s??{},r=R.current;if(A||!n||!r)return void q.current?.();const a=r.getBoundingClientRect();(e||t)&&q.current?.({x:m,y:0,width:a.width,height:a.height});let o=!1;const c=e||t?v:{x:a.x,y:a.y,width:a.width,height:a.height},l=()=>{o||q.current?.()};return n(c,y).then(async e=>{if(o)return;if(!e)return void l();const t=`data:image/png;base64,${e}`,n=new Image;n.src=t,await n.decode().catch(()=>{}),o||n.naturalWidth<=1||n.naturalHeight<=1?l():(D({sessionId:i,filePath:d,cacheBuster:f,src:t,width:a.width,height:a.height}),requestAnimationFrame(()=>{requestAnimationFrame(l)}))}).catch(l),()=>{o=!0}},[S,A]);const T=S&&!A&&V?.sessionId===i&&V.filePath===d&&V.cacheBuster===f?V:null;return t.jsxs("div",{ref:C,className:"h-full w-full relative overflow-hidden",children:[null!==_?t.jsx(E,{declineReason:_,onRetry:()=>O(e=>e+1),onOpenFile:g}):F?t.jsx(u,{className:"absolute inset-0 flex items-center justify-center"}):null,T&&t.jsx("img",{src:T.src,alt:"",className:"pointer-events-none select-none",draggable:!1,style:{width:T.width,height:T.height}})]})}function E({declineReason:e,onRetry:n,onOpenFile:s}){const r="vm_unavailable"===e?t.jsx(i,{defaultMessage:"The Cowork VM isn\u2019t connected. It may still be starting up.",id:"+bt9+eulkK"}):"soffice_failed"===e?t.jsx(i,{defaultMessage:"Couldn\u2019t convert this file for preview.",id:"BCwiPnM3dV"}):t.jsx(i,{defaultMessage:"Couldn\u2019t preview this file.",id:"RXDopwcr5x"});return t.jsxs("div",{className:"flex h-full w-full flex-col items-center justify-center gap-2 p-4 text-center","data-testid":"native-file-preview-error","data-decline-reason":e,children:[t.jsx("div",{className:"text-sm font-medium text-text-200",children:t.jsx(i,{defaultMessage:"Preview unavailable",id:"Pa/ScM/gPC"})}),t.jsx("div",{className:"text-xs text-text-400",children:r}),t.jsxs(d,{className:"mt-1 flex gap-2",children:["ipc_unavailable"!==e&&t.jsx(f,{variant:"secondary",size:"sm",onClick:n,children:t.jsx(i,{defaultMessage:"Try again",id:"FazwRldA7z"})}),s&&t.jsx(f,{variant:"secondary",size:"sm",onClick:s,children:t.jsx(i,{defaultMessage:"Open file",id:"LsFoKe0byQ"})})]})]})}export{C as N,x as O,g as a,k as b,j as c,M as d,w as i,R as u};
