1import{q as Be,m as Ge,r as o,j as e,L as ze}from"./react-vendor-DTEXiHme.js";import{a as xe}from"./pim-queries-D-PhB1f-.js";import{a as Qe,s as _,ab as x,a0 as We,N as qe,B as S,x as _e}from"./index-uBhdqU9F.js";import{I as R}from"./input-1AjB5knK.js";import{T as je}from"./textarea-YM_Xf7Tb.js";import{L as j}from"./label-BUSXwnvn.js";import{S as $e}from"./switch-DByVGcCt.js";import{S as w}from"./skeleton-C6nrDe95.js";import{S as y,a as b,b as L,c as P,d as A}from"./select-Bm7EtVPr.js";import{T as Ye,a as Xe,b as fe,c as D,d as Je,e as O}from"./table-DydzWk0g.js";import{A as Ke,a as Ze,b as es,c as ss,d as ts,e as as,f as ls,g as is}from"./alert-dialog-BkWt-txe.js";import{P as ns}from"./ProductPickerModal-DmzogFdM.js";import{S as os}from"./SearchableSelect-D4RoOh_u.js";import{A as cs}from"./arrow-left-D37foU3p.js";import{T as G}from"./trash-2-EvCYm-53.js";import{P as Ne}from"./plus-DQ00F-NA.js";import"./excel-CPlAJqTR.js";import"./radix-C8G44Doi.js";import"./dialog-DGzOvkgI.js";import"./checkbox-CfcNjTZD.js";import"./popover-1yZNW8f_.js";import"./chevrons-up-down-Bi3GZkEI.js";const rs=["ALPHA_ASC","ALPHA_DESC","BEST_SELLING","CREATED","CREATED_DESC","MANUAL","PRICE_ASC","PRICE_DESC"],ge={TAG:"Tag",TITLE:"Product Title",TYPE:"Product Type",VENDOR:"Vendor",VARIANT_PRICE:"Price",VARIANT_COMPARE_AT_PRICE:"Compare At Price",VARIANT_WEIGHT:"Weight",VARIANT_INVENTORY:"Inventory Quantity",VARIANT_TITLE:"Variant Title",IS_PRICE_REDUCED:"Price Reduced",PRODUCT_METAFIELD_DEFINITION:"Product Metafield",VARIANT_METAFIELD_DEFINITION:"Variant Metafield"},ds=Object.keys(ge),Ae={EQUALS:"is equal to",NOT_EQUALS:"is not equal to",GREATER_THAN:"is greater than",LESS_THAN:"is less than",STARTS_WITH:"starts with",ENDS_WITH:"ends with",CONTAINS:"contains",NOT_CONTAINS:"does not contain",IS_SET:"is set",IS_NOT_SET:"is not set"},us=new Set(["VARIANT_PRICE","VARIANT_COMPARE_AT_PRICE","VARIANT_WEIGHT","VARIANT_INVENTORY"]),ms=new Set(["TAG","TITLE","TYPE","VENDOR","VARIANT_TITLE"]),Ee=new Set(["IS_PRICE_REDUCED"]),ps=["EQUALS","NOT_EQUALS","GREATER_THAN","LESS_THAN"],hs=["EQUALS","NOT_EQUALS","STARTS_WITH","ENDS_WITH","CONTAINS","NOT_CONTAINS"],xs=["IS_SET","IS_NOT_SET"],Se=c=>us.has(c)?ps:Ee.has(c)?xs:ms.has(c)?hs:Object.keys(Ae),Te={VENDOR:"vendor",TYPE:"product_type",TAG:"tags"},_s=c=>c.toLowerCase().trim().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-"),Hs=()=>{const{id:c}=Be(),z=Ge(),{currentOrg:n}=Qe(),[ve,E]=o.useState(!0),[Ce,Ie]=o.useState(!1),[Q,W]=o.useState(!1),[v,q]=o.useState(!1),[Re,V]=o.useState(!1),[h,we]=o.useState(null),[f,U]=o.useState([]),[ye,k]=o.useState(!1),[be,$]=o.useState(!1),[N,Y]=o.useState(""),[X,H]=o.useState(""),[Le,J]=o.useState(!1),[K,Z]=o.useState(""),[ee,se]=o.useState("MANUAL"),[te,ae]=o.useState(""),[le,ie]=o.useState(""),[ne,oe]=o.useState(!1),[ce,re]=o.useState(!1),[C,T]=o.useState([]),g=o.useCallback(async()=>{if(!c)return;E(!0);const{data:s,error:l}=await _.from("pim_collections").select("*").eq("id",c).maybeSingle();if(l){x.error(`Failed to load collection: ${l.message}`),E(!1);return}if(!s){Ie(!0),E(!1);return}const a=s;if(we(a),Y(a.title??""),H(a.handle??""),J(!1),Z(a.description_html??""),se(a.sort_order??"MANUAL"),ae(a.seo_title??""),ie(a.seo_description??""),oe(!!a.is_published),re(!!a.rules_applied_disjunctively),a.collection_type==="smart"){const{data:t}=await _.from("pim_collection_rules").select("*").eq("collection_id",c);T((t??[]).map(u=>({column:u.rule_column,relation:u.rule_relation,conditionValue:u.condition_value??""}))),U([])}else{const{data:t}=await _.from("pim_collection_products").select("id, product_id, position").eq("collection_id",c).order("position",{ascending:!0}),r=t??[];if(r.length>0){const u=r.map(m=>m.product_id),i=(await xe(_,orgId)).filter(m=>u.includes(m.id)),p=new Map((i??[]).map(m=>[m.id,m]));U(r.map(m=>{var pe,he;return{...m,product_title:((pe=p.get(m.product_id))==null?void 0:pe.title)??"(unknown)",product_status:((he=p.get(m.product_id))==null?void 0:he.status)??""}}))}else U([]);T([])}E(!1)},[c]);o.useEffect(()=>{g()},[g]);const Pe=s=>{Y(s),Le||H(_s(s))},M=async s=>{!(n!=null&&n.id)||!c||await _.from("pim_collection_listener").insert({org_id:n.id,collection_id:c,change_type:s})},De=async()=>
1{if(!(!h||!(n!=null&&n.id)||!c)){W(!0);try{const s=h.collection_type==="smart",l={p_org_id:n.id,p_collection_id:c,p_title:N,p_handle:X,p_description_html:K,p_sort_order:ee,p_seo_title:te,p_seo_description:le,p_is_published:ne,p_collection_type:h.collection_type,p_channel_id:h.channel_id};s&&(l.p_rules=C.map(t=>({column:t.column,relation:t.relation,condition:t.conditionValue})),l.p_rules_disjunctive=ce);const{error:a}=await _.rpc("upsert_collection",l);if(a){x.error(`Save failed: ${a.message}`);return}await M("Update"),x.success("Collection saved"),g()}catch(s){x.error(`Save failed: ${(s==null?void 0:s.message)??"Unknown error"}`)}finally{W(!1)}}},de=async s=>{if(!(n!=null&&n.id)||!c||!h)return!1;const{error:l}=await _.rpc("upsert_collection",{p_org_id:n.id,p_collection_id:c,p_product_ids:s,p_collection_type:"manual",p_channel_id:h.channel_id});return l?(x.error(`Update failed: ${l.message}`),!1):(await M("Update"),!0)},Oe=async s=>{$(!0);const a=[...f.map(r=>r.product_id),...s],t=await de(a);$(!1),t&&(x.success("Products added"),k(!1),g())},Ve=async s=>{const l=f.map(t=>t.product_id).filter(t=>t!==s);await de(l)&&(x.success("Product removed"),g())},Ue=async()=>{if(!(!(n!=null&&n.id)||!c)){q(!0);try{await M("Delete");const{error:s}=await _.from("pim_collections").update({is_active:!1}).eq("id",c);if(s){x.error(`Delete failed: ${s.message}`);return}x.success("Collection deleted"),z("/pcm/collections")}catch(s){x.error(`Delete failed: ${(s==null?void 0:s.message)??"Unknown error"}`)}finally{q(!1),V(!1)}}},I=(s,l)=>{T(a=>a.map((t,r)=>r===s?{...t,...l}:t))},ke=s=>{T(l=>l.filter((a,t)=>t!==s))},He=()=>{T(s=>[...s,{column:"TAG",relation:"EQUALS",conditionValue:""}])},[F,Me]=o.useState({}),[ue,me]=o.useState(new Set);o.useEffect(()=>{if(!(n!=null&&n.id))return;const s=new Set;if(C.forEach(a=>{const t=Te[a.column];t&&!(t in F)&&!ue.has(t)&&s.add(t)}),s.size===0)return;const l=Array.from(s);me(a=>{const t=new Set(a);return l.forEach(r=>t.add(r)),t}),(async()=>{for(const a of l){const r=(await xe(_,n.id)).map(d=>d[a]).filter(d=>d!=null&&String(d).trim()!=="").map(String);let u;if(a==="tags"){const d=new Set;r.forEach(i=>i.split(",").map(p=>p.trim()).filter(Boolean).forEach(p=>d.add(p))),u=Array.from(d).sort((i,p)=>i.localeCompare(p))}else u=Array.from(new Set(r)).sort((d,i)=>d.localeCompare(i));Me(d=>({...d,[a]:u}))}me(a=>{const t=new Set(a);return l.forEach(r=>t.delete(r)),t})})()},[C,n==null?void 0:n.id,F,ue]);const Fe=o.useMemo(()=>f.map(s=>s.product_id),[f]);if(ve)return e.jsxs("div",{className:"p-6 space-y-4",children:[e.jsx(w,{className:"h-8 w-48"}),e.jsx(w,{className:"h-12 w-full"}),e.jsxs("div",{className:"grid grid-cols-2 gap-6",children:[e.jsx(w,{className:"h-96"}),e.jsx(w,{className:"h-96"})]})]});if(Ce)return e.jsxs("div",{className:"p-6 text-center space-y-4",children:[e.jsx("p",{children:"Collection not found."}),e.jsx(ze,{to:"/pcm/collections",className:"text-primary hover:underline",children:"â Back to Collections"})]});const B=h.collection_type==="smart";return e.jsxs("div",{className:"p-6 space-y-6",children:[e.jsx(We,{children:e.jsxs("title",{children:[N||"Collection"," | SKUuz"]})}),e.jsxs("button",{onClick:()=>z("/pcm/collections"),className:"inline-flex items-center text-sm text-muted-foreground hover:text-foreground",children:[e.jsx(cs,{className:"h-4 w-4 mr-1"})," Collections"]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("h1",{className:"text-2xl font-semibold",children:N}),e.jsx(qe,{variant:B?"default":"secondary",children:B?"Smart":"Manual"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(j,{htmlFor:"published-switch",className:"text-sm",children:"Published"}),e.jsx($e,{id:"published-switch",checked:ne,onCheckedChange:oe})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"space-y-4 bg-card border rounded-lg p-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"Title"}),e.jsx(R,{value:N,onChange:s=>Pe(s.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"Handle"}),e.jsx(R,{value:X,onChange:s=>{H(s.target.value),J(!0)}})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"Description"}),e.jsx(je,{rows:6,value:K,onChange:s=>Z(s.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"Sort Order"}),e.jsxs(y,{value:ee,onValueChange:se,children:[e.jsx(b,{children:e.jsx(L,{})}),e.jsx(P,{children:rs.map(s=>e.jsx(A,{value:s,children:s},s))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"SEO Title"}),e.jsx(R,{value:te,onChange:s=>ae(s.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"SEO Description"}),e.jsx(je,{rows:3,value:le,onChange:s=>ie(s.target.value)})]}),h.image_url&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{children:"Image"}),e.jsx("img",{src:h.image_url,alt:h.image_alt_text??N,className:"h-32 w-32 rounded object-cover border"})]}),e.jsxs("div",{className:"pt-4 flex items-center justify-between",children:[e.jsx(S,{onClick:De,disabled:Q,children:Q?e.jsxs(e.Fragment,{children:[e.jsx(_e,{className:"mr-2 h-4 w-4 animate-spin"}),"Saving..."]}):"Save"}),e.jsxs(S,{variant:"destructive",onClick:()=>V(!0),disabled:v,children:[e.jsx(G,{className:"mr-2 h-4 w-4"}),"Delete Collection"]})]})]}),e.jsx("div",{className:"space-y-4 bg-card border rounded-lg p-6",children:B?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex items-center justify-between",children:e.jsx("h2",{className:"text-lg font-semibold",children:"Rules"})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(j,{className:"text-sm",children:"Products must match:"}),e.jsxs(y,{value:ce?"any":"all",onValueChange:s=>re(s==="any"),children:[e.jsx(b,{className:"w-48",children:e.jsx(L,{})}),e.jsxs(P,{children:[e.jsx(A,{value:"all",children:"All rules"}),e.jsx(A,{value:"any",children:"Any rule"})]})]})]}),e.jsx("div",{className:"space-y-2",children:C.map((s,l)=>{const a=Se(s.column),t=a.includes(s.relation)?s.relation:a[0],r=Te[s.column],u=Ee.has(s.column)||t==="IS_SET"||t==="IS_NOT_SET",d=r?F[r]:void 0;return e.jsxs("div",{className:"flex items-center gap-2 p-2 border rounded",children:[e.jsxs(y,{value:s.column,onValueChange:i=>{const p=Se(i),m=p.includes(s.relation)?s.relation:p[0];I(l,{column:i,relation:m})},children:[e.jsx(b,{className:"w-44",children:e.jsx(L,{})}),e.jsx(P,{children:ds.map(i=>e.jsx(A,{value:i,children:ge[i]},i))})]}),e.jsxs(y,{value:t,onValueChange:i=>I(l,{relation:i}),children:[e.jsx(b,{className:"w-44",children:e.jsx(L,{})}),e.jsx(P,{children:a.map(i=>e.jsx(A,{value:i,children:Ae[i]},i))})]}),!u&&(r?e.jsx(os,{className:"flex-1 h-9 text-sm px-2",options:(d??[]).map(i=>({value:i,label:i})),value:s.conditionValue,onChange:i=>
1I(l,{conditionValue:i}),placeholder:d?"Select valueâ¦":"Loadingâ¦",disabled:!d}):e.jsx(R,{className:"flex-1",value:s.conditionValue,onChange:i=>I(l,{conditionValue:i.target.value}),placeholder:"Value"})),u&&e.jsx("div",{className:"flex-1"}),e.jsx(S,{variant:"ghost",size:"icon",onClick:()=>ke(l),children:e.jsx(G,{className:"h-4 w-4"})})]},l)})}),e.jsxs(S,{variant:"outline",size:"sm",onClick:He,children:[e.jsx(Ne,{className:"h-4 w-4 mr-1"})," Add Rule"]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h2",{className:"text-lg font-semibold",children:"Products"}),e.jsxs(S,{size:"sm",variant:"outline",onClick:()=>k(!0),children:[e.jsx(Ne,{className:"h-4 w-4 mr-1"})," Add Products"]})]}),f.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No products in this collection."}):e.jsxs(Ye,{children:[e.jsx(Xe,{children:e.jsxs(fe,{children:[e.jsx(D,{className:"w-20",children:"Position"}),e.jsx(D,{children:"Title"}),e.jsx(D,{children:"Status"}),e.jsx(D,{className:"w-12"})]})}),e.jsx(Je,{children:f.map(s=>e.jsxs(fe,{children:[e.jsx(O,{children:s.position}),e.jsx(O,{children:s.product_title}),e.jsx(O,{children:s.product_status}),e.jsx(O,{children:e.jsx(S,{variant:"ghost",size:"icon",onClick:()=>Ve(s.product_id),children:e.jsx(G,{className:"h-4 w-4"})})})]},s.id))})]})]})})]}),e.jsx(ns,{open:ye,onOpenChange:k,orgId:n==null?void 0:n.id,existingProductIds:Fe,title:"Add Products to Collection",onAdd:Oe,submitting:be}),e.jsx(Ke,{open:Re,onOpenChange:V,children:e.jsxs(Ze,{children:[e.jsxs(es,{children:[e.jsx(ss,{children:"Delete collection?"}),e.jsxs(ts,{children:["Are you sure you want to delete ",N,"? This action cannot be undone."]})]}),e.jsxs(as,{children:[e.jsx(ls,{disabled:v,children:"Cancel"}),e.jsxs(is,{onClick:Ue,disabled:v,className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:[v?e.jsx(_e,{className:"h-4 w-4 mr-2 animate-spin"}):null,"Delete"]})]})]})})]})};export{Hs 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.