1import{r as i,a as z,e as T,u as D,j as e,P as B,b as W,p as Z,h as _,n as $,o as X,i as v,H as G}from"./index-Bl0kFVZR.js";import{H as J,B as N,F as K}from"./Footer-BdYKadnI.js";import{I as b}from"./input-DdVaxybK.js";import{L as p}from"./label-u8Zn_6rP.js";import{T as E}from"./textarea-BYEyhReL.js";import{s as C}from"./client-CG1hgB_T.js";function Q(s){const a=i.useRef({value:s,previous:s});return i.useMemo(()=>(a.current.value!==s&&(a.current.previous=a.current.value,a.current.value=s),a.current.previous),[s])}var k="Switch",[V,de]=z(k),[Y,ee]=V(k),F=i.forwardRef((s,a)=>{const{__scopeSwitch:o,name:t,checked:d,defaultChecked:j,required:g,disabled:u,value:h="on",onCheckedChange:w,form:m,...c}=s,[x,f]=i.useState(null),r=T(a,S=>f(S)),n=i.useRef(!1),l=x?m||!!x.closest("form"):!0,[y,q]=D({prop:d,defaultProp:j??!1,onChange:w,caller:k});return e.jsxs(Y,{scope:o,checked:y,disabled:u,children:[e.jsx(B.button,{type:"button",role:"switch","aria-checked":y,"aria-required":g,"data-state":I(y),"data-disabled":u?"":void 0,disabled:u,value:h,...c,ref:r,onClick:W(s.onClick,S=>{q(O=>!O),l&&(n.current=S.isPropagationStopped(),n.current||S.stopPropagation())})}),l&&e.jsx(A,{control:x,bubbles:!n.current,name:t,value:h,checked:y,required:g,disabled:u,form:m,style:{transform:"translateX(-100%)"}})]})});F.displayName=k;var M="SwitchThumb",L=i.forwardRef((s,a)=>{const{__scopeSwitch:o,...t}=s,d=ee(M,o);return e.jsx(B.span,{"data-state":I(d.checked),"data-disabled":d.disabled?"":void 0,...t,ref:a})});L.displayName=M;var te="SwitchBubbleInput",A=i.forwardRef(({__scopeSwitch:s,control:a,checked:o,bubbles:t=!0,...d},j)=>{const g=i.useRef(null),u=T(g,j),h=Q(o),w=Z(a);return i.useEffect(()=>{const m=g.current;if(!m)return;const c=window.HTMLInputElement.prototype,f=Object.getOwnPropertyDescriptor(c,"checked").set;if(h!==o&&f){const r=new Event("click",{bubbles:t});f.call(m,o),m.dispatchEvent(r)}},[h,o,t]),e.jsx("input",{type:"checkbox","aria-hidden":!0,defaultChecked:o,...d,tabIndex:-1,ref:u,style:{...d.style,...w,position:"absolute",pointerEvents:"none",opacity:0,margin:0}})});A.displayName=te;function I(s){return s?"checked":"unchecked"}var H=F,re=L;const U=i.forwardRef(({className:s,...a},o)=>e.jsx(H,{className:_("peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50",s),...a,ref:o,children:e.jsx(re,{className:_("pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-5 data-[state=unchecked]:translate-x-0")})}));U.displayName=H.displayName;const P=s=>s.toLowerCase().trim().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,""),R=s=>{const a=s.trim().split(/\s+/).filter(Boolean).length;return Math.max(1,Math.round(a/220))},se={title:"",slug:"",excerpt:"",cover_image_url:"",content:"",author_name:"New Zealand Property Buyers",category:"",tags:"",published:!1},ue=()=>{const{id:s}=$(),a=!s||s==="new",o=X(),[t,d]=i.useState(se),[j,g]=i.useState(!a),[u,h]=i.useState(!1),[w,m]=i.useState(!1);i.useEffect(()=>{(async()=>{const{data:{session:r}}=await C.auth.getSession();if(!r){o("/admin/login",{replace:!0});return}if(!a&&s){const{data:n,error:l}=await C.from("articles").select("*").eq("id",s).maybeSingle();if(l||!n){v.error("Article not found"),o("/admin/articles",{replace:!0});return}d({title:n.title,slug:n.slug,excerpt:n.excerpt||"",cover_image_url:n.cover_image_url||"",content:n.content,author_name:n.author_name,category:n.category||"",tags:(n.tags||[]).join(", "),published:n.published}),m(!0),g(!1)}})()},[s,a,o]);const c=r=>d(n=>({...n,...r})),x=r=>{c({title:r,...w?{}:{slug:P(r)}})},f=async()=>{if(!t.title.trim()||!t.content.trim()||!t.slug.trim()){v.error("Title, slug, and content are required.");return}h(!0);const r=t.tags.split(",").map(l=>
1l.trim()).filter(Boolean),n={title:t.title.trim(),slug:t.slug.trim(),excerpt:t.excerpt.trim()||null,cover_image_url:t.cover_image_url.trim()||null,content:t.content,author_name:t.author_name.trim()||"New Zealand Property Buyers",category:t.category.trim()||null,tags:r,reading_minutes:R(t.content),published:t.published,published_at:t.published?new Date().toISOString():null};if(a){const{error:l}=await C.from("articles").insert(n);if(h(!1),l){v.error(l.message);return}v.success("Article created"),o("/admin/articles")}else{const{error:l}=await C.from("articles").update(n).eq("id",s);if(h(!1),l){v.error(l.message);return}v.success("Article saved")}};return j?e.jsx("div",{className:"min-h-screen bg-background"}):e.jsxs("div",{className:"min-h-screen flex flex-col",children:[e.jsxs(G,{children:[e.jsx("title",{children:a?"New article":"Edit article"}),e.jsx("meta",{name:"robots",content:"noindex,nofollow"})]}),e.jsx(J,{}),e.jsx("main",{className:"flex-1 pt-48 lg:pt-56 pb-20",children:e.jsxs("div",{className:"container mx-auto px-4 max-w-3xl",children:[e.jsx("h1",{className:"font-display text-3xl font-bold mb-8",children:a?"New article":"Edit article"}),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx(p,{htmlFor:"title",children:"Title"}),e.jsx(b,{id:"title",value:t.title,onChange:r=>x(r.target.value),maxLength:200})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"slug",children:"URL slug"}),e.jsx(b,{id:"slug",value:t.slug,onChange:r=>{m(!0),c({slug:P(r.target.value)})}}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Lives at /articles/",t.slug||"your-slug"]})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"excerpt",children:"Excerpt"}),e.jsx(E,{id:"excerpt",value:t.excerpt,onChange:r=>c({excerpt:r.target.value}),rows:3,maxLength:400,placeholder:"Short summary shown on the listing and used for SEO."})]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(p,{htmlFor:"category",children:"Category"}),e.jsx(b,{id:"category",value:t.category,onChange:r=>c({category:r.target.value}),placeholder:"Buying, Market Updates, Investment..."})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"author",children:"Author"}),e.jsx(b,{id:"author",value:t.author_name,onChange:r=>c({author_name:r.target.value})})]})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"tags",children:"Tags (comma separated)"}),e.jsx(b,{id:"tags",value:t.tags,onChange:r=>c({tags:r.target.value}),placeholder:"auckland, first-home, negotiation"})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"cover",children:"Cover image URL"}),e.jsx(b,{id:"cover",value:t.cover_image_url,onChange:r=>c({cover_image_url:r.target.value}),placeholder:"https://..."})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"content",children:"Content (Markdown supported)"}),e.jsx(E,{id:"content",value:t.content,onChange:r=>c({content:r.target.value}),rows:18,className:"font-mono text-sm",placeholder:`## Heading 2 3Write your article in Markdown. Use **bold**, *italics*, [links](https://example.com), lists, and headings.`}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["~",R(t.content)," min read"]})]}),e.jsxs("div",{className:"flex items-center justify-between border-t border-border pt-6",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(U,{id:"published",checked:t.published,onCheckedChange:r=>c({published:r})}),e.jsx(p,{htmlFor:"published",children:"Published"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(N,{variant:"outline",onClick:()=>o("/admin/articles"),children:"Cancel"}),e.jsx(N,{onClick:f,disabled:u,className:"bg-navy hover:bg-navy-dark text-white",children:u?"Saving...":"Save"})]})]})]})]})}),e.jsx(K,{})]})};export{ue 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.