1import{j as e}from"./ui-wHuRdSZa.js";import{j as Y,r as w}from"./vendor-DF7Vsqu4.js";import{c as V,b as v}from"./query-DKV4sgHL.js";import{T as G,H as W,B as n,C as A,av as Q,aw as _,d as S,aj as J,ak as X,al as Z,am as ee,Q as m}from"./index-DIDqu_8B.js";import{B as E}from"./badge-DyaILpYp.js";import{C as se}from"./checkbox-CsKtfg4y.js";import{I}from"./input-xAyDpLjP.js";import{L as f}from"./label-CmZbH-a6.js";import{S as te,a as re,b as ae,c as ie,d as ne}from"./select-DJ5rNCZ_.js";import{T as O}from"./textarea-D6KwOzMA.js";import{P as M}from"./plus-BRsHE_0W.js";import{A as oe,a as de}from"./arrow-up-B6tle33q.js";import{P as le}from"./pencil-DtInu9Kq.js";import{T}from"./trash-2-DULGHVoV.js";import"./charts-BAVYBguD.js";import"./supabase-Cgbt6YLr.js";import"./motion-Clpufeat.js";import"./chevron-up-CKkK2w9o.js";const C={id:"",name:"",description:""},P={id:"",categoryId:"",question:"",answer:"",showOnHomepage:!1};function ce(h){return h.toLowerCase().trim().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")}function _e(){const[h]=Y(),{toast:o}=G(),j=V(),{data:a=[],isLoading:H}=W({includeEmpty:!0}),[d,u]=w.useState(C),[L,g]=w.useState(!1),[l,c]=w.useState(P),k=w.useMemo(()=>{const s=(h.get("search")||"").trim().toLowerCase();return s?a.map(t=>({...t,items:t.items.filter(r=>`${r.question} ${r.answer}`.toLowerCase().includes(s))})).filter(t=>t.name.toLowerCase().includes(s)||t.items.length>0):a},[a,h]),b=v({mutationFn:async s=>{const t=s.name.trim(),r=s.description.trim()||null,i=ce(t);if(!t||!i)throw new Error("Category name is required.");if(s.id){const{error:p}=await m.from("faq_categories").update({name:t,slug:i,description:r}).eq("id",s.id);if(p)throw p;return}const{error:x}=await m.from("faq_categories").insert({name:t,slug:i,description:r,sort_order:a.length});if(x)throw x},onSuccess:()=>{j.invalidateQueries({queryKey:["faq-categories"]}),u(C),o({title:"Category saved"})},onError:s=>{o({title:"Unable to save category",description:s.message,variant:"destructive"})}}),q=v({mutationFn:async s=>{const t=s.question.trim(),r=s.answer.trim();if(!s.categoryId)throw new Error("Choose a category first.");if(!t)throw new Error("Question is required.");if(s.id){const{error:i}=await m.from("faq_items").update({category_id:s.categoryId,question:t,answer:r,show_on_homepage:s.showOnHomepage}).eq("id",s.id);if(i)throw i}else{const i=a.find(y=>y.id===s.categoryId),x=i?i.items.length:0,{error:p}=await m.from("faq_items").insert({category_id:s.categoryId,question:t,answer:r,show_on_homepage:s.showOnHomepage,sort_order:x});if(p)throw p}},onSuccess:()=>{j.invalidateQueries({queryKey:["faq-categories"]}),c(P),g(!1),o({title:"FAQ saved"})},onError:s=>{o({title:"Unable to save FAQ",description:s.message,variant:"destructive"})}}),z=v({mutationFn:async s=>{const{error:t}=await m.from("faq_categories").delete().eq("id",s);if(t)throw t},onSuccess:()=>{j.invalidateQueries({queryKey:["faq-categories"]}),d.id&&u(C),o({title:"Category deleted"})},onError:s=>{o({title:"Unable to delete category",description:s.message,variant:"destructive"})}}),U=v({mutationFn:async s=>{const{error:t}=await m.from("faq_items").delete().eq("id",s.id);if(t)throw t},onSuccess:()=>{j.invalidateQueries({queryKey:["faq-categories"]}),o({title:"FAQ deleted"})},onError:s=>{o({title:"Unable to delete FAQ",description:s.message,variant:"destructive"})}}),N=v({mutationFn:async({index:s,direction:t})=>{const r=[...a],i=s+t,[x]=r.splice(s,1);r.splice(i,0,x);const y=(await Promise.all(r.map((F,B)=>m.from("faq_categories").update({sort_order:B}).eq("id",F.id)))).find(F=>F.error);if(y?.error)throw y.error},onSuccess:()=>{j.invalidateQueries({queryKey:["faq-categories"]})},onError:s=>{o({title:"Unable to reorder categories",description:s.message,variant:"destructive"})}});w.useEffect(()=>{const s=h.get("faqId");if(!s)return;const t=a.flatMap(r=>r.items).find(r=>r.id===s);t&&D(t)},[a,h]);const D=s=>{g(!1),c({id:s.id,categoryId:s.category_id,question:s.question,answer:s.answer,showOnHomepage:s.show_on_homepage}),requestAnimationFrame(()=>g(!0))},$=()=>{c(P),g(!0)},K=s=>{window.confirm(s.items.length>0?`Delete "${s.name}" and its ${s.items.length} FAQs?`:`Delete "${s.name}"?`)&&z.mutate(s.id)},R=s=>{window.confirm("Delete this FAQ?")&&U.mutate(s)};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl sm:text-3xl font-bold text-foreground",children:"FAQs"}),e.jsx("p",{className:"mt-2 max-w-3xl text-sm leading-6 text-muted-foreground",children:"Create FAQ categories, control homepage visibility, and keep the public FAQ page organised in the same order shown here."})]}),e.jsxs(n,{onClick:$,className:"bg-primary text-primary-foreground",children:[e.jsx(M,{className:"mr-2 h-4 w-4"})," Add FAQ"]})]}),e.jsxs("div",{className:"grid gap-6 xl:grid-cols-[0.9fr_1.1fr] min-w-0",children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs(A,{children:[e.jsx(Q,{children:e.jsx(_,{className:"text-xl",children:"Categories"})}),e.jsx(S,{className:"space-y-4",children:H?e.jsx("p",{className:"text-sm text-muted-foreground",children:"Loading c
1ategories..."}):a.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No categories yet â add your first one below."}):a.map((s,t)=>e.jsx("div",{className:"rounded-2xl border border-border bg-card p-4",children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"font-semibold text-foreground",children:s.name}),e.jsxs(E,{variant:"outline",children:[s.items.length," FAQ",s.items.length===1?"":"s"]})]}),s.description?e.jsx("p",{className:"mt-2 text-sm leading-6 text-muted-foreground",children:s.description}):null]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>N.mutate({index:t,direction:-1}),disabled:t===0||N.isPending,children:e.jsx(oe,{className:"h-4 w-4"})}),e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>N.mutate({index:t,direction:1}),disabled:t===a.length-1||N.isPending,children:e.jsx(de,{className:"h-4 w-4"})}),e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>u({id:s.id,name:s.name,description:s.description??""}),children:e.jsx(le,{className:"h-4 w-4"})}),e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>K(s),children:e.jsx(T,{className:"h-4 w-4"})})]})]})},s.id))})]}),e.jsxs(A,{children:[e.jsx(Q,{children:e.jsx(_,{className:"text-xl",children:d.id?"Edit category":"Add category"})}),e.jsxs(S,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(f,{htmlFor:"faq-category-name",children:"Category name"}),e.jsx(I,{id:"faq-category-name",value:d.name,onChange:s=>u(t=>({...t,name:s.target.value})),placeholder:"Planning your tour"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(f,{htmlFor:"faq-category-description",children:"Description"}),e.jsx(O,{id:"faq-category-description",value:d.description,onChange:s=>u(t=>({...t,description:s.target.value})),placeholder:"Short intro for this group of questions",rows:3})]}),e.jsxs("div",{className:"flex flex-wrap gap-3",children:[e.jsxs(n,{onClick:()=>b.mutate(d),disabled:b.isPending,className:"bg-primary text-primary-foreground",children:[e.jsx(M,{className:"mr-2 h-4 w-4"}),b.isPending?"Saving...":d.id?"Save category":"Add category"]}),d.id?e.jsx(n,{variant:"outline",onClick:()=>u(C),children:"Cancel"}):null]})]})]})]}),e.jsxs(A,{className:"min-w-0",children:[e.jsx(Q,{children:e.jsx(_,{className:"text-xl",children:"Published order"})}),e.jsx(S,{className:"space-y-5",children:k.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"Your FAQ page will populate as soon as you add categories and questions."}):k.map(s=>e.jsxs("section",{className:"rounded-2xl border border-border bg-card p-4",children:[e.jsxs("div",{className:"mb-4 flex items-center justify-between gap-3 border-b border-border pb-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-foreground",children:s.name}),s.description?e.jsx("p",{className:"text-sm text-muted-foreground",children:s.description}):null]}),e.jsxs(E,{variant:"secondary",children:[s.items.length," item",s.items.length===1?"":"s"]})]}),e.jsx("div",{className:"space-y-3",children:s.items.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No FAQs in this category yet."}):s.items.map(t=>e.jsx("div",{className:"cursor-pointer rounded-xl border border-border/80 px-4 py-3 transition-colors hover:bg-muted/50",onClick:()=>D(t),children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("p",{className:"font-medium text-foreground",children:t.question}),t.show_on_homepage?e.jsx(E,{variant:"outline",children:"Homepage"}):null]}),t.answer?e.jsx("p",{className:"mt-1 line-clamp-2 text-sm text-muted-foreground break-words",children:t.answer}):null]}),e.jsx(n,{variant:"ghost",size:"icon",onClick:r=>{r.stopPropagation(),R(t)},children:e.jsx(T,{className:"h-4 w-4"})})]})},t.id))})]},s.id))})]})]}),e.jsx(J,{open:L,onOpenChange:g,children:e.jsxs(X,{className:"sm:max-w-lg",children:[e.jsx(Z,{children:e.jsx(ee,{className:"font-body",children:l.id?"Edit FAQ":"Add FAQ"})}),e.jsxs("div",{className:"space-y-4 pt-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(f,{children:"Category"}),e.jsxs(te,{value:l.categoryId,onValueChange:s=>c(t=>({...t,categoryId:s})),children:[e.jsx(re,{children:e.jsx(ae,{placeholder:a.length===0?"Add a category first":"Choose a category"})}),e.jsx(ie,{children:a.map(s=>e.jsx(ne,{value:s.id,children:s.name},s.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(f,{htmlFor:"dialog-faq-question",children:"Question"}),e.jsx(I,{id:"dialog-faq-question",value:l.question,onChange:s=>c(t=>({...t,question:s.target.value})),placeholder:"What should guests bring?"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(f,{htmlFor:"dialog-faq-answer",children:"Answer"}),e.jsx(O,{id:"dialog-faq-answer",value:l.answer,onChange:s=>c(t=>({...t,answer:s.target.value})),placeholder:"Share the full answer shown on the FAQ page",rows:5})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(f,{children:"Display on"}),e.jsx("div",{className:"space-y-2 rounded-xl border border-border p-3",children:e.jsxs("label",{className:"flex items-center gap-3",children:[e.jsx(se,{checked:l.showOnHomepage,onCheckedChange:s=>c(t=>({...t,showOnHomepage:s===!0}))}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:"Homepage"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Show in the homepage FAQ section"})]})]})})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-2",children:[e.jsx(n,{variant:"outline",onClick:()=>g(!1),children:"Cancel"}),e.jsx(n,{onClick:()=>q.mutate(l),disabled:q.isPending||a.length===0,className:"bg-primary text-primary-foreground",children:q.isPending?"Saving...":l.id?"Save FAQ":"Add FAQ"})]})]})]})})]})}export{_e 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.