import{r as e}from"./rolldown-runtime-hePW80VL.js";import{o as t}from"./vendor-forms-yQg41KHz.js";import{t as n}from"./vendor-react-CAGJwf-T.js";import{C as r,D as i,O as a,T as o,w as s,x as c}from"./index-Z_bdWDpJ.js";import{d as l,r as u}from"./loanProductsRtkApi-1D8-ibN4.js";var d=e(t(),1),f=n(),p=[{value:`two_wheeler`,label:`Two Wheeler`},{value:`four_wheeler`,label:`Four Wheeler`},{value:`commercial`,label:`Commercial`}],m=[{value:`draft`,label:`Draft`},{value:`active`,label:`Active`},{value:`inactive`,label:`Inactive`}];function h({product:e,onClose:t,onSuccess:n}){let h=!!e,[g,_]=(0,d.useState)(e?.name??``),[v,y]=(0,d.useState)(e?.code??``),[b,x]=(0,d.useState)(e?.description??``),[S,C]=(0,d.useState)(e?.vehicle_category??`two_wheeler`),[w,T]=(0,d.useState)(e?.status??`draft`),[E,D]=(0,d.useState)({}),[O,k]=(0,d.useState)(``),[A,{isLoading:j}]=u(),[M,{isLoading:N}]=l(),P=j||N,F=e=>{_(e.target.value),D(e=>({...e,name:void 0}))},I=e=>{y(e.target.value.toUpperCase()),D(e=>({...e,code:void 0}))},L=()=>{let e={};return g.trim()||(e.name=`Product name is required`),v.trim()||(e.code=`Product code is required`),v.trim()&&!/^[A-Z0-9_-]+$/i.test(v.trim())&&(e.code=`Code may only contain letters, digits, hyphens, or underscores`),D(e),Object.keys(e).length===0};return(0,f.jsx)(a,{isOpen:!0,onClose:t,title:h?`Edit Loan Product`:`New Loan Product`,description:h?`Update the product details below.`:`Create a new loan product to configure financing options.`,size:`md`,footer:(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(i,{variant:`outline`,onClick:t,disabled:P,children:`Cancel`}),(0,f.jsx)(i,{form:`product-form`,type:`submit`,isLoading:P,children:h?`Save Changes`:`Create Product`})]}),children:(0,f.jsxs)(`form`,{id:`product-form`,onSubmit:async t=>{if(t.preventDefault(),k(``),L())try{h&&e?await M({id:e.id,body:{name:g.trim(),code:v.trim(),description:b.trim(),vehicle_category:S,status:w}}).unwrap():await A({name:g.trim(),code:v.trim(),description:b.trim(),vehicle_category:S,status:w}).unwrap(),n()}catch(e){let t=c(e),n=e;if(n.data?.errors){let e={};if(n.data.errors.name&&(e.name=n.data.errors.name[0]),n.data.errors.code&&(e.code=n.data.errors.code[0]),Object.keys(e).length){D(e);return}}k(t)}},className:`space-y-4`,children:[O&&(0,f.jsx)(`div`,{className:`px-3.5 py-3 rounded-lg bg-red-50 dark:bg-red-950/40 border border-red-100 dark:border-red-900 text-[13px] text-[var(--color-error)]`,children:O}),(0,f.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,f.jsx)(o,{label:`Product Name`,placeholder:`e.g. Two Wheeler Standard`,value:g,onChange:F,error:E.name,required:!0,autoFocus:!h}),(0,f.jsx)(o,{label:`Product Code`,placeholder:`e.g. TW-STD`,value:v,onChange:I,error:E.code,required:!0,className:`uppercase`})]}),(0,f.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,f.jsx)(s,{label:`Vehicle Category`,value:S,onChange:e=>C(e),options:p}),(0,f.jsx)(s,{label:`Status`,value:w,onChange:e=>T(e),options:m})]}),(0,f.jsx)(r,{label:`Description`,placeholder:`Describe this product and its intended use case...`,value:b,onChange:e=>x(e.target.value),error:E.description,rows:3})]})})}export{h as t};