1import{_ as pe}from"./BodJxGTV.js";import{e as ve,h as he,B as fe,C as me,m as h,E as K,I as ye,i as ge,a7 as be,ad as xe,c as u,b as i,a,d as g,w as b,l as P,g as I,M as _e,t as A,f as ke,p as we,o as d,q as M,n as Ce,a4 as F,a1 as D,v as $e,a6 as L}from"./D3mXZbvm.js";import{d as q}from"./BwIT_Cxy.js";import{B as G}from"./CbyiM6aK.js";import{S as Be}from"./D0HDmefO.js";import{C as Se}from"./DmTYwAyA.js";import{s as W}from"./BF8o2BCO.js";import{_ as Ee}from"./DlAUqK2U.js";const ze={class:"min-h-screen bg-gray-50 py-12 px-4 sm:px-6 lg:px-8"},Ae={key:0,class:"max-w-4xl mx-auto"},Ve={class:"bg-white rounded-2xl shadow-sm p-8 mb-8"},Te={class:"text-3xl font-bold text-gray-900 text-center mb-4"},Oe={key:0},Pe={key:1},Ie={class:"flex justify-center mb-12"},Me={key:0},Ne={key:1},Ue={class:"space-y-4"},Re={class:"border rounded-xl overflow-hidden bg-white"},Ye={class:"flex items-center justify-between p-4 bg-gray-50 border-b"},je={class:"flex items-center flex-1 min-w-0"},Je=["onClick"],Ke={class:"drag-handle cursor-grab mr-3 text-gray-400"},Fe={key:0,class:"flex-1 mr-4"},De=["onUpdate:modelValue","onBlur","onKeyup"],Le={key:1,class:"text-lg font-bold text-gray-900 truncate flex-1"},qe={class:"flex items-center gap-2"},Ge=["onClick"],We=["onClick"],He={class:"p-4 space-y-2"},Qe={class:"flex items-center justify-between p-3 bg-white border rounded-lg group"},Xe={class:"flex items-center flex-1 min-w-0"},Ze={class:"sub-drag-handle cursor-grab mr-3 text-gray-300 opacity-0 group-hover:opacity-100 transition-opacity"},et={key:0,class:"flex-1 mr-4"},tt=["onUpdate:modelValue","onBlur","onKeyup"],ot={key:1,class:"text-gray-700 truncate flex-1"},at={class:"flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity"},rt=["onClick"],st=["onClick"],nt=["onClick"],lt={key:1,class:"max-w-xl mx-auto text-center mt-20"},it={class:"mb-8"},ut={class:"w-full bg-gray-200 rounded-full h-4 overflow-hidden"},dt={class:"text-2xl font-bold text-gray-900 mb-4"},ct={class:"mt-12"},pt={key:2,class:"max-w-xl mx-auto text-center mt-20"},vt=ve({__name:"index",setup(ht){const H=me(),V=ke(),p=he(),{request:v}=fe(),x=H.params.id,r=h(null),n=h(!1),C=h(!1),T=h(!1),$=h(null),m=h(null),y=h(null),B=h(null),S=h(null),E=h(new Set),Q=e=>{E.value.has(e)?E.value.delete(e):E.value.add(e)},N=e=>E.value.has(e),_=h(0);let k=null,z=null;const U=["We are generating your book's table of contents...","Just a moment, structuring your book...","Your book outline is almost ready!"],O=h(0),X=K(()=>U[O.value]),Z=K(()=>$.value===null?"0":new Intl.NumberFormat("en-US").format($.value)),f=async(e=!1,t=!0)=>{try{const l=await v(`/api/v3/books/${x}`,{headers:{Authorization:`Bearer ${p.token}`}});if(l.data){if(t)r.value=l.data;else{const w=new Map(r.value?.chapters?.map(o=>[o.id,o.title])||[]),s=new Map;if(r.value?.chapters?.forEach(o=>{o.subchapters?.forEach(c=>s.set(c.id,c.title))}),r.value=l.data,m.value&&w.has(m.value)){const o=r.value.chapters.find(c=>c.id===m.value);o&&(o.title=w.get(m.value))}y.value&&s.has(y.value)&&r.value.chapters.forEach(o=>{const c=o.subchapters?.find(ce=>ce.id===y.value);c&&(c.title=s.get(y.value))})}if(r.value.state==="done"){V.push("/my-books");return}r.value.state==="new"||r.value.state==="outline_in_progress"?ee():R()}}catch(l){console.error("Error fetching book:",l)}},ee=()=>{k||(_.value=0,k=W(()=>{_.value<95&&(_.value+=1)},2500))},R=()=>{k&&(clearInterval(k),k=null),_.value=100},te=()=>{z=W(()=>{f(!0),O.value=(O.value+1)%U.length},5e3)},Y=()=>{z&&(clearInterval(z),z=null)},oe=async()=>{n.value=!0,C.value=!0;try{let e;if(r.value.state==="draft"){if(e=await v(`/api/v3/books/${x}/finish_draft`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`}}),e.status===402||!e.ok){const t=await v("/api/v3/books/draft_cost",{method:"POST",headers:{Authorization:`Bearer ${p.token}`}});$.value=t.data?.cost}}else e=await v(`/api/v3/books/${x}/finalize`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`}}),$.value=r.value.preset?.creditCost;e.ok?V.push("/my-books"):e.status===402&&(T.value=!0,C.value=!1)}catch(e){console.error("Error finalizing book:",e),C.value=!1}finally{n.value=!1}},ae=async()=>{n.value=!0;try{await v(`/api/v3/books/${x}/reorder`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`},body:JSON.stringify({order:r.value.chapters})}),await f(!0)}catch(e){console.error("Error reordering chapters:",e)}finally{n.value=!1}},re=async e=>{n.value=!0;try{await v(`/api/v3/chapters/${e.id}/modifySubchapterOrder`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`},body:JSON.stringify({order:e.subchapters})}),await f(!0)}catch(t){console.error("Error reordering subchapters:",t)}finally{n.value=!1}},se=async()=>{n.value=!0;try{await v(`/api/v3/books/${x}/add`,{headers:{Authorization:`Bearer ${p.token}`}}),await f(!0)}catch(e){console.error("Error adding chapter:",e)}finally{n.value=!1}},ne=async e=>{if(confirm("Are you sure you want to remove this chapter?")){n.value=!0;try{await v(`/api/v3/chapters/${e.id}/remove`,{headers:{Authorization:`Bearer ${p.token}`}}),await f(!0)}catch(t){console.error("Error removing chapter:",t)}finally{n.value=!1}}},le=async e=>{n.value=!0;try{await v(`/api/v3/chapters/${e.id}/subchapter/add`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`}}),await f(!0)}catch(t){console.error("Error adding subchapter:",t)}finally{n.value=!1}},ie=async e=>{if(confirm("Are you sure you want to remove this subchapter?")){n.value=!0;try{await v(`/api/v3/subchapters/${e.id}/remove`,{headers:{Authorization:`Bearer ${p.token}`}}),await f(!0)}catch(t){console.error("Error removing subchapter:",t)}finally{n.value=!1}}},ue=async e=>{m.value=e.id,await L(),B.value&&B.value[0]&&B.value[0].focus()},j=async e=>{if(m.value===e.id){m.value=null,n.value=!0;try{await v(`/api/v3/chapters/${e.id}/modify`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`},body:JSON.stringify({title:e.title})}),await f(!0,!1)}catch(t){console.error("Error modifying chapter:",t)}finally{n.value=!1}}},de=async e=>{y.value=e.id,await L(),S.value&&S.value[0]&&S.value[0].focus()},J=async e=>{if(y.value===e.id){y.value=null,n.value=!0;try{await v(`/api/v3/subchapters/${e.id}/modify`,{method:"POST",headers:{Authorization:`Bearer ${p.token}`},body:JSON.stringify({title:e.title,content:""})}),await f(!0,!1)}catch(t){console.error("Error modifying subchapter:",t)}finally{n.value=!1}}};return ye(()=>r.value?.state,e=>{(e==="outline_completed"||e==="draft")&&Y()}),ge(()=>{f(),te()}),be(()=>{Y(),R()}),xe((e,t,l)=>{!C.value&&r.value?.state!=="done"?confirm("The generation hasn't started yet. Are you sure you want to leave the page? You can return to editing the outline on the My Books page.")?l():l(!1):l()}),(e,t)=>{const l=we("font-awesome-icon"),w=pe;return d(),u("div",ze,[r.value&&(r.value.state==="outline_completed"||r.value.state==="draft")?(d(),u("div",Ae,[a("div",Ve,[a("h1",Te,[t[3]||(t[3]=g(" Edit Your Outline Before ",-1)),r.value.state==="draft"?(d(),u("span",Oe,"Formatting")):(d(),u("span",Pe,"Generating")),t[4]||(t[4]=g(" (Optional) ",-1))]),t[7]||
1(t[7]=a("p",{class:"text-gray-600 text-center mb-8"}," Click the button below or edit the outline as you like. ",-1)),a("div",Ie,[i(G,{variant:"primary",class:"min-w-[200px]",onClick:oe,loading:n.value,disabled:n.value},{default:b(()=>[r.value.state==="outline_completed"?(d(),u("span",Me,"Finish generation")):(d(),u("span",Ne,"Confirm & Format"))]),_:1},8,["loading","disabled"])]),a("div",Ue,[i(I(q),{modelValue:r.value.chapters,"onUpdate:modelValue":t[0]||(t[0]=s=>r.value.chapters=s),handle:".drag-handle","item-key":"id",onEnd:ae,class:"space-y-4"},{item:b(({element:s})=>[a("div",Re,[a("div",Ye,[a("div",je,[a("button",{onClick:o=>Q(s.id),class:Ce(["p-2 mr-1 text-gray-400 hover:text-primary transition-transform duration-200",{"-rotate-90":!N(s.id)}])},[i(l,{icon:"chevron-down",class:"w-3 h-3"})],10,Je),a("span",Ke,[i(l,{icon:"bars",class:"w-4 h-4"})]),m.value===s.id?(d(),u("div",Fe,[M(a("input",{"onUpdate:modelValue":o=>s.title=o,onBlur:o=>j(s),onKeyup:F(o=>j(s),["enter"]),class:"w-full px-2 py-1 border rounded focus:ring-2 focus:ring-primary focus:border-transparent outline-none",ref_key:"chapterInput",ref:B},null,40,De),[[D,s.title]])])):(d(),u("h3",Le,A(s.title),1))]),a("div",qe,[a("button",{onClick:o=>ue(s),class:"p-2 text-gray-400 hover:text-primary transition-colors",title:"Edit Title"},[i(l,{icon:"pencil"})],8,Ge),r.value.chapters.length>1?(d(),u("button",{key:0,onClick:o=>ne(s),class:"p-2 text-gray-400 hover:text-red-500 transition-colors",title:"Remove Chapter"},[i(l,{icon:"trash"})],8,We)):P("",!0)])]),M(a("div",He,[i(I(q),{modelValue:s.subchapters,"onUpdate:modelValue":o=>
1s.subchapters=o,handle:".sub-drag-handle","item-key":"id",onEnd:o=>re(s),class:"space-y-2"},{item:b(({element:o})=>[a("div",Qe,[a("div",Xe,[a("span",Ze,[i(l,{icon:"bars",class:"w-3 h-3"})]),y.value===o.id?(d(),u("div",et,[M(a("input",{"onUpdate:modelValue":c=>o.title=c,onBlur:c=>J(o),onKeyup:F(c=>J(o),["enter"]),class:"w-full px-2 py-1 border rounded focus:ring-2 focus:ring-primary focus:border-transparent outline-none text-sm",ref_key:"subchapterInput",ref:S},null,40,tt),[[D,o.title]])])):(d(),u("span",ot,A(o.title),1))]),a("div",at,[a("button",{onClick:c=>de(o),class:"p-1.5 text-gray-400 hover:text-primary transition-colors"},[i(l,{icon:"pencil",class:"text-xs"})],8,rt),a("button",{onClick:c=>ie(o),class:"p-1.5 text-gray-400 hover:text-red-500 transition-colors"},[i(l,{icon:"trash",class:"text-xs"})],8,st)])])]),_:1},8,["modelValue","onUpdate:modelValue","onEnd"]),s.subchapters.length<5?(d(),u("button",{key:0,onClick:o=>le(s),class:"w-full py-2 px-4 border-2 border-dashed border-gray-200 rounded-lg text-gray-500 hover:border-primary hover:text-primary transition-all text-sm font-medium flex items-center justify-center gap-2"},[i(l,{icon:"plus",class:"text-xs"}),t[5]||(t[5]=g(" Add Subchapter ",-1))],8,nt)):P("",!0)],512),[[$e,N(s.id)]])])]),_:1},8,["modelValue"]),r.value.chapters.length<25?(d(),u("button",{key:0,onClick:se,class:"w-full py-4 px-6 border-2 border-dashed border-gray-300 rounded-xl text-gray-600 hover:border-primary hover:text-primary transition-all font-bold flex items-center justify-center gap-2"},[i(l,{icon:"plus"}),t[6]||(t[6]=g(" Add Chapter ",-1))])):P("",!0)])])])):r.value&&(r.value.state==="new"||r.value.state==="outline_in_progress")?(d(),u("div",lt,[a("div",it,[a("div",ut,[a("div",{class:"h-full bg-primary transition-all duration-300 ease-linear",style:_e({width:_.value+"%"})},null,4)])]),a("h2",dt,A(X.value),1),t[8]||(t[8]=a("p",{class:"text-gray-600"},"Please wait while we structure your masterpiece...",-1)),a("div",ct,[i(Be,{size:"lg",color:"primary"})])])):(d(),u("div",pt,[t[10]||(t[10]=a("h2",{class:"text-2xl font-bold text-gray-900 mb-4"},"Your book is being prepared",-1)),t[11]||(t[11]=a("p",{class:"text-gray-600 mb-8"}," You can check the status and manage your books in the dashboard. ",-1)),i(w,{to:"/my-books"},{default:b(()=>[i(G,{variant:"primary"},{default:b(()=>[...t[9]||(t[9]=[g("Go to My Books",-1)])]),_:1})]),_:1})])),i(Se,{modelValue:T.value,"onUpdate:modelValue":t[1]||(t[1]=s=>T.value=s),title:"Not enough credit",confirmText:"Pricing",cancelText:"Cancel",onConfirm:t[2]||(t[2]=s=>I(V).push("/pricing"))},{default:b(()=>[g(" You don't have enough credit ("+A(Z.value)+") to finish this book. Please recharge your credits. ",1)]),_:1},8,["modelValue"])])}}}),wt=Ee(vt,[["__scopeId","data-v-b27c24d5"]]);export{wt as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.