1import{r as p,j as n}from"./react-vendor-D653VjAu.js";import{i as $,S as k,D as A,N as u,L as N,F as E,f as C,P as I,a as S,b as B,c as M,s as R}from"./toolkit-C5LkFRN3.js";import{w as v,f as T,a as L}from"./pageops-dJQS7a6V.js";const y=["A","B","C","D","E","F"],q=({showNotification:i,showDownloadDialog:g})=>{const[a,d]=p.useState([]),[m,f]=p.useState(!1),x=a.reduce((e,s)=>e+s.pages,0),j=a.some(e=>e.encrypted),w=a.length>1&&new Set(a.map(e=>e.pages)).size>1,z=p.useCallback(async e=>{const s=await v(),o=[];for(const t of e)if($(t))try{const r=await t.arrayBuffer(),l=await s.PDFDocument.load(r,{ignoreEncryption:!0,updateMetadata:!1});o.push({name:t.name,size:t.size,bytes:r,pages:l.getPageCount(),reverse:!1,encrypted:!!l.isEncrypted,thumb:null})}catch{i(`Could not read ${t.name} â skipped`,"error")}o.length&&d(t=>[...t,...o])},[i]);p.useEffect(()=>{let e=!0;return a.forEach((s,o)=>{s.thumb===null&&T(s.bytes,120).then(t=>{e&&d(r=>r.map((l,c)=>c===o?{...l,thumb:t||!1}:l))})}),()=>{e=!1}},[a]);const P=e=>d(s=>s.map((o,t)=>t===e?{...o,reverse:!o.reverse}:o)),F=e=>d(s=>s.filter((o,t)=>t!==e)),b=(e,s)=>d(o=>{const t=[...o],r=e+s;return r<0||r>=t.length?o:([t[e],t[r]]=[t[r],t[e]],t)}),h=p.useMemo(()=>{if(a.length<2)return[];const e=a.map(t=>{const r=Array.from({length:t.pages},(l,c)=>c);return t.reverse?r.reverse():r}),s=Math.max(0,...e.map(t=>t.length)),o=[];for(let t=0;t<s;t++)for(let r=0;r<a.length;r++)t>=e[r].length||o.push(`${y[r]||r+1}${e[r][t]+1}`);return o},[a]),D=async()=>{if(a.length<2){i("Add at least two PDFs to interleave","error");return}f(!0);try{const e=await v(),s=[];for(const c of a)s.push({doc:await e.PDFDocument.load(c.bytes,{ignoreEncryption:!0,updateMetadata:!1}),reverse:c.reverse});const t=await(await L(e,s)).save({useObjectStreams:!1}),r=new Blob([t],{type:"application/pdf"}),l="alternated.pdf";R(r,l),g&&g(l,r.size),i(`Interleaved ${x} pages from ${a.length} files`,"success")}catch(e){i("Could not interleave: "+(e.message||e),"error")}finally{f(!1)}};return n.jsx(k,{name:"Alternate & Mix PDF",h1:"Alternate & Mix PDF Pages",heading:a.length?"Interleave order":"Combine Two Scans, Page by Page",icon:n.jsx(M,{className:"w-4 h-4 sm:w-5 sm:h-5 text-zinc-300"}),showH1:!a.length,children:n.jsxs("div",{className:"space-y-4",children:[n.jsx(A,{id:"altmix-upload",onFiles:z,multiple:!0,compact:a.length>0,title:a.length?"Add another PDF":"Select PDF Files",hint:"One page is taken from each file in turn ⢠Nothing is uploaded"}),j&&n.jsx(u,{tone:"warn",title:"One or more files are encrypted",icon:n.jsx(N,{className:"w-4 h-4"}),children:"Pages may fail to copy while locked. Run those through Unlock PDF first."}),a.length>0&&n.jsx("div",{className:"space-y-2",children:a.map((e,s)=>n.jsx(E,{name:`${y[s]||s+1} · ${e.name}`,thumb:e.thumb?e.thumb.url:void 0,meta:`${e.pages} page${e.pages!==1?"s":""} ⢠${C(e.size)}${e.reverse?" ⢠reversed":""}`,onRemove:()=>F(s),children:n.jsxs("div",{className:"flex items-center gap-0.5 flex-shrink-0",children:[n.jsx("button",{onClick:()=>b(s,-1),disabled:s===0,"aria-label":`Move ${e.name} up`,className:"p-1 text-zinc-500 hover:text-zinc-200 disabled:opacity-25",children:"â²"}),n.jsx("button",{onClick:()=>b(s,1),disabled:s===a.length-1,"aria-label":`Move ${e.name} down`,className:"p-1 text-zinc-500 hover:text-zinc-200 disabled:opacity-25",children:"â¼"}),n.jsx("button",{onClick:()=>P(s),"aria-pressed":e.reverse,"aria-label":`${e.reverse?"Use original order":"Reverse page order"} for ${e.name}`,className:`text-[11px] px-2 py-1 rounded-lg border transition-colors ml-1 ${e.reverse?"bg-zinc-100 text-zinc-900 border-zinc-100":"bg-zinc-900 text-zinc-400 border-zinc-800 hover:border-zinc-700"}`,children:"Reverse"})]})},s))}),h.length>0&&n.jsxs("div",{children:[n.jsxs("p",{className:"text-xs font-medium text-zinc-400 mb-1.5",children:["Resulting order â ",h.length," pages"]}),n.jsxs("div",{className:"flex flex-wrap gap-1 bg-zinc-950 border border-zinc-800 rounded-xl p-3 max-h-28 overflow-y-auto",children:[h.slice(0,60).map((e,s)=>n.jsx("span",{className:"text-[11px] font-mono px-1.5 py-0.5 rounded bg-zinc-800 text-zinc-300",children:e},s)),h.length>60&&n.jsxs("span",{className:"text-[11px] text-zinc-600 px-1.5 py-0.5",children:["+",h.length-60," more"]})]}),n.jsxs("p",{className:"text-[11px] text-zinc-600 mt-1.5",children:["A1 means page 1 of file A. Fronts in order plus backs reversed should read A1 B",n.jsx("em",{children:"n"})," A2 B",n.jsx("em",{children:"n-1"})," â¦"]})]}),w&&n.jsx(u,{title:"These files have different page counts",children:"That is fine â when one runs out the rest carry on, so the remaining pages are appended in order. If a duplex scan came out uneven, the last sheet was probably blank on the back."}),a.length===1&&n.jsx(u,{title:"Add one more PDF",children:"Interleaving needs at least two files. Add the second stack â usually the backs of a double-sided scan."}),m&&n.jsx(I,{label:"Interleaving pagesâ¦",done:0,total:0}),n.jsxs(S,{onClick:D,disabled:m||a.length<2,children:[n.jsx(B,{className:"w-4 h-4"}),m?"Interleavingâ¦":a.length<2?"Add at least two PDFs":`Interleave ${x} pages & download`]})]})})};export{q 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.