import{r as e,j as t}from"./vendor-react-DcxrbCFs.js";import{b9 as n,ba as s,bb as r}from"./c4b350ac1-BTR_0NaM.js";import{M as i}from"./vendor-intl-C0z7gCkr.js";import{al as a,am as o,an as c,ao as l,ap as u}from"./index-BSvCx8kl.js";import{S as d}from"./c0632795f-CL3ffsDQ.js";import{k as f,B as h}from"./vendor-ant-cds-BOj-tX6j.js";const p=/\.(html?|svg|pdf|docx?|pptx?|xlsx?)$/i,x=/\.(docx?|pptx?|xlsx?)$/i,m=/\.(pdf|docx?|pptx?|xlsx?)$/i,v=-1e4,w={x:v,y:0,width:1,height:1};function g(e){return p.test(e)}function b(e){return x.test(e)}const j={};function y(e){delete j[e]}function R(t,n){const[s,r]=e.useState(j[t]);return e.useEffect(()=>{void 0!==j[t]&&r(j[t]);let e=!1;return Promise.resolve(n()??!1).then(n=>{j[t]=n,e||r(n)},()=>{e||r(j[t]??!1)}),()=>{e=!0}},[]),s}function k(){return R("preview",()=>s?.isEnabled?.())}function M(){return R("epitaxy",()=>s?.isEpitaxyPreviewEnabled?.())}function C(){return R("open",()=>s?.isOpenInDefaultAppEnabled?.())}function E({sessionId:i,filePath:f,cacheBuster:h,noCloudFallback:p,show:m,onFallback:g,onOpenFile:b,onRendered:j}){const R=e.useId(),k=e.useRef(null),[M,C]=e.useState(null),E=e.useCallback(e=>{k.current=e,C(e)},[]),[O,_]=e.useState(!0),[z,I]=e.useState(null),[N,P]=e.useState(0);a(M),o(),c();const S=n(e=>e.overflowMenuOpen),B=l(),A=S||B,L=u(),$=e.useRef(A||L),q=e.useRef(null),T=e.useRef(p);e.useEffect(()=>{T.current=p},[p]),e.useEffect(()=>{const e=k.current,{show:t,hide:n,isVmReady:a}=s??{},o=m??(t?(e,n,s,r)=>t(e,encodeURIComponent(n.replace(/^computer:\/\//,"")),s,r):void 0),c=e=>{T.current&&r?(n?.(R),I(e)):g?.()};if(!e||!o)return void c("ipc_unavailable");_(!0),I(null);const l=performance.now();let u=!1,d=!1,h=!0;const p=t=>{if(u||d)return;const n=h;h=!1;const s=e.getBoundingClientRect(),r=t??($.current?w:{x:s.x,y:s.y,width:s.width,height:s.height});o(i,f,r,R).then(e=>{if(u)return;const{ok:t,declineReason:s}="boolean"==typeof e?{ok:e}:e;n&&(_(!1),j?.({ok:t,durationMs:performance.now()-l,reason:t?void 0:"declined",declineReason:s})),t||("gate_disabled"===s&&y(m?"epitaxy":"preview"),d=!0,c(s??"declined"))}).catch(()=>{u||(n&&j?.({ok:!1,durationMs:performance.now()-l,reason:"ipc_error"}),_(!1),d=!0,c("ipc_error"))})},v=()=>p();let b;const M=()=>{q.current=p,p(),b=new ResizeObserver(v),b.observe(e),window.addEventListener("resize",v)};return a&&x.test(f)?a().then(e=>{if(!u){if(e)return M();_(!1),j?.({ok:!1,durationMs:performance.now()-l,reason:"declined",declineReason:"vm_unavailable"}),c("vm_unavailable")}}).catch(()=>{u||M()}):M(),()=>{u=!0,q.current=null,b?.disconnect(),window.removeEventListener("resize",v),n?.(R)}},[i,f,h,N]);const[U,V]=e.useState(null),W=e.useRef(L);e.useEffect(()=>{const e=W.current;W.current=L;const t=$.current;if($.current=A||L,!A&&!L)return void q.current?.();const{parkAndCapture:n}=s??{},r=k.current;if(L||!n||!r)return void q.current?.();const a=r.getBoundingClientRect();(e||t)&&q.current?.({x:v,y:0,width:a.width,height:a.height});let o=!1;const c=e||t?w:{x:a.x,y:a.y,width:a.width,height:a.height},l=()=>{o||q.current?.()};return n(c,R).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():(V({sessionId:i,filePath:f,cacheBuster:h,src:t,width:a.width,height:a.height}),requestAnimationFrame(()=>{requestAnimationFrame(l)}))}).catch(l),()=>{o=!0}},[A,L]);const D=A&&!L&&U?.sessionId===i&&U.filePath===f&&U.cacheBuster===h?U:null;return t.jsxs("div",{ref:E,className:"h-full w-full relative overflow-hidden",children:[null!==z?t.jsx(F,{declineReason:z,onRetry:()=>P(e=>e+1),onOpenFile:b}):O?t.jsx(d,{className:"absolute inset-0 flex items-center justify-center"}):null,D&&t.jsx("img",{src:D.src,alt:"",className:"pointer-events-none select-none",draggable:!1,style:{width:D.width,height:D.height}})]})}function F({declineReason:e,onRetry:n,onOpenFile:s}){const r="vm_unavailable"===e?t.jsx(i,{defaultMessage:"The Cowork VM isn't connected. It may still be starting up.",id:"MJuFWFhk/n"}):"soffice_failed"===e?t.jsx(i,{defaultMessage:"Couldn't convert this file for preview.",id:"CEzX5fe6ri"}):t.jsx(i,{defaultMessage:"Couldn't preview this file.",id:"mE32oUZt/O"});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(f,{className:"mt-1 flex gap-2",children:["ipc_unavailable"!==e&&t.jsx(h,{variant:"secondary",size:"sm",onClick:n,children:t.jsx(i,{defaultMessage:"Try again",id:"FazwRldA7z"})}),s&&t.jsx(h,{variant:"secondary",size:"sm",onClick:s,children:t.jsx(i,{defaultMessage:"Open file",id:"LsFoKe0byQ"})})]})]})}export{E as N,m as O,b as a,C as b,y as c,M as d,g as i,k as u};
