import{o as e}from"./rolldown-runtime-GyhWBpbT.js";import{i as t,l as n,o as r,r as i}from"./react-vendor-DoSylNpz.js";import{An as a,Cr as o,Fr as s,Gr as c,Hr as l,Nr as u,Sr as d,Wr as f,Xt as p,br as m,fr as h,ii as g,ni as _,wr as v}from"./mui-vendor-1IGyn9nW.js";import{L as y,ct as b,d as x}from"./index-BD_w4_v3.js";import{t as S}from"./CountryFormDialog-DB8qYMdl.js";var C=e(g(),1),w=_(),T=({open:e,onClose:g,onSubmit:_,state:T,loading:E=!1,prefilledCountryId:D})=>{let{control:O,handleSubmit:k,reset:A,formState:{errors:j},setValue:M}=t({defaultValues:{code:``,name:``,gst_code:``,country_id:``}}),[N,P]=(0,C.useState)(null),[F,I]=(0,C.useState)(!1),L=n();(0,C.useEffect)(()=>{e&&(async()=>{if(D&&!T)try{let e=await y.getCountry(D);P({id:e.id,name:e.name}),M(`country_id`,e.id)}catch(e){console.error(`Error fetching prefilled country:`,e)}})()},[e,D,T]);let R=r({mutationFn:e=>y.createCountry(e),onSuccess:e=>{L.invalidateQueries({queryKey:[`countries`]}),P({id:e.id,name:e.name}),M(`country_id`,e.id),I(!1)}});(0,C.useEffect)(()=>{if(e){let e=T?.country?.id||D||``;A({code:T?.code||``,name:T?.name||``,gst_code:T?.gst_code||``,country_id:e}),P(T?.country?{id:T.country.id,name:T.country.name}:null)}},[e,T,A,D]);let z=async e=>{await _(e),A()},B=()=>{E||(A(),P(null),I(!1),g())},V=async e=>{await R.mutateAsync(e)};return(0,w.jsxs)(v,{open:e,onClose:(e,t)=>{t===`backdropClick`||t===`escapeKeyDown`||B()},maxWidth:`md`,fullWidth:!0,children:[(0,w.jsxs)(m,{sx:{display:`flex`,alignItems:`center`,justifyContent:`space-between`},children:[T?`Edit State`:`Add State`,(0,w.jsx)(f,{"aria-label":`close`,onClick:B,size:`small`,disabled:E,sx:{color:`text.secondary`},children:(0,w.jsx)(p,{})})]}),(0,w.jsxs)(`form`,{onSubmit:k(z),children:[(0,w.jsx)(d,{sx:{py:1},children:(0,w.jsxs)(h,{container:!0,spacing:2,children:[(0,w.jsx)(h,{size:{xs:12,sm:6},children:(0,w.jsxs)(s,{children:[(0,w.jsx)(l,{variant:`subtitle2`,sx:{mb:.5,fontWeight:600,color:`text.primary`},children:`State Code`}),(0,w.jsx)(i,{name:`code`,control:O,render:({field:e})=>(0,w.jsx)(a,{...e,placeholder:`Leave empty for auto-generation`,fullWidth:!0,error:!!j.code,helperText:j.code?.message||`Leave empty for auto-generation`,disabled:E,autoFocus:!0,sx:{"& .MuiInputBase-root":{height:42,borderRadius:1}}})})]})}),(0,w.jsx)(h,{size:{xs:12,sm:6},children:(0,w.jsxs)(s,{children:[(0,w.jsxs)(l,{variant:`subtitle2`,sx:{mb:.5,fontWeight:600,color:`text.primary`},children:[`State Name `,(0,w.jsx)(s,{component:`span`,sx:{color:`#f44336`,fontWeight:600},children:`*`})]}),(0,w.jsx)(i,{name:`name`,control:O,rules:{required:`State name is required`,maxLength:{value:100,message:`State name must not exceed 100 characters`}},render:({field:e})=>(0,w.jsx)(a,{...e,placeholder:`e.g., California, Texas, New York`,fullWidth:!0,error:!!j.name,helperText:j.name?.message,disabled:E,sx:{"& .MuiInputBase-root":{height:42,borderRadius:1}}})})]})}),(0,w.jsx)(h,{size:{xs:12,sm:6},children:(0,w.jsxs)(s,{children:[(0,w.jsx)(l,{variant:`subtitle2`,sx:{mb:.5,fontWeight:600,color:`text.primary`},children:`GST Code`}),(0,w.jsx)(i,{name:`gst_code`,control:O,rules:{pattern:{value:/^[0-9]{1,2}$/,message:`GST code must be 1-2 digits (e.g., 01, 33)`}},render:({field:e})=>(0,w.jsx)(a,{...e,placeholder:`e.g., 01, 02, 33`,fullWidth:!0,error:!!j.gst_code,helperText:j.gst_code?.message||`Enter the GST state code (optional)`,disabled:E,inputProps:{maxLength:2},sx:{"& .MuiInputBase-root":{height:42,borderRadius:1}}})})]})}),(0,w.jsx)(h,{size:{xs:12,sm:6},children:(0,w.jsxs)(s,{children:[(0,w.jsxs)(l,{variant:`subtitle2`,sx:{mb:.5,fontWeight:600,color:`text.primary`},children:[`Country `,(0,w.jsx)(s,{component:`span`,sx:{color:`#f44336`,fontWeight:600},children:`*`})]}),(0,w.jsx)(i,{name:`country_id`,control:O,rules:{required:`Country is required`},render:({field:{onChange:e}})=>(0,w.jsx)(x,{label:``,apiEndpoint:b.COUNTRIES,value:N,onChange:t=>{P(t),e(t?.id||``)},onCreateClick:()=>I(!0),error:!!j.country_id,helperText:j.country_id?.message||(D?`Pre-filled from parent form (read-only)`:``),disabled:E||!!D,placeholder:`Select a country`,showCreateButton:!D})})]})})]})}),(0,w.jsxs)(o,{sx:{px:3,pb:2,justifyContent:`flex-end`},children:[(0,w.jsx)(u,{onClick:B,disabled:E,variant:`outlined`,color:`secondary`,children:`Cancel`}),(0,w.jsx)(u,{type:`submit`,variant:`contained`,disabled:E,startIcon:E&&(0,w.jsx)(c,{size:20}),children:E?`Saving...`:`Save`})]})]}),(0,w.jsx)(S,{open:F,onClose:()=>I(!1),onSubmit:V,loading:R.isPending})]})};export{T as t};