PlotDirector/PlotLine/wwwroot/js/word-companion-host.min.js
2026-06-27 20:52:02 +01:00

1 line
66 KiB
JavaScript

(()=>{const e="plotdirector.word.projectId",t="plotdirector.word.bookId",r="plotdirector.word.activeTab",n="plotdirector.word.documentGuid",o="plotdirector.word.boundBookId",a="plotdirector.word.boundProjectId",c="plotdirector.word.bindingVersion",i=new Set(["scene","links","structure","diagnostics"]),s=document.querySelector(".word-companion-shell"),d=document.querySelector(".word-companion-tabs"),l=[...document.querySelectorAll("[data-tab-button]")],u=[...document.querySelectorAll("[data-tab-panel]")],p=document.querySelector("[data-first-run-wizard]"),h=document.querySelector("[data-first-run-project-select]"),m=document.querySelector("[data-first-run-book-select]"),y=document.querySelector("[data-first-run-new-book-title]"),f=document.querySelector("[data-first-run-create-book]"),w=document.querySelector("[data-first-run-scan]"),S=document.querySelector("[data-first-run-cancel]"),g=document.querySelector("[data-first-run-status]"),C=document.querySelector("[data-first-run-summary]"),b=document.querySelector("[data-first-run-character-section]"),I=document.querySelector("[data-first-run-character-status]"),k=document.querySelector("[data-first-run-character-candidates]"),v=document.querySelector("[data-first-run-character-actions]"),E=document.querySelector("[data-first-run-create-characters]"),L=document.querySelector("[data-first-run-skip-characters]"),N=document.querySelector("[data-first-run-import-actions]"),q=document.querySelector("[data-first-run-import]"),x=document.querySelector("[data-first-run-import-cancel]"),P=document.querySelector("[data-first-run-replace-dialog]"),D=document.querySelector("[data-first-run-replace-confirm]"),A=document.querySelector("[data-first-run-replace-cancel]"),T=document.querySelector("[data-office-status]"),$=document.querySelector("[data-connection-status]"),j=document.querySelector("[data-summary-connection]"),R=document.querySelector("[data-summary-project]"),W=document.querySelector("[data-summary-book]"),U=document.querySelector("[data-project-select]"),M=document.querySelector("[data-book-select]"),O=document.querySelector("[data-project-message]"),H=document.querySelector("[data-book-message]"),B=document.querySelector("[data-refresh-current-scene]"),F=document.querySelector("[data-refresh-document]"),G=document.querySelector("[data-current-chapter]"),V=document.querySelector("[data-current-scene]"),J=document.querySelector("[data-current-word-count]"),Y=document.querySelector("[data-scene-tracking-status]"),z=document.querySelector("[data-document-message]"),K=document.querySelector("[data-sync-note]"),Q=document.querySelector("[data-document-outline]"),X=document.querySelector("[data-analyse-manuscript-structure]"),Z=document.querySelector("[data-apply-structure-sync]"),_=document.querySelector("[data-structure-preview-status]"),ee=document.querySelector("[data-structure-preview-results]"),te=document.querySelector("[data-refresh-plotdirector-scene]"),re=document.querySelector("[data-plotdirector-scene-status]"),ne=document.querySelector("[data-anchor-status]"),oe=document.querySelector("[data-anchor-book-id]"),ae=document.querySelector("[data-anchor-chapter-id]"),ce=document.querySelector("[data-anchor-scene-id]"),ie=document.querySelector("[data-attach-plotdirector-ids]"),se=document.querySelector("[data-plotdirector-scene-details]"),de=document.querySelector("[data-plotdirector-scene-title]"),le=document.querySelector("[data-plotdirector-revision-status]"),ue=document.querySelector("[data-plotdirector-estimated-words]"),pe=document.querySelector("[data-plotdirector-actual-words]"),he=document.querySelector("[data-plotdirector-blocked-status]"),me=document.querySelector("[data-plotdirector-blocked-reason-row]"),ye=document.querySelector("[data-plotdirector-blocked-reason]"),fe=document.querySelector("[data-sync-scene-progress]"),we=document.querySelector("[data-sync-word-count]"),Se=document.querySelector("[data-sync-plotdirector-count]"),ge=document.querySelector("[data-sync-last-synced]"),Ce=document.querySelector("[data-sync-status]"),be=document.querySelector("[data-writing-brief-block]"),Ie=document.querySelector("[data-writing-brief]"),ke=document.querySelector("[data-plotdirector-link-groups]"),ve=document.querySelector("[data-character-chips]"),Ee=document.querySelector("[data-asset-chips]"),Le=(document.querySelector("[data-location-chips]"),document.querySelector("[data-primary-location-select]")),Ne=document.querySelector("[data-save-scene-links]"),qe=document.querySelector("[data-scene-links-status]"),xe=document.querySelector("[data-links-editing-scene]"),Pe=document.querySelector("[data-chapter-create-link]"),De=document.querySelector("[data-chapter-create-link-chapter]"),Ae=document.querySelector("[data-create-plotdirector-chapter]"),Te=document.querySelector("[data-link-existing-chapter]"),$e=document.querySelector("[data-chapter-create-link-status]"),je=document.querySelector("[data-scene-create-link]"),Re=document.querySelector("[data-create-link-chapter]"),We=document.querySelector("[data-create-link-scene]"),Ue=document.querySelector("[data-create-plotdirector-scene]"),Me=document.querySelector("[data-link-existing-scene]"),Oe=document.querySelector("[data-create-link-status]"),He=document.querySelector("[data-link-existing-dialog]"),Be=document.querySelector("[data-link-scene-search]"),Fe=document.querySelector("[data-link-scene-options]"),Ge=document.querySelector("[data-link-selected-scene]"),Ve=document.querySelector("[data-link-dialog-cancel]"),Je=document.querySelector("[data-link-dialog-status]"),Ye=document.querySelector("[data-create-scene-dialog]"),ze=document.querySelector("[data-create-dialog-scene]"),Ke=document.querySelector("[data-create-dialog-chapter]"),Qe=document.querySelector("[data-create-dialog-confirm]"),Xe=document.querySelector("[data-create-dialog-cancel]"),Ze=document.querySelector("[data-link-existing-chapter-dialog]"),_e=document.querySelector("[data-link-chapter-search]"),et=document.querySelector("[data-link-chapter-options]"),tt=document.querySelector("[data-link-selected-chapter]"),rt=document.querySelector("[data-link-chapter-dialog-cancel]"),nt=document.querySelector("[data-link-chapter-dialog-status]"),ot=document.querySelector("[data-create-chapter-dialog]"),at=document.querySelector("[data-create-chapter-dialog-chapter]"),ct=document.querySelector("[data-create-chapter-dialog-book]"),it=document.querySelector("[data-create-chapter-dialog-confirm]"),st=document.querySelector("[data-create-chapter-dialog-cancel]"),dt=document.querySelector("[data-apply-structure-sync-dialog]"),lt=document.querySelector("[data-apply-structure-sync-summary]"),ut=document.querySelector("[data-apply-structure-sync-confirm]"),pt=document.querySelector("[data-apply-structure-sync-cancel]"),ht=document.querySelector("[data-resolve-project-id]"),mt=document.querySelector("[data-resolve-project-title]"),yt=document.querySelector("[data-resolve-book-id]"),ft=document.querySelector("[data-resolve-book-title]"),wt=document.querySelector("[data-resolve-detected-chapter]"),St=document.querySelector("[data-resolve-detected-scene]"),gt=document.querySelector("[data-resolve-request-chapter]"),Ct=document.querySelector("[data-resolve-request-scene]"),bt=document.querySelector("[data-resolve-result]"),It=document.querySelector("[data-resolve-chapter-id]"),kt=document.querySelector("[data-resolve-scene-id]"),vt=document.querySelector("[data-run-diagnostics]"),Et=document.querySelector("[data-diagnostic-host]"),Lt=document.querySelector("[data-diagnostic-platform]"),Nt=document.querySelector("[data-diagnostic-ready]"),qt=document.querySelector("[data-diagnostic-word-api]"),xt=document.querySelector("[data-diagnostic-last-scan]"),Pt=document.querySelector("[data-diagnostic-last-error]"),Dt=document.querySelector("[data-diagnostic-anchor-type]"),At=document.querySelector("[data-diagnostic-stored-scene-id]"),Tt=document.querySelector("[data-diagnostic-stored-chapter-id]"),$t=document.querySelector("[data-diagnostic-resolution-method]"),jt=document.querySelector("[data-diagnostic-paragraphs]"),Rt=document.querySelector("[data-diagnostic-heading1]"),Wt=document.querySelector("[data-diagnostic-heading2]"),Ut="true"===s?.dataset.authenticated;let Mt=[],Ot=[],Ht=!1,Bt=!1,Ft="Unknown",Gt="Unknown",Vt="",Jt="",Yt=null,zt=null,Kt=null,Qt=null,Xt=null,Zt=null,_t="",er="Title Match",tr=!1,rr=[],nr=null,or=null,ar=[],cr=null,ir=null,sr=null,dr=null,lr=null,ur=null,pr=[],hr=!1,mr=!1,yr=!1,fr=!1,wr=null,Sr="Manual",gr=!1,Cr=null,br=!1,Ir=!1,kr="",vr="",Er={characters:[],assets:[],locations:[],primaryLocationId:null};const Lr=e=>{T&&(T.textContent=e)},Nr=(e,t=!0)=>{const n=i.has(e)&&l.some(t=>t.dataset.tabButton===e)?e:"scene";for(const e of l){const t=e.dataset.tabButton===n;e.classList.toggle("is-active",t),e.setAttribute("aria-selected",t?"true":"false")}for(const e of u){const t=e.dataset.tabPanel===n;e.classList.toggle("is-active",t),e.hidden=!t}t&&((e,t)=>{try{t?window.localStorage.setItem(e,String(t)):window.localStorage.removeItem(e)}catch{}})(r,n)},qr=()=>{const e=(e=>{try{return window.localStorage.getItem(e)||""}catch{return""}})(r);Nr(i.has(e)?e:"scene",!1)},xr=e=>{$&&($.textContent=e),j&&(j.textContent=e)},Pr=e=>{O&&(O.textContent=e)},Dr=e=>{H&&(H.textContent=e)},Ar=e=>{z&&(z.textContent=e)},Tr=e=>{re&&(re.textContent=e)},$r=e=>{const t=t=>Object.prototype.hasOwnProperty.call(e,t);t("anchorType")&&gn(Dt,Qr(e.anchorType)),t("storedSceneId")&&gn(At,Qr(e.storedSceneId)),t("storedChapterId")&&gn(Tt,Qr(e.storedChapterId)),t("resolutionMethod")&&gn($t,Qr(e.resolutionMethod))},jr=()=>{const e=xn(),t=Number.isInteger(Xt)&&Xt>0&&Qt===Xt,r=!Number.isInteger(Zt)||Zt<=0||Kt===Zt,n=t&&r;gn(ne,n?"Attached to PlotDirector":"Not attached"),gn(oe,e?.bookId?String(e.bookId):"-"),gn(ae,Number.isInteger(Zt)&&Zt>0?String(Zt):"-"),gn(ce,Number.isInteger(Xt)&&Xt>0?String(Xt):"-"),ie&&(ie.disabled=!Xt||n&&!tr,ie.textContent=Qt&&!n?"Reattach PlotDirector IDs":"Attach PlotDirector IDs"),$r({anchorType:er||"Title Match",storedSceneId:Qt,storedChapterId:Kt,resolutionMethod:_t})},Rr=(e,t,r,n=null,o=null,a=null)=>{Vt=e||"",Jt=t||"",Yt=Number.isInteger(r)?r:null,zt=Number.isInteger(a)&&a>0?a:null,Kt=Number.isInteger(n)&&n>0?n:null,Qt=Number.isInteger(o)&&o>0?o:null,G&&(G.textContent=e||"Not detected"),V&&(V.textContent=t||"Not detected"),J&&(J.textContent=Number.isInteger(r)?String(r):"-"),gn(we,Number.isInteger(r)?String(r):"-"),jr()},Wr=(e,t)=>{e&&(e.hidden=t)},Ur=e=>{gn(Ce,e)},Mr=e=>{gn(_,e)},Or=(e="Select a project and book to analyse manuscript structure.")=>{if(sr=null,ee){ee.innerHTML="";const e=document.createElement("p");e.textContent="No preview generated.",ee.append(e)}Mr(e),Br()},Hr=(e=sr)=>((e=sr)=>zn.flatMap(t=>Array.isArray(e?.[t.key])?e[t.key]:[]))(e).filter(e=>"couldLink"===e.category||"wouldCreateChapters"===e.category||"wouldCreateScenes"===e.category),Br=()=>{Z&&(Z.disabled=fr||0===Hr().length)},Fr=()=>{X&&(X.disabled=fr||!qn()||!xn()),Br()},Gr=()=>{xe&&(xe.textContent=Xt?`Editing links for: ${kr||Jt||"Current scene"}`:"Link a PlotDirector scene first.")},Vr=()=>{const e=gr||!Xt;fe&&(fe.disabled=e),Ne&&(Ne.disabled=e),Le&&(Le.disabled=e);for(const t of[ve,Ee])for(const r of t?[...t.querySelectorAll("input[type='checkbox']")]:[])r.disabled=e},Jr=e=>{"Live"!==e&&"Manual"!==e||(Sr=e),gn(Y,e||Sr||"Manual")},Yr=(e,t="")=>{gr=!!e,gn(Y,gr?"Stale":Sr),Vr(),t&&(Ar(t),Xr(t))},zr=(e,t=null,r="")=>{Xt=Number.isInteger(e)&&e>0?e:null,Zt=Number.isInteger(t)&&t>0?t:null,_t=r||"",Vr(),Ur(gr?"Refresh Current Scene before syncing.":Xt?"Ready to sync.":"No resolved PlotDirector scene."),Xr(gr?"Refresh Current Scene before saving.":Xt?"Ready to save.":"Link a PlotDirector scene first."),Gr(),jr()},Kr=e=>{B&&(B.disabled=e,B.textContent=e?"Refreshing...":"Refresh Current Scene")},Qr=e=>null==e||""===e?"-":String(e),Xr=e=>{gn(qe,e)},Zr=e=>{gn(Oe,e)},_r=e=>{gn($e,e)},en=()=>!!xn()&&!!Vt&&!Zt,tn=()=>!!xn()&&!!Jt&&Number.isInteger(Zt)&&Zt>0,rn=()=>{Wr(Pe,!0),_r(""),Ae&&(Ae.disabled=!0,Ae.textContent="Create Chapter in PlotDirector"),Te&&(Te.disabled=!0,Te.textContent="Link to Existing Chapter")},nn=(e="")=>{gn(De,Qr(Vt)),Wr(Pe,!1),_r(e);const t=en();Ae&&(Ae.disabled=!t,Ae.textContent="Create Chapter in PlotDirector"),Te&&(Te.disabled=!t,Te.textContent="Link to Existing Chapter")},on=()=>{Wr(je,!0),Zr(""),Ue&&(Ue.disabled=!0,Ue.textContent="Create Scene in PlotDirector"),Me&&(Me.disabled=!0,Me.textContent="Link to Existing Scene")},an=(e="")=>{gn(Re,Qr(Vt)),gn(We,Qr(Jt)),Wr(je,!1),Zr(e);const t=tn();Ue&&(Ue.disabled=!t,Ue.textContent="Create Scene in PlotDirector"),Me&&(Me.disabled=!t,Me.textContent="Link to Existing Scene")},cn=e=>{Tr(e),Wr(se,!0),Wr(be,!0),Wr(ke,!1),rn(),on(),_t="",kr="",vr="",er=Qt?"SceneID Anchor":Kt?"ChapterID Anchor":"Title Match",tr=!1,Er={characters:[],assets:[],locations:[],primaryLocationId:null},dn(ve,[],"character"),dn(Ee,[],"asset"),ln([],null,!1),zr(null),gn(Se,"-"),gn(de,"-"),gn(le,"-"),gn(ue,"-"),gn(pe,"-"),gn(he,"-"),gn(ye,"-"),Wr(me,!0),jr(),Gr()},sn=(e,t)=>"asset"===t?e?.assetId||e?.AssetId||e?.storyAssetId||e?.StoryAssetId||0:"location"===t?e?.locationId||e?.LocationId||0:e?.characterId||e?.CharacterId||0,dn=(e,t,r,n=!1)=>{if(!e)return;e.innerHTML="";const o=Array.isArray(t)?t:[];if(0===o.length){const t=document.createElement("p");return t.className="word-companion-empty-list",t.textContent="None",void e.append(t)}for(const t of o){const o=Number.parseInt(sn(t,r),10);if(!Number.isInteger(o)||o<=0)continue;const a=document.createElement("label");a.className="word-companion-check-item";const c=document.createElement("input");c.type="checkbox",c.value=String(o),c.checked=!!t.linked,c.disabled=!n;const i=document.createElement("span");i.textContent=t.name||"Unnamed",a.append(c,i),e.append(a)}},ln=(e,t,r=!1)=>{if(!Le)return;Le.innerHTML="";const n=document.createElement("option");n.value="",n.textContent="None",Le.append(n);const o=Number.parseInt(t||"",10);for(const t of Array.isArray(e)?e:[]){const e=Number.parseInt(sn(t,"location"),10);if(!Number.isInteger(e)||e<=0)continue;const r=document.createElement("option");r.value=String(e),r.textContent=t.name||"Unnamed",r.selected=e===o,Le.append(r)}Le.disabled=!r},un=e=>String(e||"").trim().replace(/\s+/g," "),pn=(e,t)=>{const r=un(e);if(!r)return"";const n=new RegExp(`^${"scene"===t?"scene":"chapter"}\\s+(?:\\d+|[ivxlcdm]+|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)\\s*(?::|-|\\u2013|\\u2014)\\s*`,"i");return un(r.replace(n,""))||r},hn=e=>{const t=t=>Object.prototype.hasOwnProperty.call(e,t);t("projectId")&&gn(ht,Qr(e.projectId)),t("projectTitle")&&gn(mt,Qr(e.projectTitle)),t("bookId")&&gn(yt,Qr(e.bookId)),t("bookTitle")&&gn(ft,Qr(e.bookTitle)),t("detectedChapter")&&gn(wt,Qr(e.detectedChapter)),t("detectedScene")&&gn(St,Qr(e.detectedScene)),t("requestChapter")&&gn(gt,Qr(e.requestChapter)),t("requestScene")&&gn(Ct,Qr(e.requestScene)),t("result")&&gn(bt,Qr(e.result)),t("chapterId")&&gn(It,Qr(e.chapterId)),t("sceneId")&&gn(kt,Qr(e.sceneId))},mn=async e=>{const t=await fo(`/api/word-companion/books/${e}/structure`);return Array.isArray(t?.chapters)?t.chapters:[]},yn=async e=>{const t=await fo(`/api/word-companion/books/${e}/chapters`);return Array.isArray(t?.chapters)?t.chapters:[]},fn=e=>{if(Number.isInteger(Zt)&&Zt>0){const t=e.find(e=>e.chapterId===Zt);if(t)return t}const t=pn(Vt,"chapter").toLocaleLowerCase();return t&&e.find(e=>pn(e.title,"chapter").toLocaleLowerCase()===t)||null},wn=async()=>{const e=xn();return e?fn(await mn(e.bookId)):null},Sn=async(e,t,r,n,o)=>{const a=await fo(`/api/word-companion/scenes/${t}/companion`);try{a.revisionStatus=await(async(e,t)=>{const r=await fo(`/api/word-companion/books/${e}/structure`),n=Array.isArray(r?.chapters)?r.chapters:[];for(const e of n){const r=(Array.isArray(e.scenes)?e.scenes:[]).find(e=>e.sceneId===t);if(r)return r.revisionStatus||""}return""})(e,t)}catch{a.revisionStatus=""}var c;er=o||"Title Match",tr=!1,Yr(!1),Tr("Linked to PlotDirector"),Ar(""),rn(),on(),zr(t,r,n),c=a,kr=c?.sceneTitle||Jt||"",vr=c?.chapterTitle||Vt||"",gn(de,Qr(c?.sceneTitle)),gn(le,Qr(c?.revisionStatus||c?.revisionStatusName||"Not provided")),gn(ue,Qr(c?.estimatedWords)),gn(pe,Qr(c?.actualWords)),gn(Se,Qr(c?.actualWords)),gn(he,c?.blocked?"Blocked":"Not blocked"),c?.blockedReason?(gn(ye,c.blockedReason),Wr(me,!1)):(gn(ye,"-"),Wr(me,!0)),Ie&&(Ie.textContent=c?.writingBrief||"No writing brief has been added for this scene."),Er={characters:Array.isArray(c?.characters)?c.characters:[],assets:Array.isArray(c?.assets)?c.assets:[],locations:Array.isArray(c?.locations)?c.locations:[],primaryLocationId:c?.primaryLocationId??null},dn(ve,Er.characters,"character",!!Xt&&!gr),dn(Ee,Er.assets,"asset",!!Xt&&!gr),ln(Er.locations,Er.primaryLocationId,!!Xt&&!gr),Gr(),Xr(gr?"Refresh Current Scene before saving.":Xt?"Ready to save.":"No resolved PlotDirector scene."),Wr(se,!1),Wr(be,!1),Wr(ke,!1)},gn=(e,t)=>{e&&(e.textContent=t)},Cn=e=>{const t=t=>Object.prototype.hasOwnProperty.call(e,t);t("host")&&gn(Et,e.host),t("platform")&&gn(Lt,e.platform),t("ready")&&gn(Nt,e.ready),t("wordApi")&&gn(qt,e.wordApi),t("lastScan")&&gn(xt,e.lastScan),t("lastError")&&gn(Pt,e.lastError),t("paragraphs")&&gn(jt,e.paragraphs),t("heading1")&&gn(Rt,e.heading1),t("heading2")&&gn(Wt,e.heading2)},bn=e=>{const t=[];return e?.name&&t.push(e.name),e?.code&&e.code!==e.name&&t.push(e.code),e?.message&&t.push(e.message),e?.debugInfo?.errorLocation&&t.push(`Location: ${e.debugInfo.errorLocation}`),t.length>0?t.join(": "):String(e)},In=async()=>{if(!window.Office||"function"!=typeof window.Office.onReady)return Ht=!1,Bt=!1,Ft="Unavailable",Gt="Unavailable",Cn({host:Ft,platform:Gt,ready:"No",wordApi:"No"}),null;const e=await window.Office.onReady();return Ht=!0,Ft=e?.host||"Unknown",Gt=e?.platform||"Unknown",Bt=!!window.Word&&!!window.Office.HostType&&e?.host===window.Office.HostType.Word,Cn({host:Ft,platform:Gt,ready:"Yes",wordApi:Bt?"Yes":"No"}),e},kn=e=>{try{const t=window.localStorage.getItem(e),r=Number.parseInt(t||"",10);return Number.isInteger(r)&&r>0?r:null}catch{return null}},vn=(e,t)=>{try{t?window.localStorage.setItem(e,String(t)):window.localStorage.removeItem(e)}catch{}},En=(e,t)=>{if(!e)return;e.innerHTML="";const r=document.createElement("option");r.value="",r.textContent=t,e.append(r),e.disabled=!0},Ln=(e,t,r,n,o)=>{if(!e)return;e.innerHTML="";const a=document.createElement("option");a.value="",a.textContent=t,e.append(a);for(const t of r){const r=document.createElement("option");r.value=String(t[n]),r.textContent=t[o],e.append(r)}e.disabled=0===r.length},Nn=e=>{const t=String(e?.title||"").trim(),r=String(e?.subtitle||"").trim();return r?`${t}: ${r}`:t},qn=()=>{const e=Number.parseInt(U?.value||"",10);return Mt.find(t=>t.projectId===e)||null},xn=()=>{const e=Number.parseInt(M?.value||"",10);return Ot.find(t=>t.bookId===e)||null},Pn=()=>{const e=Number.parseInt(h?.value||"",10);return Mt.find(t=>t.projectId===e)||null},Dn=()=>{const e=Number.parseInt(m?.value||"",10);return Ot.find(t=>t.bookId===e)||null},An=e=>gn(g,e),Tn=()=>{const e=Pn(),t=Dn();w&&(w.disabled=!e||!t||mr),f&&(f.disabled=!e||!String(y?.value||"").trim()||mr),q&&(q.disabled=!lr?.canImport||!ur||mr),E&&(E.disabled=yr||0===Wn().length)},$n=e=>{Wr(p,!e),Wr(d,e);for(const t of u)Wr(t,!!e||"scene"!==t.dataset.tabPanel&&!t.classList.contains("is-active"));e||qr()},jn=()=>{if(!h)return;if(0===Mt.length)return void En(h,"No projects available.");Ln(h,"Choose a project",Mt,"projectId","title");const e=Number.parseInt(U?.value||"",10);Number.isInteger(e)&&e>0&&(h.value=String(e))},Rn=(e="Select a Project and Book to begin.")=>{lr=null,ur=null,pr=[],hr=!1,Wr(C,!0),Wr(N,!0),Wr(b,!0),Wr(v,!0),C&&(C.innerHTML=""),k&&(k.innerHTML=""),gn(I,""),An(e),Tn()},Wn=()=>k?[...k.querySelectorAll("input[type='checkbox']:checked")].map(e=>String(e.value||"").trim()).filter(Boolean):[],Un=e=>{if(pr=Array.isArray(e)?e:[],b&&k){if(k.innerHTML="",Wr(b,!1),Wr(v,!hr||0===pr.length),0===pr.length)return gn(I,"No likely major characters found."),void Tn();gn(I,`${pr.length} likely major characters found.`);for(const e of pr){const t=document.createElement("label");t.className="word-companion-character-candidate";const r=document.createElement("input");r.type="checkbox",r.value=e.text||"",r.checked="high"===String(e.confidence||"").trim().toLowerCase(),r.addEventListener("change",Tn);const n=document.createElement("strong");n.textContent=e.text||"";const o=document.createElement("em"),a=String(e.reason||"").trim();o.textContent=a?`${Number(e.mentionCount||0).toLocaleString()} mentions · ${a}`:`${Number(e.mentionCount||0).toLocaleString()} mentions`;const c=document.createElement("span");c.className="word-companion-character-candidate-text",c.append(n,o),t.append(r,c),k.append(t)}Tn()}},Mn=()=>dr?.documentGuid?dr.documentGuid:window.crypto?.randomUUID?window.crypto.randomUUID():"10000000-1000-4000-8000-100000000000".replace(/[018]/g,e=>(Number(e)^16*Math.random()>>Number(e)/4).toString(16)),On=()=>{const e=qn(),t=xn();R&&(R.textContent=e?.title||"(Not connected)"),W&&(W.textContent=t?Nn(t):"(Not connected)"),K&&(K.textContent=t?"":"Select a PlotDirector book before refreshing."),Fr()},Hn=e=>String(e?.styleBuiltIn||e?.style||"").replace(/\s+/g,"").toLowerCase(),Bn=(e,t)=>{const r=Hn(e);return r===`heading${t}`||r.endsWith(`.heading${t}`)||r.includes(`heading${t}`)},Fn=e=>String(e||"").trim().split(/\s+/).filter(Boolean).length,Gn=(e,t)=>{const r=String(e||"").match(new RegExp(`^${t}-(\\d+)$`,"i"));if(!r)return null;const n=Number.parseInt(r[1],10);return Number.isInteger(n)&&n>0?n:null},Vn=(e,t)=>{const r=Array.isArray(e?.contentControls?.items)?e.contentControls.items:[];for(const e of r){const r=Gn(e.tag,t);if(r)return r}return null},Jn=(e,t)=>{const r=[];let n=0,o=0,a=null,c=null;e.forEach((e,t)=>{const i=String(e.text||"").trim();if(i){if(Bn(e,1))return n+=1,a={index:r.length+1,paragraphIndex:t,title:i,anchorId:Vn(e,"PD-CHAPTER"),scenes:[]},r.push(a),void(c=null);if(Bn(e,2)){if(o+=1,!a)return;return c={index:a.scenes.length+1,paragraphIndex:t,title:i,anchorId:Vn(e,"PD-SCENE"),wordCount:0},void a.scenes.push(c)}c&&(c.wordCount+=Fn(i))}});const i=t.find(e=>String(e.text||"").trim()),s=i?e.findIndex(e=>{return t=e,r=i,String(t?.text||"").trim()===String(r?.text||"").trim()&&Hn(t)===Hn(r);var t,r}):-1,d=s>=0&&r.filter(e=>e.paragraphIndex<=s).at(-1)||null,l=d&&s>=0&&d.scenes.filter(e=>e.paragraphIndex<=s).at(-1)||null;return{chapters:r,currentChapter:d,currentScene:l,paragraphCount:e.length,heading1Count:n,heading2Count:o}},Yn=e=>{if(!Q)return;if(Q.innerHTML="",0===e.chapters.length){const e=document.createElement("p");return e.textContent="No chapters detected. Use Heading 1 for chapter titles.",void Q.append(e)}if(!e.chapters.some(e=>e.scenes.length>0)){const e=document.createElement("p");e.textContent="No scenes detected. Use Heading 2 for scene titles.",Q.append(e)}for(const t of e.chapters){const e=document.createElement("div");e.className="word-companion-outline-chapter";const r=document.createElement("strong");if(r.textContent=t.title,e.append(r),t.scenes.length>0){const r=document.createElement("ul");for(const e of t.scenes){const t=document.createElement("li");t.textContent=e.title,r.append(t)}e.append(r)}Q.append(e)}},zn=[{key:"alreadyLinked",title:"Already Linked"},{key:"couldLink",title:"Could Link"},{key:"wouldCreateChapters",title:"Would Create Chapters"},{key:"wouldCreateScenes",title:"Would Create Scenes"},{key:"manualReview",title:"Manual Review Required"}],Kn={alreadyLinked:"Already Linked",couldLink:"Could Link",wouldCreateChapter:"Would Create Chapter",wouldCreateScene:"Would Create Scene",manualReview:"Manual Review Required"},Qn={alreadyLinked:"✓",couldLink:"~",wouldCreateChapter:"+",wouldCreateScene:"+",manualReview:"?"},Xn=(e,t)=>pn(e,t).toLocaleLowerCase(),Zn=({category:e,action:t,type:r,wordTitle:n,match:o="",anchorStatus:a="None",matches:c=[],pdChapter:i=null,pdScene:s=null,wordChapter:d=null,wordScene:l=null,parentItem:u=null})=>({id:`${r}-${d?.index||0}-${l?.index||0}-${d?.paragraphIndex??l?.paragraphIndex??0}`,category:e,action:t,type:r,wordTitle:n,match:o,anchorStatus:a,matches:c,pdChapter:i,pdScene:s,wordChapter:d,wordScene:l,parentItem:u,result:"Pending",error:""}),_n=(e,t)=>{Array.isArray(e[t.category])&&e[t.category].push(t)},eo=e=>Array.isArray(e?.scenes)?e.scenes:[],to=(e,t,r)=>{const n=e.anchorId?`PD-CHAPTER-${e.anchorId}`:"None";if(e.anchorId){const o=t.find(t=>t.chapterId===e.anchorId);if(o){const t=Zn({category:"alreadyLinked",action:"alreadyLinked",type:"Chapter",wordTitle:e.title,match:`Chapter ${o.chapterId}: ${o.title}`,anchorStatus:n,pdChapter:o,wordChapter:e});return _n(r,t),t}const a=Zn({category:"manualReview",action:"manualReview",type:"Chapter",wordTitle:e.title,anchorStatus:`${n} not found`,matches:[],pdChapter:null,wordChapter:e});return _n(r,a),a}const o=t.filter(t=>Xn(t.title,"chapter")===Xn(e.title,"chapter"));if(1===o.length){const t=Zn({category:"couldLink",action:"couldLink",type:"Chapter",wordTitle:e.title,match:`Chapter ${o[0].chapterId}: ${o[0].title}`,anchorStatus:n,pdChapter:o[0],wordChapter:e});return _n(r,t),t}if(o.length>1){const t=Zn({category:"manualReview",action:"manualReview",type:"Chapter",wordTitle:e.title,anchorStatus:n,matches:o.map(e=>`Chapter ${e.chapterId}: ${e.title}`),pdChapter:null,wordChapter:e});return _n(r,t),t}const a=Zn({category:"wouldCreateChapters",action:"wouldCreateChapter",type:"Chapter",wordTitle:e.title,anchorStatus:n,pdChapter:null,wordChapter:e});return _n(r,a),a},ro=(e,t,r,n)=>{const o=e.anchorId?`PD-SCENE-${e.anchorId}`:"None";if(e.anchorId){const a=((e,t)=>{for(const r of e){const e=eo(r).find(e=>e.sceneId===t);if(e)return{chapter:r,scene:e}}return null})(r,e.anchorId);return a?void _n(n,Zn({category:"alreadyLinked",action:"alreadyLinked",type:"Scene",wordTitle:e.title,match:`Scene ${a.scene.sceneId}: ${a.scene.title}`,anchorStatus:o,pdChapter:a.chapter,pdScene:a.scene,wordChapter:t?.wordChapter,wordScene:e,parentItem:t})):void _n(n,Zn({category:"manualReview",action:"manualReview",type:"Scene",wordTitle:e.title,anchorStatus:`${o} not found`,wordChapter:t?.wordChapter,wordScene:e,parentItem:t}))}if(!t?.pdChapter&&"manualReview"===t?.category)return void _n(n,Zn({category:"manualReview",action:"manualReview",type:"Scene",wordTitle:e.title,anchorStatus:o,matches:["Parent chapter requires manual review."],wordChapter:t?.wordChapter,wordScene:e,parentItem:t}));if(!t?.pdChapter)return void _n(n,Zn({category:"wouldCreateScenes",action:"wouldCreateScene",type:"Scene",wordTitle:e.title,anchorStatus:o,wordChapter:t?.wordChapter,wordScene:e,parentItem:t}));const a=eo(t.pdChapter).filter(t=>Xn(t.title,"scene")===Xn(e.title,"scene"));1!==a.length?a.length>1?_n(n,Zn({category:"manualReview",action:"manualReview",type:"Scene",wordTitle:e.title,anchorStatus:o,matches:a.map(e=>`Scene ${e.sceneId}: ${e.title}`),wordChapter:t.wordChapter,wordScene:e,parentItem:t})):_n(n,Zn({category:"wouldCreateScenes",action:"wouldCreateScene",type:"Scene",wordTitle:e.title,anchorStatus:o,pdChapter:t.pdChapter,wordChapter:t.wordChapter,wordScene:e,parentItem:t})):_n(n,Zn({category:"couldLink",action:"couldLink",type:"Scene",wordTitle:e.title,match:`Scene ${a[0].sceneId}: ${a[0].title}`,anchorStatus:o,pdChapter:t.pdChapter,pdScene:a[0],wordChapter:t.wordChapter,wordScene:e,parentItem:t}))},no=e=>{const t=document.createElement("article");t.className="word-companion-preview-item";const r=document.createElement("strong");r.textContent=`${Qn[e.action]||""} ${e.type}: ${e.wordTitle}`,t.append(r);const n=document.createElement("span");if(n.textContent=`Anchor: ${e.anchorStatus||"None"}`,t.append(n),e.match){const r=document.createElement("span");r.textContent=`Match: ${e.match}`,t.append(r)}if(Array.isArray(e.matches)&&e.matches.length>0){const r=document.createElement("div");r.className="word-companion-preview-matches";const n=document.createElement("span");n.textContent="Matches:",r.append(n);const o=document.createElement("ul");for(const t of e.matches){const e=document.createElement("li");e.textContent=t,o.append(e)}r.append(o),t.append(r)}const o=document.createElement("span");if(o.textContent=`Action: ${Kn[e.action]||e.action}`,t.append(o),e.result&&"Pending"!==e.result){const r=document.createElement("span");r.textContent=e.error?`Result: ${e.result} - ${e.error}`:`Result: ${e.result}`,t.append(r)}return t},oo=e=>{if(ee){ee.innerHTML="";for(const t of zn){const r=document.createElement("section");r.className="word-companion-preview-group";const n=document.createElement("strong");n.textContent=t.title,r.append(n);const o=Array.isArray(e?.[t.key])?e[t.key]:[];if(0===o.length){const e=document.createElement("p");e.className="word-companion-empty-list",e.textContent="None",r.append(e)}else for(const e of o)r.append(no(e));ee.append(r)}Br()}},ao=async e=>{const t=e.document.body.paragraphs,r=e.document.getSelection().paragraphs;t.load("text,styleBuiltIn,style"),r.load("text,styleBuiltIn,style"),await e.sync();for(const e of t.items)(Bn(e,1)||Bn(e,2))&&e.contentControls.load("items/tag,title");await e.sync();const n=Jn(t.items,r.items);return n.bodyParagraphs=t.items,n},co=async()=>{if(Ar(""),!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return Rr(null,null,null),Ar("Word document access is unavailable."),Cn({lastScan:"Failure",lastError:"Word document access is unavailable."}),!1;F&&(F.disabled=!0,F.textContent="Scanning..."),Ar("Scanning document structure...");try{const e=await window.Word.run(async e=>await ao(e));return Rr(e.currentChapter?.title,e.currentScene?.title,e.currentScene?.wordCount,e.currentChapter?.anchorId,e.currentScene?.anchorId,e.currentChapter?.index),cn("Not detected"),Yn(e),Ar(""),Cn({lastScan:"Success",lastError:"-",paragraphs:String(e.paragraphCount),heading1:String(e.heading1Count),heading2:String(e.heading2Count)}),!0}catch(e){const t=bn(e);return console.error("Unable to scan the Word document.",e),Rr(null,null,null),Ar(`Unable to scan the Word document: ${t}`),Cn({lastScan:"Failure",lastError:t}),!1}finally{F&&(F.disabled=!1,F.textContent="Refresh Document Structure")}},io=async()=>{const e=xn();if(!qn()||!e)return Mr("Select a project and book to analyse manuscript structure."),!1;if(!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return Mr("Word document access is unavailable."),Cn({lastScan:"Failure",lastError:"Word document access is unavailable."}),!1;X&&(X.disabled=!0,X.textContent="Analysing..."),Mr("Analysing manuscript structure...");try{const t=await window.Word.run(async e=>await ao(e)),r=await fo(`/api/word-companion/books/${e.bookId}/structure`);return Rr(t.currentChapter?.title,t.currentScene?.title,t.currentScene?.wordCount,t.currentChapter?.anchorId,t.currentScene?.anchorId,t.currentChapter?.index),Yn(t),sr=((e,t)=>{const r={alreadyLinked:[],couldLink:[],wouldCreateChapters:[],wouldCreateScenes:[],manualReview:[]},n=Array.isArray(t?.chapters)?t.chapters:[];for(const t of Array.isArray(e?.chapters)?e.chapters:[]){const e=to(t,n,r);for(const o of eo(t))ro(o,e,n,r)}return r})(t,r),oo(sr),Mr("Preview generated. No changes have been applied."),Cn({lastScan:"Success",lastError:"-",paragraphs:String(t.paragraphCount),heading1:String(t.heading1Count),heading2:String(t.heading2Count)}),!0}catch(e){return console.error("Unable to analyse manuscript structure.",e),Mr("Unable to analyse manuscript structure."),Cn({lastScan:"Failure",lastError:bn(e)}),!1}finally{X&&(X.textContent="Analyse Manuscript Structure",Fr())}},so=e=>{wr&&(wr(e),wr=null),dt?.close?dt.close():dt&&(dt.hidden=!0)},lo=()=>{const e=((e=sr)=>{const t=Hr(e);return{linkChapters:t.filter(e=>"couldLink"===e.category&&"Chapter"===e.type).length,linkScenes:t.filter(e=>"couldLink"===e.category&&"Scene"===e.type).length,createChapters:t.filter(e=>"wouldCreateChapters"===e.category).length,createScenes:t.filter(e=>"wouldCreateScenes"===e.category).length,manualReview:Array.isArray(e?.manualReview)?e.manualReview.length:0}})();return(e=>{if(!lt)return;lt.innerHTML="";const t=[`Link ${e.linkChapters+e.linkScenes} chapters/scenes`,`Create ${e.createChapters} chapters`,`Create ${e.createScenes} scenes`],r=document.createElement("ul");for(const e of t){const t=document.createElement("li");t.textContent=e,r.append(t)}lt.append(r)})(e),dt?new Promise(e=>{wr=e,dt.showModal?dt.showModal():dt.hidden=!1}):Promise.resolve(window.confirm(`Apply Structure Sync?\n\nThis will:\n\n- Link ${e.linkChapters+e.linkScenes} chapters/scenes\n- Create ${e.createChapters} chapters\n- Create ${e.createScenes} scenes\n\nManual review items will be skipped.`))},uo=(e,t,r="")=>{e.result=t,e.error=r},po=e=>Number.isInteger(e?.wordChapter?.index)&&e.wordChapter.index>0?10*e.wordChapter.index:null,ho=e=>Number.isInteger(e?.wordScene?.index)&&e.wordScene.index>0?10*e.wordScene.index:null,mo=async(e,t)=>{const r=await wo(`/api/word-companion/books/${e}/chapters`,{title:un(t.wordTitle),sortOrder:po(t)});if(!r?.chapterId)throw new Error("Chapter creation did not return a chapter ID.");t.pdChapter={chapterId:r.chapterId,title:r.title||t.wordTitle,sortOrder:r.sortOrder||po(t)||0,scenes:[]},t.match=`Chapter ${t.pdChapter.chapterId}: ${t.pdChapter.title}`},yo=async(e,t)=>{const r=t.pdChapter||t.parentItem?.pdChapter;if(!r?.chapterId)throw new Error("Scene parent chapter is not available.");const n=await wo(`/api/word-companion/books/${e}/chapters/${r.chapterId}/scenes`,{sceneTitle:un(t.wordTitle),sortOrder:ho(t)});if(!n?.sceneId)throw new Error("Scene creation did not return a scene ID.");t.pdChapter=r,t.pdScene={sceneId:n.sceneId,title:t.wordTitle,sortOrder:ho(t)||0},t.match=`Scene ${t.pdScene.sceneId}: ${t.pdScene.title}`},fo=async(e,t={})=>{const r=await window.fetch(e,{credentials:"same-origin",...t,headers:{Accept:"application/json",...t.headers||{}}});if(!r.ok)throw new Error(`Request failed: ${r.status}`);return await r.json()},wo=(e,t)=>fo(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}),So=()=>{$n(!1),An(""),Wr(v,!0)},go=async()=>{if(!Ut||!Bt)return void $n(!1);const r=(()=>{const e=window.Office?.context?.document?.settings;if(!e)return null;const t=String(e.get(n)||"").trim(),r=Number.parseInt(e.get(o)||"",10),i=Number.parseInt(e.get(a)||"",10),s=Number.parseInt(e.get(c)||"",10);return!t||!Number.isInteger(r)||r<=0||!Number.isInteger(i)||i<=0?null:{documentGuid:t,bookId:r,projectId:i,bindingVersion:Number.isInteger(s)&&s>0?s:1}})();if(r)try{const n=await fo(`/api/word-companion/manuscript/book/${r.bookId}`),o=n?.manuscriptDocument;if(o&&String(o.documentGuid).toLowerCase()===r.documentGuid.toLowerCase())return dr=r,vn(e,r.projectId),vn(t,r.bookId),U&&(U.value=String(r.projectId)),await Uo(r.projectId,r.bookId),$n(!1),void xr("Connected")}catch{}dr=null,jn(),Pn()?await Uo(Pn().projectId,null):En(m,"Select a project first"),Rn("Select a Project and Book to begin."),$n(!0)},Co=e=>e?[...e.querySelectorAll("input[type='checkbox']:checked")].map(e=>Number.parseInt(e.value||"",10)).filter(e=>Number.isInteger(e)&&e>0):[],bo=()=>{const e=Number.parseInt(Le?.value||"",10);return Number.isInteger(e)&&e>0?e:null},Io=(e,t,r,n)=>{const o=((e,t)=>(Array.isArray(e?.contentControls?.items)?e.contentControls.items:[]).find(e=>Gn(e.tag,t))||null)(e,n),a=o||e.getRange().insertContentControl();return a.title=t,a.tag=r,a.appearance="Hidden",a},ko=async(e="PlotDirector IDs attached successfully.")=>{if(!Xt||!Zt)return Ar("No resolved PlotDirector scene."),!1;if((Qt&&Qt!==Xt||Kt&&Kt!==Zt)&&!window.confirm("This scene is already linked. Replace the existing PlotDirector link?"))return!1;if(!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return Ar("Word document access is unavailable."),!1;ie&&(ie.disabled=!0,ie.textContent="Attaching...");try{return await window.Word.run(async e=>{const t=await ao(e);if(!t.currentChapter||!t.currentScene)throw new Error("No scene detected in Word. Use Heading 2 for scenes.");const r=t.bodyParagraphs[t.currentChapter.paragraphIndex],n=t.bodyParagraphs[t.currentScene.paragraphIndex];if(!r||!n)throw new Error("Unable to locate the current Word headings.");Io(r,"PlotDirector Chapter",`PD-CHAPTER-${Zt}`,"PD-CHAPTER"),Io(n,"PlotDirector Scene",`PD-SCENE-${Xt}`,"PD-SCENE"),await e.sync()}),Kt=Zt,Qt=Xt,tr=!1,er="SceneID Anchor",_t="Anchor",Ar(e),Cn({lastError:"-"}),jr(),!0}catch(e){return console.error("Unable to attach PlotDirector IDs.",e),Ar("Unable to attach PlotDirector IDs."),Cn({lastError:bn(e)}),jr(),!1}finally{ie&&(ie.textContent=Qt===Xt?"Attach PlotDirector IDs":"Reattach PlotDirector IDs",jr())}},vo=async e=>{if(!Zt)return _r("No resolved PlotDirector chapter."),!1;if(!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return _r("Word document access is unavailable."),!1;try{return await window.Word.run(async e=>{const t=await ao(e);if(!t.currentChapter)throw new Error("No chapter detected in Word. Use Heading 1 for chapters.");const r=t.bodyParagraphs[t.currentChapter.paragraphIndex];if(!r)throw new Error("Unable to locate the current Word chapter heading.");Io(r,"PlotDirector Chapter",`PD-CHAPTER-${Zt}`,"PD-CHAPTER"),await e.sync()}),Kt=Zt,tr=!1,er="ChapterID Anchor",_t="Chapter anchor",_r(e),Ar(e),Cn({lastError:"-"}),jr(),!0}catch(e){return console.error("Unable to attach PlotDirector chapter ID.",e),_r("Unable to attach PlotDirector chapter ID."),Cn({lastError:bn(e)}),jr(),!1}},Eo=async e=>{rn(),await $o(),Ar(e),_r(e)},Lo=e=>{ir&&(ir(e),ir=null),ot?.close?ot.close():ot&&(ot.hidden=!0)},No=()=>{if(!et)return;const e=un(_e?.value||"").toLocaleLowerCase(),t=ar.filter(t=>!e||String(t.title||"").toLocaleLowerCase().includes(e));if(et.innerHTML="",cr=null,tt&&(tt.disabled=!0),0===t.length){const e=document.createElement("p");return e.className="word-companion-empty-list",e.textContent="No chapters found.",void et.append(e)}for(const e of t){const t=Number.parseInt(e.chapterId||"",10);if(!Number.isInteger(t)||t<=0)continue;const r=document.createElement("label");r.className="word-companion-scene-option";const n=document.createElement("input");n.type="radio",n.name="word-companion-link-chapter",n.value=String(t),n.addEventListener("change",()=>{cr=t,tt&&(tt.disabled=!1)});const o=document.createElement("span");o.textContent=e.title||"Untitled chapter",r.append(n,o),et.append(r)}},qo=()=>{Ze?.close?Ze.close():Ze&&(Ze.hidden=!0)},xo=async(e,t,r)=>{const n=xn();if(!n)return Zr("Select a PlotDirector book first."),!1;await Sn(n.bookId,e,t,"Manual link","Title Match");return!!await ko(r)&&(Zr(r),on(),!0)},Po=e=>{or&&(or(e),or=null),Ye?.close?Ye.close():Ye&&(Ye.hidden=!0)},Do=()=>{if(!Fe)return;const e=un(Be?.value||"").toLocaleLowerCase(),t=rr.filter(t=>!e||String(t.title||"").toLocaleLowerCase().includes(e));if(Fe.innerHTML="",nr=null,Ge&&(Ge.disabled=!0),0===t.length){const e=document.createElement("p");return e.className="word-companion-empty-list",e.textContent="No scenes found.",void Fe.append(e)}for(const e of t){const t=Number.parseInt(e.sceneId||"",10);if(!Number.isInteger(t)||t<=0)continue;const r=document.createElement("label");r.className="word-companion-scene-option";const n=document.createElement("input");n.type="radio",n.name="word-companion-link-scene",n.value=String(t),n.addEventListener("change",()=>{nr=t,Ge&&(Ge.disabled=!1)});const o=document.createElement("span");o.textContent=e.title||"Untitled scene",r.append(n,o),Fe.append(r)}},Ao=()=>{He?.close?He.close():He&&(He.hidden=!0)},To=async()=>{const e=qn(),t=xn(),r=pn(Vt,"chapter");if(hn({projectId:e?.projectId,projectTitle:e?.title,bookId:t?.bookId,bookTitle:t?Nn(t):void 0,detectedChapter:Vt,detectedScene:Jt,requestChapter:r,requestScene:"",result:"Not run",chapterId:null,sceneId:null}),!t)return cn("Not detected"),Ar("Select a PlotDirector book first."),!1;if(!Vt)return cn("Not detected"),Ar("No chapter detected in Word. Use Heading 1 for chapters."),!1;cn("Not detected");try{const e=await yn(t.bookId),n=Kt?e.find(e=>e.chapterId===Kt):null,o=r.toLocaleLowerCase(),a=o?e.find(e=>pn(e.title,"chapter").toLocaleLowerCase()===o):null,c=n||a;return c?(rn(),zr(null,c.chapterId,n?"Chapter anchor":"Chapter title"),er=n?"ChapterID Anchor":"Title Match",Tr("Chapter linked to PlotDirector"),Ar("No scene detected in Word. Use Heading 2 for scenes."),hn({result:"Chapter matched",chapterId:c.chapterId,sceneId:null}),!0):(zr(null),Tr("Chapter not found in PlotDirector."),Ar("Chapter not found in PlotDirector."),hn({result:"Chapter not matched",chapterId:null,sceneId:null}),nn("Chapter not found in PlotDirector."),!1)}catch(e){return cn("Not found in PlotDirector"),Cn({lastError:bn(e)}),Ar("Unable to load PlotDirector chapter data."),!1}},$o=async()=>{const e=qn(),t=xn(),r=pn(Vt,"chapter"),n=pn(Jt,"scene");if(hn({projectId:e?.projectId,projectTitle:e?.title,bookId:t?.bookId,bookTitle:t?Nn(t):void 0,detectedChapter:Vt,detectedScene:Jt,requestChapter:r,requestScene:n,result:"Not run",chapterId:null,sceneId:null}),$r({anchorType:Qt?"SceneID Anchor":Kt?"ChapterID Anchor":"Title Match",storedSceneId:Qt,storedChapterId:Kt,resolutionMethod:"-"}),!t)return cn("Not detected"),Ar("Select a PlotDirector book first."),hn({result:"Error"}),!1;if(!Vt||!Jt)return cn("Not detected"),Ar("No scene detected in Word. Use Heading 2 for scenes."),hn({result:"Error"}),!1;te&&(te.disabled=!0,te.textContent="Refreshing...");const o=()=>{te&&(te.disabled=!1,te.textContent="Refresh PlotDirector Scene")};cn("Not detected"),Ar("Resolving PlotDirector scene...");try{if(Qt){const e=await(async(e,t)=>{const r=await fo(`/api/word-companion/books/${e}/structure`),n=Array.isArray(r?.chapters)?r.chapters:[];for(const e of n){const r=Array.isArray(e.scenes)?e.scenes:[];for(const n of r)if(t(e,n))return{chapter:e,scene:n}}return null})(t.bookId,(e,t)=>t.sceneId===Qt);if(e)return hn({result:"Matched",chapterId:e.chapter.chapterId,sceneId:e.scene.sceneId}),await Sn(t.bookId,e.scene.sceneId,e.chapter.chapterId,"Anchor","SceneID Anchor"),!0;tr=!0,er="SceneID Anchor",Tr("Not found in PlotDirector"),Ar("Stored PlotDirector ID is invalid."),hn({result:"Error",chapterId:Kt,sceneId:Qt}),$r({anchorType:"SceneID Anchor",storedSceneId:Qt,storedChapterId:Kt,resolutionMethod:"Anchor"})}if(Kt){const e=(await mn(t.bookId)).find(e=>e.chapterId===Kt),r=e?(Array.isArray(e.scenes)?e.scenes:[]).find(e=>pn(e.title,"scene").toLocaleLowerCase()===n.toLocaleLowerCase()):null;if(r)return hn({result:"Matched",chapterId:e.chapterId,sceneId:r.sceneId}),await Sn(t.bookId,r.sceneId,e.chapterId,"Anchor","ChapterID Anchor"),!0;e&&!Qt?(er="ChapterID Anchor",zr(null,Kt,"Chapter anchor"),hn({result:"Chapter matched",chapterId:Kt,sceneId:null})):Qt||(er="ChapterID Anchor",hn({result:"Chapter anchor not found",chapterId:Kt,sceneId:null}))}const e=await wo(`/api/word-companion/books/${t.bookId}/resolve-scene`,{chapterTitle:r,sceneTitle:n});if(!e?.matched||!e.sceneId){const t=tr,r=Number.parseInt(e?.chapterId||"",10),n=Number.isInteger(Zt)&&Zt>0?Zt:null,a=Number.isInteger(r)&&r>0?r:n,c=!!e?.chapterMatched||!!a;return cn("Not found in PlotDirector"),tr=t,c&&(zr(null,a,a===n?"Chapter anchor":"Chapter title"),Tr("Scene not found in PlotDirector.")),Ar(t?"Stored PlotDirector ID is invalid.":c?"Scene not found in PlotDirector.":"This chapter does not exist in PlotDirector. Create or link the chapter first."),hn({result:"Not matched",chapterId:c?a:e?.chapterId,sceneId:e?.sceneId}),t||(c?(rn(),an("Scene not found in PlotDirector.")):(on(),nn("Chapter not found in PlotDirector."))),o(),!1}const a=tr;return hn({result:a?"Matched by title fallback":"Matched",chapterId:e.chapterId,sceneId:e.sceneId}),await Sn(t.bookId,e.sceneId,e.chapterId,a?"Title fallback":"Title","Title Match"),a&&(tr=!0,Ar("Stored PlotDirector ID is invalid."),jr()),!0}catch(e){const t=tr;return Cn({lastError:bn(e)}),cn("Not found in PlotDirector"),tr=t,Ar(t?"Stored PlotDirector ID is invalid.":"Unable to load PlotDirector scene data."),hn({result:"Error"}),!1}finally{o()}},jo=async()=>{if(!Ir&&Ut&&xn()){if(!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return Jr("Manual"),void Ar("Automatic scene tracking is unavailable in this Word version. Use Refresh Current Scene.");Ir=!0,Jr("Refreshing"),Ar("Updating current scene...");try{const e=await window.Word.run(async e=>await ao(e)),t=(e=>{const t=e.currentChapter?.title||"",r=e.currentScene?.title||"",n=e.currentChapter?.anchorId||null,o=e.currentScene?.anchorId||null;return pn(t,"chapter")!==pn(Vt,"chapter")||pn(r,"scene")!==pn(Jt,"scene")||n!==Kt||o!==Qt})(e);if(Rr(e.currentChapter?.title,e.currentScene?.title,e.currentScene?.wordCount,e.currentChapter?.anchorId,e.currentScene?.anchorId,e.currentChapter?.index),Yn(e),Cn({lastScan:"Success",lastError:"-",paragraphs:String(e.paragraphCount),heading1:String(e.heading1Count),heading2:String(e.heading2Count)}),!t)return Yr(!1),Jr("Live"),void Ar("Current scene updated.");Jt?await $o():await To(),Yr(!1),Jr("Live"),Ar("Current scene updated.")}catch(e){console.error("Unable to refresh current scene from Word selection.",e),Cn({lastScan:"Failure",lastError:bn(e)}),Yr(!0,"The Word cursor is now in a different scene. Refresh Current Scene before saving.")}finally{Ir=!1}}},Ro=()=>{Cr&&window.clearTimeout(Cr),Cr=window.setTimeout(jo,850)},Wo=()=>{if(br&&window.Office?.context?.document&&"function"==typeof window.Office.context.document.removeHandlerAsync&&window.Office?.EventType?.DocumentSelectionChanged)try{window.Office.context.document.removeHandlerAsync(window.Office.EventType.DocumentSelectionChanged,{handler:Ro}),br=!1}catch(e){console.warn("Unable to remove Word selection tracking handler.",e)}},Uo=async(e,r)=>{if(!e)return Ot=[],En(M,"Select a project first"),En(m,"Select a project first"),Dr(""),vn(t,null),On(),cn("Not detected"),void Or();En(M,"Loading books..."),Dr(""),cn("Not detected"),Or("Select a book to analyse manuscript structure.");try{const n=await fo(`/api/word-companion/projects/${e}/books`);if(Ot=Array.isArray(n.books)?n.books:[],0===Ot.length)return En(M,"No books available."),En(m,"No books available."),Dr("No books available."),vn(t,null),On(),void Rn("No books available.");Ln(M,"Choose a book",Ot.map(e=>({...e,displayTitle:Nn(e)})),"bookId","displayTitle");const o=Ot.find(e=>e.bookId===r);o&&M?(M.value=String(o.bookId),vn(t,o.bookId)):vn(t,null),((e=null)=>{if(!m)return;if(0===Ot.length)return void En(m,"No books available.");Ln(m,"Choose a book",Ot.map(e=>({...e,displayTitle:Nn(e)})),"bookId","displayTitle");const t=e||Number.parseInt(M?.value||"",10),r=Ot.find(e=>e.bookId===t);r&&(m.value=String(r.bookId)),Tn()})(o?.bookId||r),On(),cn((xn(),"Not detected")),Or(xn()?"Ready to analyse manuscript structure.":"Select a book to analyse manuscript structure."),Rn(Dn()?"Ready to scan manuscript structure.":"Select a Book to begin.")}catch{Ot=[],En(M,"Unable to load books."),En(m,"Unable to load books."),Dr("Unable to load books."),vn(t,null),On(),cn("Not detected"),Or("Unable to load books."),Rn("Unable to load books.")}};for(const e of l)e.addEventListener("click",()=>Nr(e.dataset.tabButton));qr(),U?.addEventListener("change",async()=>{const r=Number.parseInt(U.value||"",10);h&&Number.isInteger(r)&&r>0&&(h.value=String(r)),vn(e,Number.isInteger(r)&&r>0?r:null),vn(t,null),cn("Not detected"),Or(),Rn("Select a Book to begin."),await Uo(r,null)}),M?.addEventListener("change",()=>{const e=Number.parseInt(M.value||"",10);vn(t,Number.isInteger(e)&&e>0?e:null),m&&Number.isInteger(e)&&e>0&&(m.value=String(e)),On(),cn("Not detected"),Or(xn()?"Ready to analyse manuscript structure.":"Select a book to analyse manuscript structure."),Rn(Dn()?"Ready to scan manuscript structure.":"Select a Book to begin.")}),h?.addEventListener("change",async()=>{const r=Number.parseInt(h.value||"",10);U&&Number.isInteger(r)&&r>0&&(U.value=String(r)),vn(e,Number.isInteger(r)&&r>0?r:null),vn(t,null),Rn("Select a Book to begin."),await Uo(r,null)}),m?.addEventListener("change",()=>{const e=Number.parseInt(m.value||"",10);M&&Number.isInteger(e)&&e>0&&(M.value=String(e)),vn(t,Number.isInteger(e)&&e>0?e:null),On(),Rn(Dn()?"Ready to scan manuscript structure.":"Select a Book to begin.")}),y?.addEventListener("input",Tn),f?.addEventListener("click",async()=>{const e=Pn(),t=String(y?.value||"").trim();if(e&&t){f.disabled=!0,An("Creating Book...");try{const r=await wo(`/api/word-companion/projects/${e.projectId}/books`,{title:t});y&&(y.value=""),U&&(U.value=String(e.projectId)),await Uo(e.projectId,r.bookId),m&&(m.value=String(r.bookId)),An("Ready to scan manuscript structure.")}catch(e){console.error("Unable to create Book from Word Companion.",e),An(`Unable to create Book: ${bn(e)}`)}finally{Tn()}}else Tn()}),w?.addEventListener("click",async()=>{const e=Pn(),t=Dn();if(e&&t)if(Ht&&Bt&&window.Word&&"function"==typeof window.Word.run){w&&(w.disabled=!0,w.textContent="Scanning..."),An("Scanning manuscript structure...");try{const r=await window.Word.run(async e=>{const r=e.document.body.paragraphs;return r.load("text,styleBuiltIn,style"),await e.sync(),((e,t)=>{const r=[];let n=null,o=null,a=0;const c=[],i=new Set(["***","###"]),s=()=>{if(!n)return null;const e={title:`Scene ${n.scenes.length+1}`,sortOrder:10*(n.scenes.length+1),wordCount:0};return n.scenes.push(e),e};return e.forEach(e=>{const d=String(e.text||"").trim();if(!d)return;if(c.push(d),Bn(e,1))return n={title:d,sortOrder:10*(r.length+1),wordCount:0,scenes:[]},r.push(n),o=s(),void(a+=Fn(d));if(!n||!o)return;if(t&&i.has(d))return void(o=s());const l=Fn(d);n.wordCount+=l,o.wordCount+=l,a+=l}),{chapters:r,chapterCount:r.length,sceneCount:r.reduce((e,t)=>e+t.scenes.length,0),wordCount:a,documentText:c.join("\n")}})(r.items,!!t.usesExplicitScenes)});ur=r,hr=!1;const n=await wo("/api/word-companion/manuscript/analyse",{projectId:e.projectId,bookId:t.bookId,documentGuid:Mn(),chapterCount:r.chapterCount,sceneCount:r.sceneCount,wordCount:r.wordCount});lr=n,(e=>{if(!C)return;C.innerHTML="";const t=document.createElement("strong");t.textContent="Manuscript Analysis",C.append(t);const r=document.createElement("dl"),n=[["Project",e.projectTitle],["Book",Nn({title:e.bookTitle,subtitle:e.bookSubtitle})],["Chapters",Number(e.chapterCount||0).toLocaleString()],["Scenes",Number(e.sceneCount||0).toLocaleString()],["Words",Number(e.wordCount||0).toLocaleString()]];for(const[e,t]of n){const n=document.createElement("dt");n.textContent=e;const o=document.createElement("dd");o.textContent=t||"-",r.append(n,o)}C.append(r),Wr(C,!1),Wr(N,!1),An(e.message||"Preview generated."),Tn()})(n);try{const t=await wo("/api/word-companion/manuscript/discover-characters",{projectId:e.projectId,documentText:r.documentText||""});Un(t?.candidates||[])}catch(e){console.error("Unable to discover character candidates.",e),Un([]),gn(I,"Character discovery is unavailable.")}Cn({lastScan:"Success",lastError:"-",paragraphs:"-",heading1:String(r.chapterCount),heading2:"-"})}catch(e){console.error("Unable to scan manuscript for first-run import.",e),Rn(`Unable to scan manuscript: ${bn(e)}`),Cn({lastScan:"Failure",lastError:bn(e)})}finally{w&&(w.textContent="Scan Manuscript"),Tn()}}else Rn("Word document access is unavailable.");else Rn("Select a Project and Book to begin.")}),q?.addEventListener("click",async()=>{const r=Pn(),i=Dn();if(!(r&&i&&ur&&lr?.canImport))return void Tn();let s=!1;if(!lr.requiresReplaceConfirmation||(s=await new Promise(e=>{if(!P||"function"!=typeof P.showModal)return void e(window.confirm("This Book already contains planned chapters and scenes.\n\nImporting this manuscript will replace the planned structure.\n\nContinue?"));const t=t=>{D?.removeEventListener("click",r),A?.removeEventListener("click",n),P?.removeEventListener("cancel",o),P?.close(),e(t)},r=()=>t(!0),n=()=>t(!1),o=e=>{e.preventDefault(),t(!1)};D?.addEventListener("click",r),A?.addEventListener("click",n),P?.addEventListener("cancel",o),P.showModal()}),s)){mr=!0,Tn(),An("Importing manuscript structure...");try{const l=Mn(),u=await wo("/api/word-companion/manuscript/import",{projectId:r.projectId,bookId:i.bookId,documentGuid:l,bindingVersion:1,replacePlanned:s,chapters:ur.chapters});if(!u.imported)return lr={...lr,canImport:!u.requiresReplaceConfirmation,requiresReplaceConfirmation:!!u.requiresReplaceConfirmation,message:u.message||lr.message},An(u.message||"Import was not completed."),void Tn();const p=u.manuscriptDocument;await(d={documentGuid:p?.documentGuid||l,bookId:u.bookId,projectId:u.projectId,bindingVersion:p?.bindingVersion||1},new Promise((e,t)=>{const r=window.Office?.context?.document?.settings;r?(r.set(n,d.documentGuid),r.set(o,String(d.bookId)),r.set(a,String(d.projectId)),r.set(c,String(d.bindingVersion||1)),r.saveAsync(r=>{r.status===window.Office.AsyncResultStatus.Succeeded?e():t(r.error||new Error("Unable to save Word document metadata."))})):t(new Error("Word document settings are unavailable."))})),dr={documentGuid:p?.documentGuid||l,bookId:u.bookId,projectId:u.projectId,bindingVersion:p?.bindingVersion||1},vn(e,u.projectId),vn(t,u.bookId),U&&(U.value=String(u.projectId));const h=pr;await Uo(u.projectId,u.bookId),xr("Connected"),hr=!0,pr=h,h.length>0?($n(!0),Un(h),Wr(N,!0),Wr(v,!1),An(u.message||"Manuscript structure imported."),Ar(u.message||"Manuscript structure imported.")):($n(!1),Ar(u.message||"Manuscript structure imported."))}catch(e){console.error("Unable to import manuscript structure.",e),An(`Unable to import manuscript: ${bn(e)}`)}finally{mr=!1,Tn()}var d}else An("Import cancelled.")}),E?.addEventListener("click",async()=>{const e=Pn()||qn(),t=Wn();if(e&&0!==t.length){yr=!0,Tn(),gn(I,"Creating selected characters...");try{const r=await wo("/api/word-companion/manuscript/create-discovered-characters",{projectId:e.projectId,candidateNames:t});Ar(r.message||"Characters created."),gn(I,`${r.createdCount||0} created, ${r.skippedCount||0} already existed.`),So()}catch(e){console.error("Unable to create discovered characters.",e),gn(I,`Unable to create characters: ${bn(e)}`)}finally{yr=!1,Tn()}}else Tn()}),L?.addEventListener("click",So),S?.addEventListener("click",()=>$n(!1)),x?.addEventListener("click",()=>Rn("Import cancelled.")),F?.addEventListener("click",co),X?.addEventListener("click",io),Z?.addEventListener("click",async()=>{const e=xn();if(!e||0===Hr().length||fr)return void Br();if(!await lo())return;fr=!0,Fr(),Mr("Applying structure sync...");const t={createdChapters:0,createdScenes:0,linkedChapters:0,linkedScenes:0,skipped:Array.isArray(sr?.manualReview)?sr.manualReview.length:0,failed:0},r=Hr().filter(e=>"couldLink"===e.category&&"Chapter"===e.type),n=Hr().filter(e=>"wouldCreateChapters"===e.category),o=Hr().filter(e=>"couldLink"===e.category&&"Scene"===e.type),a=Hr().filter(e=>"wouldCreateScenes"===e.category),c=[];let i="";const s=(e,t)=>{c.push({item:e,counterName:t})},d=async(e,r)=>{try{await(async e=>{if(!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)throw new Error("Word document access is unavailable.");await window.Word.run(async t=>{const r=await ao(t),n="Chapter"===e.type?e.wordChapter:e.wordScene,o=Number.isInteger(n?.paragraphIndex)?r.bodyParagraphs[n.paragraphIndex]:null;if(!o)throw new Error("Unable to locate the Word heading.");if("Chapter"===e.type){const t=e.pdChapter?.chapterId;if(!t)throw new Error("Chapter ID is not available.");Io(o,"PlotDirector Chapter",`PD-CHAPTER-${t}`,"PD-CHAPTER")}else{const t=e.pdScene?.sceneId;if(!t)throw new Error("Scene ID is not available.");Io(o,"PlotDirector Scene",`PD-SCENE-${t}`,"PD-SCENE")}await t.sync()})})(e),uo(e,"Success"),t[r]+=1}catch(r){uo(e,"Failed",bn(r)),t.failed+=1}};try{for(const e of r)s(e,"linkedChapters");for(const r of n)try{await mo(e.bookId,r),s(r,"createdChapters")}catch(e){uo(r,"Failed",bn(e)),t.failed+=1}for(const e of o)s(e,"linkedScenes");for(const r of a)try{await yo(e.bookId,r),s(r,"createdScenes")}catch(e){uo(r,"Failed",bn(e)),t.failed+=1}for(const e of c)await d(e.item,e.counterName);for(const e of sr.manualReview||[])uo(e,"Skipped");oo(sr),i=(e=>`Structure Sync Complete. Created: ${e.createdChapters} Chapters, ${e.createdScenes} Scenes. Linked: ${e.linkedChapters} Chapters, ${e.linkedScenes} Scenes. Skipped: ${e.skipped} Manual Review items. Failed: ${e.failed} Items.`)(t),Mr(i)}finally{fr=!1,Fr(),i&&(await io(),Mr(`${i} Preview refreshed.`))}}),ut?.addEventListener("click",()=>so(!0)),pt?.addEventListener("click",()=>so(!1)),dt?.addEventListener("cancel",e=>{e.preventDefault(),so(!1)}),te?.addEventListener("click",$o),B?.addEventListener("click",async()=>{if(!xn())return Ar("Select a PlotDirector book first."),void cn("Not detected");Kr(!0);try{if(!await co())return;if(Yr(!1),!Jt)return void await To();await $o()}finally{Kr(!1)}}),fe?.addEventListener("click",async()=>{if(!Xt)return void Ur("No resolved PlotDirector scene.");if(gr)return void Ur("Refresh Current Scene before syncing.");if(!Number.isInteger(Yt))return void Ur("Unable to sync progress.");fe&&(fe.disabled=!0,fe.textContent="Syncing...");const e=(new Date).toISOString();try{const n=await(t=`/api/word-companion/scenes/${Xt}/word-count`,r={actualWordCount:Yt,lastWorkedOn:e},fo(t,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)}));gn(Se,Qr(n?.actualWords??Yt)),gn(pe,Qr(n?.actualWords??Yt)),gn(ge,(new Date).toLocaleString()),Ur("Progress synced successfully.")}catch{Ur("Unable to sync progress.")}finally{fe&&(fe.disabled=!Xt||gr,fe.textContent="Sync Progress")}var t,r}),Ne?.addEventListener("click",async()=>{if(Xt)if(!gr&&await(async()=>{if(!Xt)return!1;if(!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return Yr(!0,"The Word cursor is now in a different scene. Refresh Current Scene before saving."),!1;try{const e=await window.Word.run(async e=>await ao(e)),t=e.currentChapter?.title||"",r=e.currentScene?.title||"",n=e.currentScene?.anchorId||null,o=e.currentChapter?.anchorId||null,a=Number.isInteger(n)&&n===Xt,c=Number.isInteger(o)&&o===Zt,i=pn(r,"scene").toLocaleLowerCase()===pn(kr||Jt,"scene").toLocaleLowerCase(),s=pn(t,"chapter").toLocaleLowerCase()===pn(vr||Vt,"chapter").toLocaleLowerCase();return a||i&&(c||s)?(Yr(!1),!0):(Yr(!0,"The Word cursor is now in a different scene. Refresh Current Scene before saving."),!1)}catch(e){return console.error("Unable to verify current Word scene before saving.",e),Cn({lastError:bn(e)}),Yr(!0,"The Word cursor is now in a different scene. Refresh Current Scene before saving."),!1}})()){Ne&&(Ne.disabled=!0,Ne.textContent="Saving...");try{await(e=`/api/word-companion/scenes/${Xt}/links`,t={characterIds:Co(ve),assetIds:Co(Ee),locationIds:[],primaryLocationId:bo()},fo(e,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})),(()=>{const e=new Set(Co(ve)),t=new Set(Co(Ee));Er={characters:Er.characters.map(t=>({...t,linked:e.has(Number.parseInt(sn(t,"character"),10))})),assets:Er.assets.map(e=>({...e,linked:t.has(Number.parseInt(sn(e,"asset"),10))})),locations:Er.locations,primaryLocationId:bo()}})(),Xr("Scene links saved successfully."),Cn({lastError:"-"})}catch(e){Xr("Unable to save scene links."),Cn({lastError:bn(e)})}finally{Ne&&(Ne.disabled=!Xt||gr,Ne.textContent="Save Scene Links")}var e,t}else Xr("The Word cursor is now in a different scene. Refresh Current Scene before saving.");else Xr("Link a PlotDirector scene first.")}),ie?.addEventListener("click",async()=>{await ko()}),Ae?.addEventListener("click",async()=>{const e=xn();if(!e||!Vt||Zt)return void nn("Chapter not found in PlotDirector.");const t=un(Vt),r=await((e,t)=>(gn(at,`"${e}"`),gn(ct,`"${t}"`),ot?new Promise(e=>{ir=e,ot.showModal?ot.showModal():ot.hidden=!1}):Promise.resolve(window.confirm(`Create PlotDirector chapter:\n\n"${e}"\n\nin book:\n\n"${t}"\n\n?`))))(t,Nn(e)||"selected book");if(r){Ae&&(Ae.disabled=!0,Ae.textContent="Creating...");try{const r=Number.isInteger(zt)&&zt>0?10*zt:null,n=await wo(`/api/word-companion/books/${e.bookId}/chapters`,{title:t,sortOrder:r});if(!n?.chapterId)throw new Error("Chapter creation did not return a chapter ID.");zr(null,n.chapterId,"Manual chapter link");if(!await vo("Chapter created and linked successfully."))return;await Eo("Chapter created and linked successfully.")}catch(e){_r("Unable to create chapter."),Cn({lastError:bn(e)})}finally{Ae&&(Ae.disabled=!en(),Ae.textContent="Create Chapter in PlotDirector")}}}),Te?.addEventListener("click",async()=>{const e=xn();if(e&&Vt&&!Zt){Te&&(Te.disabled=!0,Te.textContent="Loading...");try{ar=await yn(e.bookId),cr=null,_e&&(_e.value=""),gn(nt,""),No(),Ze?.showModal?Ze.showModal():Ze&&(Ze.hidden=!1)}catch(e){_r("Unable to load chapters."),Cn({lastError:bn(e)})}finally{Te&&(Te.disabled=!en(),Te.textContent="Link to Existing Chapter")}}else nn("Chapter not found in PlotDirector.")}),_e?.addEventListener("input",No),tt?.addEventListener("click",async()=>{const e=ar.find(e=>e.chapterId===cr);if(e){tt&&(tt.disabled=!0,tt.textContent="Linking...");try{zr(null,e.chapterId,"Manual chapter link");if(!await vo("Chapter linked successfully."))return;qo(),await Eo("Chapter linked successfully.")}catch(e){gn(nt,"Unable to link chapter."),_r("Unable to link chapter."),Cn({lastError:bn(e)})}finally{tt&&(tt.disabled=!cr,tt.textContent="Link Chapter")}}else gn(nt,"Select a chapter.")}),rt?.addEventListener("click",qo),it?.addEventListener("click",()=>Lo(!0)),st?.addEventListener("click",()=>Lo(!1)),ot?.addEventListener("cancel",e=>{e.preventDefault(),Lo(!1)}),Ue?.addEventListener("click",async()=>{const e=xn();if(!e||!Jt)return void an("Scene not found in PlotDirector.");const t=pn(Jt,"scene"),r=pn(Vt,"chapter"),n=await((e,t)=>(gn(ze,`"${e}"`),gn(Ke,`"${t}"`),Ye?new Promise(e=>{or=e,Ye.showModal?Ye.showModal():Ye.hidden=!1}):Promise.resolve(window.confirm(`Create PlotDirector scene:\n\n"${e}"\n\nwithin chapter:\n\n"${t}"\n\n?`))))(t,r);if(n){Ue&&(Ue.disabled=!0,Ue.textContent="Creating...");try{const r=await wn();if(!r)return void an("This chapter does not exist in PlotDirector. Create or link the chapter first.");const n=await wo(`/api/word-companion/books/${e.bookId}/chapters/${r.chapterId}/scenes`,{sceneTitle:t});if(!n?.sceneId||!n?.chapterId)throw new Error("Scene creation did not return a scene ID.");await xo(n.sceneId,n.chapterId,"Scene created and linked successfully.")}catch(e){Zr("Unable to create scene."),Cn({lastError:bn(e)})}finally{Ue&&(Ue.disabled=!tn(),Ue.textContent="Create Scene in PlotDirector")}}}),Me?.addEventListener("click",async()=>{const e=xn();if(e&&Jt){Me&&(Me.disabled=!0,Me.textContent="Loading...");try{const t=await mn(e.bookId),r=fn(t);if(!r)return void an("This chapter does not exist in PlotDirector. Create or link the chapter first.");rr=Array.isArray(r.scenes)?r.scenes:[],nr=null,Be&&(Be.value=""),gn(Je,""),Do(),He?.showModal?He.showModal():He&&(He.hidden=!1)}catch(e){Zr("Unable to load scenes."),Cn({lastError:bn(e)})}finally{Me&&(Me.disabled=!tn(),Me.textContent="Link to Existing Scene")}}else an("Scene not found in PlotDirector.")}),Be?.addEventListener("input",Do),Ge?.addEventListener("click",async()=>{const e=xn(),t=rr.find(e=>e.sceneId===nr);if(e&&t){Ge&&(Ge.disabled=!0,Ge.textContent="Linking...");try{const e=await wn();if(!e)throw new Error("This chapter does not exist in PlotDirector. Create or link the chapter first.");Ao(),await xo(t.sceneId,e.chapterId,"Scene linked successfully.")}catch(e){gn(Je,"Unable to link scene."),Zr("Unable to link scene."),Cn({lastError:bn(e)})}finally{Ge&&(Ge.disabled=!nr,Ge.textContent="Link Scene")}}else gn(Je,"Select a scene.")}),Ve?.addEventListener("click",Ao),Qe?.addEventListener("click",()=>Po(!0)),Xe?.addEventListener("click",()=>Po(!1)),Ye?.addEventListener("cancel",e=>{e.preventDefault(),Po(!1)}),vt?.addEventListener("click",async()=>{vt&&(vt.disabled=!0,vt.textContent="Running...");try{if(await In(),!Ht||!Bt||!window.Word||"function"!=typeof window.Word.run)return void Cn({lastScan:"Failure",lastError:"Word document access is unavailable.",paragraphs:"-",heading1:"-",heading2:"-"});const e=await window.Word.run(async e=>{const t=e.document.body.paragraphs;return t.load("items"),await e.sync(),t.items.length});Cn({lastScan:"Success",lastError:"-",paragraphs:String(e)})}catch(e){console.error("Word Companion diagnostics failed.",e);const t=bn(e);Cn({lastScan:"Failure",lastError:t}),Ar(`Unable to scan the Word document: ${t}`)}finally{vt&&(vt.disabled=!1,vt.textContent="Run Diagnostics")}});const Mo=Ut?(async()=>{En(U,"Loading projects..."),En(M,"Select a project first"),En(h,"Loading projects..."),En(m,"Select a project first"),Pr(""),Dr("");try{const r=await fo("/api/word-companion/projects");if(Mt=Array.isArray(r.projects)?r.projects:[],0===Mt.length)return En(U,"No projects available."),En(h,"No projects available."),Pr("No projects available."),vn(e,null),vn(t,null),On(),void Rn("No projects available.");Ln(U,"Choose a project",Mt,"projectId","title"),jn();const n=kn(e),o=Mt.find(e=>e.projectId===n);o&&U?(U.value=String(o.projectId),h&&(h.value=String(o.projectId)),vn(e,o.projectId),await Uo(o.projectId,kn(t))):(vn(e,null),vn(t,null),On(),Rn("Select a Project and Book to begin."))}catch{Mt=[],Ot=[],En(U,"Unable to connect to PlotDirector."),En(M,"Select a project first"),En(h,"Unable to connect to PlotDirector."),En(m,"Select a project first"),xr("Unable to connect to PlotDirector."),Pr("Unable to connect to PlotDirector."),vn(e,null),vn(t,null),On(),Rn("Unable to connect to PlotDirector.")}})():Promise.resolve();if(Ut||(En(U,"Please sign in to PlotDirector."),En(M,"Please sign in to PlotDirector."),En(h,"Please sign in to PlotDirector."),En(m,"Please sign in to PlotDirector."),On()),!window.Office||"function"!=typeof window.Office.onReady)return Lr("Word Companion ready"),Ar("Word document access is unavailable."),Jr("Manual"),void Cn({host:"Unavailable",platform:"Unavailable",ready:"No",wordApi:"No"});In().then(async()=>{Lr("Word Companion ready"),await Mo,await go(),Bt?(()=>{if(Ut){if(!window.Office?.context?.document||"function"!=typeof window.Office.context.document.addHandlerAsync||!window.Office?.EventType?.DocumentSelectionChanged)return Jr("Manual"),void Ar("Automatic scene tracking is unavailable in this Word version. Use Refresh Current Scene.");try{window.Office.context.document.addHandlerAsync(window.Office.EventType.DocumentSelectionChanged,Ro,e=>{e?.status===window.Office.AsyncResultStatus.Succeeded?(br=!0,Jr("Live")):(Jr("Manual"),Ar("Automatic scene tracking is unavailable in this Word version. Use Refresh Current Scene."))}),window.addEventListener("beforeunload",Wo)}catch(e){console.warn("Unable to register Word selection tracking handler.",e),Jr("Manual"),Ar("Automatic scene tracking is unavailable in this Word version. Use Refresh Current Scene.")}}})():(Ar("Word document access is unavailable."),Jr("Manual"))}).catch(e=>{console.error("Office readiness check failed.",e),xr("Unable to connect to PlotDirector."),Lr("Word Companion unavailable"),Ar("Word document access is unavailable."),Cn({ready:"No",wordApi:"No",lastScan:"Failure"})})})();