1import{j as e}from"./jsx-runtime-0DLF9kdB.js";import{r as l}from"./index-DH-tsESH.js";import{u as D}from"./platform-context-pZZ_Wbof.js";import{L as f}from"./linear-icon-C4MoDyVR.js";import I from"./noto-sans-latin-400-normal-DgXudELe.js";import F from"./noto-sans-latin-ext-400-normal-C23Ialtn.js";import _ from"./noto-sans-cyrillic-400-normal-BviOoXf6.js";function U(t){const n=[];for(let r=0;r<t.length;r++){const p=t[r],s={...p,depth:0,children:[]};let i=!1;for(let c=r-1;c>=0;c--){const u=t[c];if(u.level<p.level){const d=o=>{for(const a of o){if(a.id===u.id)return s.depth=a.depth+1,a.children.push(s),!0;if(d(a.children))return!0}return!1};d(n),i=!0;break}}i||n.push(s)}return n}function B({item:t,onToggle:n,onRename:r,onLevelUp:p,onLevelDown:s,onDelete:i}){const[c,u]=l.useState(!1),[d,o]=l.useState(t.text),a=l.useRef(null);l.useEffect(()=>{c&&a.current&&(a.current.focus(),a.current.select())},[c]);const h=l.useCallback(()=>{o(t.text),u(!0)},[t.text]),x=l.useCallback(()=>{d.trim()&&r(d.trim()),u(!1)},[d,r]),y=l.useCallback(v=>{v.key==="Enter"?x():v.key==="Escape"&&(o(t.text),u(!1))},[x,t.text]);return e.jsxs("div",{className:"toc-tree-item",style:{paddingLeft:t.depth*20+8,opacity:t.enabled?1:.45},children:[e.jsxs("label",{className:"toc-tree-item-checkbox",onClick:v=>v.stopPropagation(),children:[e.jsx("input",{type:"checkbox",checked:t.enabled,onChange:n}),e.jsx("span",{className:"toc-tree-checkbox-visual"})]}),e.jsx("div",{className:"toc-tree-item-body",onDoubleClick:h,children:c?e.jsx("input",{ref:a,className:"toc-tree-item-input",value:d,onChange:v=>o(v.target.value),onBlur:x,onKeyDown:y}):e.jsx("span",{className:"toc-tree-item-text",title:t.text,children:t.text})}),e.jsxs("div",{className:"toc-tree-item-level-badge",children:["H",t.level]}),e.jsxs("div",{className:"toc-tree-item-page",children:["p.",t.pageIndex+1]}),e.jsxs("div",{className:"toc-tree-item-actions",children:[e.jsx("button",{type:"button",className:"toc-tree-btn",onClick:p,title:"Promote",disabled:t.level<=1,children:e.jsx(f,{name:"chevron-left",size:12})}),e.jsx("button",{type:"button",className:"toc-tree-btn",onClick:s,title:"Demote",disabled:t.level>=3,children:e.jsx(f,{name:"chevron-right",size:12})}),e.jsx("button",{type:"button",className:"toc-tree-btn toc-tree-btn-danger",onClick:i,title:"Remove",children:e.jsx(f,{name:"x",size:12})})]})]})}function z(t,n,r){const p=[];for(const s of t){const i=()=>{r(n.map(a=>a.id===s.id?{...a,enabled:!a.enabled}:a))},c=a=>{r(n.map(h=>h.id===s.id?{...h,text:a}:h))},u=()=>{r(n.map(a=>a.id===s.id&&a.level>1?{...a,level:a.level-1}:a))},d=()=>{r(n.map(a=>a.id===s.id&&a.level<3?{...a,level:a.level+1}:a))},o=()=>{r(n.filter(a=>a.id!==s.id))};p.push(e.jsx(B,{item:s,onToggle:i,onRename:c,onLevelUp:u,onLevelDown:d,onDelete:o},s.id)),s.children.length>0&&p.push(...z(s.children,n,r))}return p}function O({headers:t,onChange:n}){const[r,p]=l.useState(""),[s,i]=l.useState(1),[c,u]=l.useState(!1),[d,o]=l.useState(""),a=t.filter(m=>m.text.toLowerCase().includes(d.toLowerCase())),h=U(a),x=l.useCallback(()=>{if(!r.trim())return;const m={id:`hdr-new-${Date.now()}`,text:r.trim(),pageIndex:Math.max(0,s-1),y:0,level:1,enabled:!0};n([...t,m]),p(""),i(1),u(!1)},[r,s,t,n]),y=l.useCallback(()=>{const m=new Set(a.map(b=>b.id));n(t.map(b=>m.has(b.id)?{...b,enabled:!0}:b))},[t,a,n]),v=l.useCallback(()=>{const m=new Set(a.map(b=>b.id));n(t.map(b=>m.has(b.id)?{...b,enabled:!1}:b))},[t,a,n]),k=z(h,t,n);return e.jsxs("div",{className:"toc-tree-root",children:[e.jsxs("div",{className:"toc-tree-toolbar",children:[e.jsxs("span",{className:"toc-tree-count",children:[d?`${a.length} of `:"",t.length," heading",t.length!==1?"s":""]}),e.jsxs("div",{className:"toc-tree-toolbar-actions",children:[e.jsxs("button",{type:"button",className:"toc-tree-toolbar-btn",onClick:y,children:[e.jsx(f,{name:"check",size:12})," All"]}),e.jsxs("button",{type:"button",className:"toc-tree-toolbar-btn",onClick:v,children:[e.jsx(f,{name:"x",size:12})," None"]}),e.jsxs("button",{type:"button",className:"toc-tree-toolbar-btn",onClick:()=>u(!c),children:[e.jsx(f,{name:"plus",size:12})," Add"]})]})]}),e.jsx("div",{className:"toc-tree-filter",style:{padding:"0 8px 10px"},children:e.jsx("input",{type:"text",className:"toc-tree-filter-input",style:{width:"100%",padding:"6px 10px",borderRadius:"4px",border:"1px solid var(--border)",background:"var(--bg)",color:"var(--text)",fontSize:"12.5px",outline:"none"},placeholder:"Filter headings...",value:d,onChange:m=>o(m.target.value)})}),c&&e.jsxs("div",{className:"toc-tree-add-form",children:[e.jsx("input",{className:"toc-tree-add-input",placeholder:"Heading text...",value:r,onChange:m=>p(m.target.value),onKeyDown:m=>{m.key==="Enter"&&x()}}),e.jsxs("div",{className:"toc-tree-add-row",children:[e.jsxs("label",{className:"toc-tree-add-label",children:["Page:",e.jsx("input",{type:"number",className:"toc-tree-add-page-input",min:1,value:s,onChange:m=>i(Math.max(1,parseInt(m.target.value,10)||1))})]}),e.jsx("button",{type:"button",className:"toc-tree-toolbar-btn toc-tree-add-btn",onClick:x,children:"Add"})]})]}),e.jsx("div",{className:"toc-tree-list custom-scrollbar",children:h.length===0?e.jsx("div",{className:"toc-tree-empty",children:d?"No headings match your filter.":"No headings detected. Try adjusting detection settings or add a heading manually."}):k})]})}function L({headers:t,bodyTextSize:n,headingCandidatesFound:r,totalSpansExtracted:p,onHeadersChange:s,onApply:i,onBack:c,isProcessing:u}){const[d,o]=l.useState(!1),a=t.filter(x=>x.enabled).length,h=l.useCallback(()=>{i({headers:t.filter(x=>x.enabled),generateTocPage:d})},[t,i,d]);return e.jsxs("div",{className:"tool-config-root toc-review-root",children:[e.jsxs("div",{className:"toc-review-header",children:[e.jsxs("div",{className:"toc-review-header-top",children:[e.jsx("h3",{className:"toc-review-title",children:"Review Headings"}),e.jsxs("div",{className:"toc-review-stats",children:[e.jsxs("div",{className:"toc-review-stat",children:[e.jsx("span",{className:"toc-review-stat-value",children:r}),e.jsx("span",{className:"toc-review-stat-label",children:"found"})]}),e.jsxs("div",{className:"toc-review-stat",children:[e.jsx("span",{className:"toc-review-stat-value",children:a}),e.jsx("span",{className:"toc-review-stat-label",children:"enabled"})]}),n!==null&&e.jsxs("div",{className:"toc-review-stat",children:[e.jsxs("span",{className:"toc-review-stat-value",children:[n.toFixed(1),"pt"]}),e.jsx("span",{className:"toc-review-stat-label",children:"body"})]})]})]}),e.jsx("p",{className:"tool-config-copy",children:"Review detected headings, edit text, adjust levels, and remove noise. Only checked items will be included."})]}),e.jsx("div",{className:"toc-review-body",children:e.jsx(O,{headers:t,onChange:s})}),e.jsxs("label",{className:"toc-option-row",children:[e.jsx("input",{type:"checkbox",checked:d,onChange:x=>o(x.target.checked),disabled:u}),e.jsxs("div",{className:"toc-option-text",children:[e.jsx("strong",{children:"Add Table of Contents page"}),e.jsx("span",{children:"Inserts a clickable TOC page at the beginning of the document"})]})]}),e.jsxs("div",{className:"tool-config-actions premium-actions toc-review-actions",children:[e.jsx("button",{className:"btn-ghost",onClick:c,disabled:u,children:e.jsxs("span",{className:"btn-inline",children:[e.jsx(f,{name:"x",className:"linear-icon"}),"Back"]})}),e.jsxs("div",{className:"toc-review-actions-right",children:[e.jsxs("span",{className:"toc-review-action-hint",children:[a," of ",t.length," heading",t.length!==1?"s":""]}),e.jsx("button",{className:"btn-primary btn-premium-glow",onClick:h,disabled:u||a===0,children:e.jsxs("span",{className:"btn-inline",children:[e.jsx(f,{name:"check",className:"linear-icon"}),u?"Applying...":"Apply to PDF"]})})]})]})]})}async function H(t,n,r){const p={id:crypto.randomUUID(),type:"COMMAND",payload:{type:"PROCESS_TOOL",payload:{toolId:"auto-toc",inputIds:[n],options:{action:"parse"}}}},s=await t.workerOrchestrator.dispatch(p,void 0,r);if(s.payload.type==="RESULT"){const i=s.payload.payload;if(i.outputIds.length>0){const c=i.outputIds[0],d=await(await t.vfs.read(c)).getText();try{const o=JSON.parse(d);return{headers:Array.isArray(o.headers)?o.headers:[],bodyTextSize:typeof o.bodyTextSize=="number"?o.bodyTextSize:null,totalSpansExtracted:typeof o.totalSpansExtracted=="number"?o.totalSpansExtracted:0,headingCandidatesFound:typeof o.headingCandidatesFound=="number"?o.headingCandidatesFound:0,totalPages:typeof o.totalPages=="number"?o.totalPages:void 0,error:typeof o.error=="string"?o.error:void 0}}catch{return{headers:[],bodyTextSize:null,totalSpansExtracted:0,headingCandidatesFound:0,error:"Failed to parse tool output"}}}return{headers:[],bodyTextSize:null,totalSpansExtracted:0,headingCandidatesFound:0,error:"No output from tool"}}return s.payload.type==="ERROR"?{headers:[],bodyTextSize:null,totalSpansExtracted:0,headingCandidatesFound:0,error:s.payload.payload.message}:{headers:[],bodyTextSize:null,totalSpansExtracted:0,headingCandidatesFound:0,error:"Unexpected worker response"}}var M="/app/assets/noto-sans-latin-700-normal-ZyrKABVF.woff",$="/app/assets/noto-sans-latin-ext-700-normal-CgsG9aVh.woff",K="/app/assets/noto-sans-cyrillic-700-normal-D8UNalU-.woff";function X({inputFiles:t,onStart:n,onBack:r}){const{runtime:p}=D(),[s,i]=l.useState("config"),[c,u]=l.useState(null),[d,o]=l.useState([]),[a,h]=l.useState(null),x=l.useRef(0),y=l.useRef(null);l.useEffect(()=>()=>{var j;return(j=y.current)==null?void 0:j.abort()},[]);const v=l.useCallback(async()=>{var w,C,T,E,A,R;if(t.length===0)return;(w=y.current)==null||w.abort();const j=++x.current,N=new AbortController;y.current=N,i("parsing"),h(null),(C=window.posthog)==null||C.capture("app_tool_run_started",{toolId:"auto-toc",action:"parse",fileCount:t.length});try{const g=await H(p,t[0],N.signal);if(j!==x.current)return;if(g.error){(T=window.posthog)==null||T.capture("app_tool_run_error",{toolId:"auto-toc",action:"parse",message:g.error}),h(g.error),i("config");return}(E=window.posthog)==null||E.capture("app_tool_run_success",{toolId:"auto-toc",action:"parse",headersFound:g.headingCandidatesFound,totalPages:g.totalPages});const P=p.billing.getContext().plan,S=g.totalPages??0;if(P==="basic"&&S>5){(A=window.posthog)==null||A.capture("app_upsell_shown",{toolId:"auto-toc",
1source:"page_limit",pageCount:S}),h(`Free plan limited to 5 pages (this document has ${S}). Upgrade to Pro for unlimited page TOC generation.`),i("config");return}u(g),o(g.headers),i("review")}catch(g){if(j!==x.current||g instanceof DOMException&&g.name==="AbortError")return;(R=window.posthog)==null||R.capture("app_tool_run_error",{toolId:"auto-toc",action:"parse",message:g instanceof Error?g.message:"Unknown"}),h(g instanceof Error?g.message:"Unknown error"),i("config")}},[t,p]),k=l.useCallback(j=>{o(j)},[]),m=l.useCallback(j=>{const N="/app/",w=`${N}fonts/Roboto-Regular.ttf`.replace(/\/+/g,"/"),C=`${N}fonts/Roboto-Bold.ttf`.replace(/\/+/g,"/");n({action:"apply",headers:j.headers,bodyTextSize:c==null?void 0:c.bodyTextSize,generateTocPage:j.generateTocPage,fontUrls:{latinUrl:I,latinExtUrl:F,cyrillicUrl:_,latinBoldUrl:M,latinExtBoldUrl:$,cyrillicBoldUrl:K,robotoUrl:w,robotoBoldUrl:C}})},[n,c]),b=l.useCallback(()=>{i("config"),u(null),o([]),h(null)},[]);return s==="parsing"?e.jsxs("div",{className:"tool-config-root autoc-root",children:[e.jsxs("div",{className:"autoc-header",style:{textAlign:"center",padding:"40px 0"},children:[e.jsx("div",{className:"autoc-spinner",style:{margin:"0 auto 16px",width:40,height:40,border:"3px solid var(--border)",borderTopColor:"var(--accent)",borderRadius:"50%",animation:"autoc-spin 0.8s linear infinite"}}),e.jsx("h3",{className:"autoc-title",children:"Analyzing Documentâ¦"}),e.jsx("p",{className:"tool-config-copy",children:"Extracting text layer and detecting headings."})]}),e.jsx("style",{children:"@keyframes autoc-spin { to { transform: rotate(360deg); } }"})]}):s==="review"&&c?e.jsx(L,{headers:d,bodyTextSize:c.bodyTextSize,headingCandidatesFound:c.headingCandidatesFound,totalSpansExtracted:c.totalSpansExtracted,onHeadersChange:k,onApply:m,onBack:b}):e.jsxs("div",{className:"tool-config-root autoc-root",children:[e.jsxs("div",{className:"autoc-header",children:[e.jsx("h3",{className:"autoc-title",children:"Auto-TOC & Bookmarks"}),e.jsx("p",{className:"tool-config-copy",children:"Automatically scan your PDF for headings, then generate an interactive table of contents with bookmarks for easy navigation."})]}),e.jsxs("div",{className:"autoc-features",children:[e.jsxs("div",{className:"autoc-feature",children:[e.jsx(f,{name:"ocr",className:"linear-icon icon-md"}),e.jsxs("div",{children:[e.jsx("strong",{children:"Smart Detection"}),e.jsx("p",{children:"Finds headings by analyzing font size, weight, and structure."})]})]}),e.jsxs("div",{className:"autoc-feature",children:[e.jsx(f,{name:"signature",className:"linear-icon icon-md"}),e.jsxs("div",{children:[e.jsx("strong",{children:"PDF Bookmarks"}),e.jsx("p",{children:"Adds interactive navigation to your document outline."})]})]}),e.jsxs("div",{className:"autoc-feature",children:[e.jsx(f,{name:"edit",className:"linear-icon icon-md"}),e.jsxs("div",{children:[e.jsx("strong",{children:"Edit & Refine"}),e.jsx("p",{children:"Review detected headings, adjust levels, and customize the final output."})]})]})]}),a&&e.jsxs("div",{className:"autoc-error",children:[e.jsx(f,{name:"x",className:"linear-icon"}),e.jsx("span",{children:a})]}),t.length>0&&e.jsxs("div",{className:"autoc-file-info",children:[e.jsx(f,{name:"file-input",className:"linear-icon"}),e.jsxs("span",{children:[t.length," file(s) ready for analysis"]})]}),e.jsxs("div",{className:"tool-config-actions premium-actions",children:[e.jsx("button",{className:"btn-ghost",onClick:r,children:e.jsxs("span",{className:"btn-inline",children:[e.jsx(f,{name:"x",className:"linear-icon"}),"Cancel"]})}),e.jsx("button",{className:"btn-primary btn-premium-glow",onClick:v,disabled:t.length===0,children:e.jsxs("span",{className:"btn-inline",children:[e.jsx(f,{name:"play",className:"linear-icon"}),"Analyze Document"]})})]})]})}export{X 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.