import{r as o,j as e,I as b,B as F,u as h}from"./main-C5rJoLCl.js";import{u as y,F as C,a as i,b as d,c as u,d as p,e as x,s as k}from"./form-TtPPMvDl.js";import{z as l}from"./index-C4bLmoCR.js";import{F as I,S as T,a as E,b as V,c as M,d as j}from"./FetchWrapper-DZ79aYT8.js";import{T as O}from"./textarea-DrZBTCi5.js";import{S as A}from"./spinner-zDOxBldC.js";import{u as U}from"./layout-2E7L6V5j.js";const J=l.object({name:l.string().min(1,"Category name is required"),status:l.enum(["active","inactive"]),desc:l.string().optional()});function se({initialData:s,type:c}){const[L,S]=o.useState(0),[P,_]=o.useState(!1),[z,B]=o.useState([]),[W,q]=o.useState(""),[H,R]=o.useState(!1),[m,f]=o.useState(!1),[Y,$]=o.useState(!1),g=new I(linkfiliate==null?void 0:linkfiliate.pluginApiUrl,linkfiliate==null?void 0:linkfiliate.nonce),w=(linkfiliate==null?void 0:linkfiliate.is_free)==="1"||(linkfiliate==null?void 0:linkfiliate.is_free)===1||(linkfiliate==null?void 0:linkfiliate.is_free)===!0,{openModal:v}=U.getState();o.useEffect(()=>{const r=window.location.origin+"/go/",t=document.createElement("span");t.style.visibility="hidden",t.style.position="absolute",t.style.whiteSpace="nowrap",t.innerText=r,document.body.appendChild(t);const n=t.offsetWidth;document.body.removeChild(t),S(n+24)},[]),s!=null&&s.settings&&JSON.parse(s.settings);const a=y({resolver:k(J),defaultValues:{name:c!=="new"?s==null?void 0:s.name:"",status:c!=="new"?s==null?void 0:s.status:"active",desc:c!=="new"?s==null?void 0:s.description:""}});async function N(r){try{f(!0);const t=c==="edit"?`/category/update/${s==null?void 0:s.id}`:"/category/create",n=await(c==="edit"?g.put(t,JSON.stringify(r)):g.post(t,JSON.stringify(r)));if(n.status!=="success")throw{status:n.status,message:n.message||"Unknown error"};if(n.status==="success")h.success("Success",{description:n.message,onAutoClose:()=>{window.location.href="/wp-admin/admin.php?page=linkfiliate#/campaign"}});else throw new Error(n.message||"Something went wrong")}catch(t){w&&t.status===403&&v(!0),h.error("Error",{description:t.message||"Failed to save campaign"})}finally{f(!1)}}return e.jsx(e.Fragment,{children:e.jsx(C,{...a,children:e.jsxs("form",{onSubmit:a.handleSubmit(N),className:"space-y-8",children:[e.jsxs("div",{className:"flex w-full gap-8",children:[e.jsxs("div",{className:"form1 flex-1 space-y-4",children:[e.jsx(i,{control:a.control,name:"name",render:({field:r})=>e.jsxs(d,{className:"flex items-center gap-4",children:[e.jsx(u,{className:"min-w-[150px] text-right",children:"Category Name :"}),e.jsxs("div",{className:"flex-1",children:[e.jsx(p,{children:e.jsx(b,{placeholder:"",...r})}),e.jsx(x,{})]})]})}),e.jsx(i,{control:a.control,name:"status",render:({field:r})=>e.jsxs(d,{className:"flex items-center gap-4",children:[e.jsx(u,{className:"min-w-[150px] text-left",children:"Status :"}),e.jsxs("div",{className:"flex-1",children:[e.jsxs(T,{onValueChange:r.onChange,defaultValue:r.value,children:[e.jsx(p,{children:e.jsx(E,{children:e.jsx(V,{placeholder:"Select Status"})})}),e.jsxs(M,{children:[e.jsx(j,{value:"active",children:"Active"}),e.jsx(j,{value:"inactive",children:"inActive"})]})]}),e.jsx(x,{})]})]})}),e.jsx(i,{control:a.control,name:"desc",render:({field:r})=>e.jsxs(d,{className:"flex items-center gap-4",children:[e.jsx(u,{className:"min-w-[150px]",children:"Description :"}),e.jsx("div",{className:"flex-1",children:e.jsx(p,{children:e.jsx(O,{placeholder:"",className:"resize-none !bg-background !text-foreground",...r})})}),e.jsx(x,{})]})})]}),e.jsx("div",{className:"form2 w-1/2 pr-4",style:{maxHeight:"calc(100vh - 300px)",overflowY:"auto"}})]}),e.jsx("div",{className:"flex justify-end",children:e.jsx(F,{type:"submit",className:"font-semibold text-white",disabled:m,children:m?e.jsxs(e.Fragment,{children:[e.jsx(A,{className:"mr-2 h-4 w-4 text-white"}),"Loading..."]}):e.jsx(e.Fragment,{children:m?"Please wait...":c==="edit"?"Update":c==="copy"?"Copy Campaign":"Create"})})})]})})})}export{se as C};
//# sourceMappingURL=campaign-form-B-szhSuX.js.map
