1import{r as i,a as Z,s as c,j as e,B as u,I as $,L as ee,bq as te,F as se}from"./index-KSWbwNeY.js";import{C as O}from"./card-CwL_ed79.js";import{L as y}from"./label-ndOKmXJw.js";import{A}from"./AdminNav-DsaLvjFx.js";import{R as re}from"./quill.snow-CYjwR_8r.js";import{p as ie}from"./purify.es--yiYCWnk.js";import{D as ae,e as oe,a as ne,b as le,c as ce}from"./dialog-Bpfij23O.js";import{P as z}from"./plus-oseq6Y0_.js";import{U as de}from"./upload-CstIFHrJ.js";import{C as me}from"./calendar-Cdsbx2VF.js";import{S as ue}from"./square-pen-CRmh_xc0.js";import{E as pe}from"./eye-off-DqR1XpTQ.js";import{E as he}from"./eye-Cwzmef_1.js";import{T as fe}from"./trash-2-CwcwS_bf.js";import{p as H}from"./parseISO-BHcD4hpi.js";import{f as G}from"./format-hhvI2L0z.js";import"./index-DsYLezIw.js";import"./user-cog-DvqSS7bY.js";import"./book-open-DS7Nmuzn.js";import"./receipt-DgWJf9Ou.js";import"./chevron-down-D7IdZFzc.js";import"./index-BpbyRUKB.js";import"./en-US-BOoww7nS.js";const xe={toolbar:[["bold","italic","underline"],[{list:"ordered"},{list:"bullet"}],["link"],["clean"]]},ge=["bold","italic","underline","list","bullet","link"],je=()=>new Promise((o,d)=>{if(window.pdfjsLib){o(window.pdfjsLib);return}const l=document.createElement("script");l.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js",l.onload=()=>{const x=window.pdfjsLib;x.GlobalWorkerOptions.workerSrc="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js",o(x)},l.onerror=()=>d(new Error("Failed to load PDF.js")),document.head.appendChild(l)}),be=async o=>{const d=await je(),l=await o.arrayBuffer(),N=await(await d.getDocument({data:l}).promise).getPage(1),r=N.getViewport({scale:2}),m=document.createElement("canvas");m.width=r.width,m.height=r.height;const g=m.getContext("2d");if(!g)throw new Error("Could not get canvas context");return await N.render({canvasContext:g,viewport:r}).promise,new Promise((C,j)=>{m.toBlob(b=>{b?C(b):j(new Error("Failed to create blob from canvas"))},"image/jpeg",.9)})},R=o=>{try{const d=H(`${o}-01`);return G(d,"MMMM yyyy")}catch{return o}},Re=()=>{const[o,d]=i.useState([]),[l,x]=i.useState(!0),[N,p]=i.useState(!1),[r,m]=i.useState(null),[g,C]=i.useState(!1),[j,b]=i.useState(!1),[T,D]=i.useState(""),[h,F]=i.useState(""),[q,M]=i.useState(!1),[k,U]=i.useState(null),[E,I]=i.useState(null),[B,S]=i.useState(null),{toast:a}=Z();i.useEffect(()=>{P()},[]);const P=async()=>{try{const{data:t,error:s}=await c.from("print_editions").select("*").order("issue_month",{ascending:!1});if(s)throw s;d(t||[])}catch(t){a({variant:"destructive",title:"Error fetching editions",description:t.message})}finally{x(!1)}},L=()=>{D(""),F(""),M(!1),U(null),I(null),S(null),m(null)},J=t=>{m(t),D(t.description||""),F(t.issue_month.slice(0,7)),M(t.is_published),S(t.cover_image_url),p(!0)},Q=i.useCallback(async t=>{var v;const s=(v=t.target.files)==null?void 0:v[0];if(s){I(s),b(!0);try{const f=await be(s);U(f);const _=URL.createObjectURL(f);S(_),a({title:"Cover extracted",description:"Cover image has been extracted from the PDF's first page."})}catch(f){console.error("Failed to extract PDF cover:",f),a({variant:"destructive",title:"Cover extraction failed",description:"Could not extract cover from PDF. Please try a different file."})}finally{b(!1)}}},[a]),V=async t=>{if(t.preventDefault(),!h){a({variant:"destructive",title:"Missing month",description:"Please select an issue month."});return}if(!r&&(!k||!E)){a({variant:"destructive",title:"Missing files",description:"Please upload a PDF file."});return}C(!0);try{let s=(r==null?void 0:r.cover_image_url)||"",v=(r==null?void 0:r.pdf_url)||"";const f=R(h);if(k){const n=`${Date.now()}-${Math.random().toString(36).substring(7)}.jpg`,{error:w}=await c.storage.from("print-covers").upload(n,k,{cacheControl:"public, max-age=31536000, immutable",contentType:"image/jpeg"});if(w)throw w;const{data:{publicUrl:Y}}=c.storage.from("print-covers").getPublicUrl(n);s=Y}if(E){const n=`${Date.now()}-${Math.random().toString(36).substring(7)}.pdf`,{error:w}=await c.storage.from("print-editions").upload(n,E,{cacheControl:"public, max-age=604800",contentType:"application/pdf"});if(w)throw w;v=n}
1const _={title:f,description:T||null,cover_image_url:s,pdf_url:v,issue_month:`${h}-01`,is_published:q};if(r){const{error:n}=await c.from("print_editions").update(_).eq("id",r.id);if(n)throw n;a({title:"Edition updated",description:"The print edition has been updated successfully."})}else{const{error:n}=await c.from("print_editions").insert(_);if(n)throw n;a({title:"Edition created",description:"The print edition has been created successfully."})}p(!1),L(),P()}catch(s){a({variant:"destructive",title:"Error saving edition",description:s.message})}finally{C(!1)}},W=async t=>{if(confirm("Are you sure you want to delete this print edition?"))try{const{error:s}=await c.from("print_editions").delete().eq("id",t);if(s)throw s;a({title:"Edition deleted",description:"The print edition has been deleted successfully."}),P()}catch(s){a({variant:"destructive",title:"Error deleting edition",description:s.message})}},K=async t=>{try{const{error:s}=await c.from("print_editions").update({is_published:!t.is_published}).eq("id",t.id);if(s)throw s;a({title:t.is_published?"Edition unpublished":"Edition published",description:`The edition is now ${t.is_published?"hidden from":"visible to"} subscribers.`}),P()}catch(s){a({variant:"destructive",title:"Error updating edition",description:s.message})}},X=t=>{try{return G(H(t),"MMMM yyyy")}catch{return t}};return l?e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(A,{}),e.jsx("main",{className:"md:ml-52 p-8",children:e.jsx("div",{className:"text-center",children:"Loading..."})})]}):e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(A,{}),e.jsx("main",{className:"md:ml-52 p-8",children:e.jsxs("div",{className:"max-w-7xl mx-auto",children:[e.jsxs("div",{className:"flex justify-between items-center mb-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"font-serif text-4xl font-bold",children:"Print Editions"}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[o.length," ",o.length===1?"edition":"editions"," total"]})]}),e.jsxs(ae,{open:N,onOpenChange:t=>{p(t),t||L()},children:[e.jsx(oe,{asChild:!0,children:e.jsxs(u,{children:[e.jsx(z,{className:"mr-2 h-4 w-4"}),"Add Edition"]})}),e.jsxs(ne,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsx(le,{children:e.jsx(ce,{children:r?"Edit Print Edition":"Add Print Edition"})}),e.jsxs("form",{onSubmit:V,className:"space-y-6 mt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(y,{htmlFor:"issueMonth",children:"Issue Month *"}),e.jsx($,{id:"issueMonth",type:"month",value:h,onChange:t=>F(t.target.value),required:!0}),h&&e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Title will be: ",e.jsx("span",{className:"font-medium",children:R(h)})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(y,{htmlFor:"description",children:"Description"}),e.jsx("div",{className:"prose-editor-container",children:e.jsx(re,{theme:"snow",value:T,onChange:D,modules:xe,formats:ge,placeholder:"Brief description of this issue's highlights...",className:"bg-background rounded-md"})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(y,{htmlFor:"pdf",children:["PDF File ",!r&&"*"]}),e.jsx($,{id:"pdf",type:"file",accept:".pdf",onChange:Q,required:!r,disabled:j}),r&&!E&&e.jsx("p",{className:"text-xs text-muted-foreground",children:"Leave empty to keep existing PDF"}),j&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(ee,{className:"h-4 w-4 animate-spin"}),"Extracting cover from PDF..."]}),B&&e.jsxs("div",{className:"mt-4",children:[e.jsx(y,{className:"text-sm text-muted-foreground mb-2 block",children:"Cover Preview (auto-extracted from PDF)"}),e.jsx("div",{className:"w-32 h-44 rounded-md overflow-hidden border bg-muted shadow-sm",children:e.jsx("img",{src:B,alt:"Cover preview",className:"w-full h-full object-cover"})})]})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(te,{id:"published",checked:q,onCheckedChange:M}),e.jsx(y,{htmlFor:"published",children:"Publish immediately"})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4",children:[e.jsx(u,{type:"button",variant:"outline",onClick:()=>{p(!1),L()},children:"Cancel"}),e.jsxs(u,{type:"submit",disabled:g||j,children:[e.jsx(de,{className:"mr-2 h-4 w-4"}),g?"Uploading...":r?"Update Edition":"Create Edition"]})]})]})]})]})]}),o.length===0?e.jsxs(O,{className:"p-12 text-center",children:[e.jsx(se,{className:"h-12 w-12 mx-auto text-muted-foreground mb-4"}),e.jsx("h3",{className:"text-lg font-semibold mb-2",children:"No print editions yet"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Upload your first print edition to make it available to subscribers."}),e.jsxs(u,{onClick:()=>p(!0),children:[e.jsx(z,{className:"mr-2 h-4 w-4"}),"Add First Edition"]})]}):e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6",children:o.map(t=>e.jsxs(O,{className:"overflow-hidden group",children:[e.jsxs("div",{className:"aspect-[3/4] relative bg-muted",children:[e.jsx("img",{src:t.cover_image_url,alt:t.title,className:"w-full h-full object-cover transition-transform duration-500 group-hover:scale-[1.03]"}),e.jsx("div",{className:"absolute top-2 right-2",children:e.jsx("span",{className:`px-2 py-1 text-xs font-medium rounded-full ${t.is_published?"bg-green-500/90 text-white":"bg-muted-foreground
1/90 text-white"}`,children:t.is_published?"Published":"Draft"})})]}),e.jsxs("div",{className:"p-4 space-y-3",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-lg line-clamp-1",children:X(t.issue_month)}),e.jsxs("p",{className:"text-sm text-muted-foreground flex items-center gap-1",children:[e.jsx(me,{className:"h-3 w-3"}),t.title]})]}),t.description&&e.jsx("div",{className:"text-sm text-muted-foreground line-clamp-2 prose prose-sm",dangerouslySetInnerHTML:{__html:ie.sanitize(t.description)}}),e.jsxs("div",{className:"flex gap-2 pt-2",children:[e.jsxs(u,{variant:"outline",size:"sm",className:"flex-1",onClick:()=>J(t),children:[e.jsx(ue,{className:"h-4 w-4 mr-1"}),"Edit"]}),e.jsx(u,{variant:"outline",size:"sm",onClick:()=>K(t),children:t.is_published?e.jsx(pe,{className:"h-4 w-4"}):e.jsx(he,{className:"h-4 w-4"})}),e.jsx(u,{variant:"outline",size:"sm",onClick:()=>W(t.id),children:e.jsx(fe,{className:"h-4 w-4 text-destructive"})})]})]})]},t.id))})]})}),e.jsx("style",{children:` 2 .prose-editor-container .ql-container { 3 min-height: 120px; 4 font-size: 14px; 5 } 6 .prose-editor-container .ql-editor { 7 min-height: 100px; 8 } 9 .prose-editor-container .ql-toolbar { 10 border-top-left-radius: 6px; 11 border-top-right-radius: 6px; 12 background: hsl(var(--muted)); 13 } 14 .prose-editor-container .ql-container { 15 border-bottom-left-radius: 6px; 16 border-bottom-right-radius: 6px; 17 } 18 `})]})};export{Re 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.