import{r as o,j as e}from"./client-BbMi5ewG.js";import{S as N,a as C,b as w,c as v,f as j}from"./select-CPHcJnpt.js";import{a as S,s as f,u as D}from"./store-jX25q14N.js";import{D as E,a as B,b as I,c as _}from"./dialog-C9IQloQy.js";import"./label-1wXMewvs.js";import{I as A}from"./input-DhCVOJC-.js";import{B as M}from"./Button-1dacbE2T.js";import{P as H}from"./plus-DcvXjop0.js";function P({isOpen:a,onOpenChange:i,categoryToEdit:r,onCategorySaved:g}){const[n,x]=o.useState(""),[c,s]=o.useState(!1),[u,l]=o.useState(null),t=!!r,p=S(f);o.useEffect(()=>{a&&(x(t?r.name:""),l(null))},[a,t,r]);const b=async m=>{try{return await p.saveCategory(m)}catch(h){console.error("Error saving category:",h)}},y=async m=>{if(m.preventDefault(),s(!0),l(null),!n.trim()){l("Category name cannot be empty."),s(!1);return}const h={name:n.trim()};try{if(t){const d=await b({...r,...h});g({...r,...d})}else{const d=await b({...h});g({...d,services:[]})}i(!1)}catch(d){l(d.message)}finally{s(!1)}};return e.jsx(E,{open:a,onOpenChange:i,children:e.jsxs(B,{className:"rtrb-terms-add sm:max-w-md p-0 border-none overflow-hidden",children:[e.jsx(I,{className:"bg-light-bg px-[30px] h-[60px] flex justify-center flex-col",children:e.jsx(_,{className:"text-base font-semibold text-title",children:t?"Edit Category":"Add Category"})}),e.jsxs("form",{onSubmit:y,className:"grid gap-4 px-[30px] pb-[30px] dialog-close-btn:bg-red-900",children:[e.jsx("p",{className:"text-title2 text-[14px]",children:t?"Update the details for this category.":"Enter the name for your  category."}),u&&e.jsx("p",{className:"text-red-500 text-sm",children:u}),e.jsx("div",{className:"flex items-center gap-4",children:e.jsx(A,{placeholder:"Name",id:"category-name",value:n,onChange:m=>x(m.target.value),className:"w-full h-[45px] rtrb-input",required:!0})}),e.jsx("div",{className:"",children:e.jsx(M,{type:"submit",className:"h-[45px] px-[40px] shadow-[0px_2px_6px_#9BB1EB;]",disabled:c,children:c?t?"Saving...":"Creating...":t?"Save Changes":"Create Category"})})]})]})})}const z=({value:a,onChange:i,placeholder:r="Select a category",errors:g})=>{const n=S(f),[x,c]=o.useState(!1);o.useEffect(()=>{n.getAllCategory()},[n]);const s=D(t=>t(f).getAllCategories(),[]),u=t=>{t==="create_new"?c(!0):i(t)},l=t=>{n.getAllCategory(),t!=null&&t.id&&i(t.id.toString())};return e.jsxs(e.Fragment,{children:[e.jsxs(N,{value:a==null?void 0:a.toString(),onValueChange:u,children:[e.jsx(C,{className:`rtrb-input h-11 text-base border-border focus:border-gray-900 focus:ring-1 focus:ring-gray-900 rounded-lg ${g?"border-red-500":"border-border"}`,children:e.jsx(w,{placeholder:r})}),e.jsxs(v,{className:"rounded-lg border shadow-lg",children:[e.jsx(j,{value:"create_new",className:"text-blue-600 font-medium cursor-pointer",children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx(H,{className:"w-4 h-4"}),"Create New Category"]})}),(s==null?void 0:s.length)>0&&e.jsx("div",{className:"h-px bg-gray-200 my-1"}),s==null?void 0:s.map(t=>{var p;return e.jsx(j,{value:(p=t.value)==null?void 0:p.toString(),children:t.label},t.value)})]})]}),e.jsx(P,{isOpen:x,onOpenChange:c,categoryToEdit:null,onCategorySaved:l})]})};export{z as C,P as a};
//# sourceMappingURL=CategorySelect-C2tAEr7d.js.map
