import{A as e,D as t,Q as n,Tt as r,Z as i,et as a,o}from"./vue-vendor-DMUmTARL.js";import{I as s,n as c,s as l,v as u,vt as d,yt as f}from"./index-BcZTP2Ia.js";import{n as ee}from"./file-data-uFMtP6Ff.js";import{t as p}from"./useFilePwd-ydEI5tjE.js";import{t as m}from"./useShareData-CW-8lVh5.js";import{t as h}from"./pro-capability-BR-p0W7Z.js";import{t as g}from"./useCapability-CAwTuoTM.js";var _=e=>c({url:`/onlyOffice/config/token`,method:`post`,data:e}),v=e=>c({url:`/api/share/onlyOffice/config/token`,method:`post`,data:e}),y=e=>c({url:`/onlyOffice/history/list`,method:`post`,data:e}),b=e=>c({url:`/onlyOffice/history/data`,method:`post`,data:e}),x=e=>c({url:`/onlyOffice/history/restore`,method:`post`,data:e}),S={class:`relative h-full w-full`},C={__name:`OfficeViewer`,props:{fileName:{type:String,default:``}},setup(c){let C=u(),{isCapabilityEnabled:w}=g(),{getPathPwd:T}=p(),{sharePassword:E,getShareFolderPassword:D}=m(),O=f(),k=ee(),A=o(),{storageKey:j,currentPath:M}=d(),N=c,P=null,F=null,I=0,L=0,R=!1,z=r(null),B=!1,V=()=>{let e=k.currentClickRow?.path;return s(e||M.value,N.fileName)},H=()=>{let e=V(),t=W(e),n=U(A.params?.shareKey,``);if(n)return{shareKey:n,path:e,sharePassword:E.value||U(A.query?.pwd,``),folderPassword:D(t)};let r=j.value;return r?{storageKey:r,path:e,password:T(t)}:(console.warn(`[office-viewer] 缺少 storageKey，无法请求 OnlyOffice token`,{fileName:N.fileName,routePath:A.fullPath,shareKey:A.params?.shareKey}),null)},U=(e,t=``)=>Array.isArray(e)?e.length>0?String(e[0]||t):t:e==null?t:String(e),W=e=>{let t=(e||`/`).split(`/`).filter(Boolean);t.pop();let n=`/`+t.join(`/`);return n===`//`?`/`:n||`/`},G=e=>e.shareKey?v(e):_(e),K=(e={})=>{let t=F||{};return{storageKey:t.storageKey,path:t.path,password:t.password,...e}},q=()=>{F?.shareKey||y(K()).then(e=>{P?.refreshHistory(e.data),z.value=e.data?.currentVersion??null}).catch(e=>{console.error(`[office-viewer] 获取 OnlyOffice 历史列表失败`,e),C.error(`加载历史列表失败`),P?.refreshHistory({error:`加载历史列表失败`})})},J=async e=>{if(F?.shareKey)return;let t=e?.data,n=++I;try{let e=await b(K({version:t}));if(n!==I)return;P?.setHistoryData(e.data)}catch(e){if(n!==I)return;console.error(`[office-viewer] 获取 OnlyOffice 历史版本数据失败`,e),P?.setHistoryData({error:`加载历史版本失败`}),C.error(`加载历史版本失败`)}},Y=()=>{$()},X=e=>{e.events={...e.events||{},...w(h.onlyofficeHistory)?{onRequestHistory:q,onRequestHistoryData:J,onRequestHistoryClose:Y,onRequestRestore:te}:{}},P=new DocsAPI.DocEditor(`office-body`,e)},Z=()=>{if(P){if(P.destroyEditor)P.destroyEditor();else{let e=document.getElementById(`office-body`);e&&(e.innerHTML=``),console.warn(`[OnlyOffice] docEditor.destroyEditor 不可用，已清空容器兜底`)}P=null}},Q=e=>{F=e;let t=++L;return G(e).then(e=>{R||t!==L||(Z(),X(e.data))}).catch(n=>{R||t!==L||(console.error(`[office-viewer] 获取 OnlyOffice token 失败`,{fileInfo:e,error:n}),C.error(`获取 Office 预览配置失败，请稍后重试`))})},$=()=>{let e=H();if(!e){C.warning(`未获取到存储源信息，无法重新加载 Office 预览`);return}Q(e)},te=async e=>{if(F?.shareKey||B)return;let t=e?.data?.version;if(t!=null){try{await l.confirm(`确认恢复到版本 v${t}？将基于该版本生成一个新版本覆盖当前线上文档，正在协同编辑的用户会被重连，且此操作不可撤销。`,`恢复历史版本`,{confirmButtonText:`确认恢复`,cancelButtonText:`取消`,type:`warning`})}catch{return}B=!0;try{await x(K({version:t,currentVersion:z.value??void 0})),C.success(`已恢复`),$()}catch(e){console.error(`[office-viewer] 恢复 OnlyOffice 历史版本失败`,e),C.error(`恢复历史版本失败，请稍后重试`)}finally{B=!1}}};i(()=>{ne(s(O.globalConfig.onlyOfficeUrl,`web-apps/apps/api/documents/api.js`),()=>{let e=H();if(!e){C.warning(`未获取到存储源信息，无法进行 Office 预览`);return}Q(e)})}),n(()=>{R=!0,Z()});function ne(e,t){let n=document.createElement(`script`),r=document.getElementsByTagName(`head`)[0];n.type=`text/javascript`,n.charset=`UTF-8`,n.src=e,n.addEventListener?(n.addEventListener(`load`,function(){t()},!1),n.addEventListener(`error`,function(){C.warning(`调用在线文档服务失败，请检查在线文档服务是否正常`)},!1)):n.attachEvent&&n.attachEvent(`onreadystatechange`,function(){window.event.srcElement.readyState===`loaded`&&t()}),r.appendChild(n)}return(n,r)=>(a(),e(`div`,S,[...r[0]||=[t(`div`,{id:`office-body`,class:`h-full w-full`},null,-1)]]))}};export{C as default};