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-BD_NkAgt.js";import{Er as a,Fr as o,Kr as s,Lr as c,Mn as l,Sr as u,Tr as d,Wr as f,Zt as p,ii as m,mr as h,oi as g,qr as _,wr as v}from"./mui-vendor-DsoH2_ot.js";import{R as y,d as b,lt as x}from"./index-Bo9BrBsx.js";import{t as S}from"./CountryFormDialog-CnlW_hjW.js";var C=e(g(),1),w=m(),T=({open:e,onClose:m,onSubmit:g,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 g(e),A()},B=()=>{E||(A(),P(null),I(!1),m())},V=async e=>{await R.mutateAsync(e)};return(0,w.jsxs)(a,{open:e,onClose:(e,t)=>{t===`backdropClick`||t===`escapeKeyDown`||B()},maxWidth:`md`,fullWidth:!0,children:[(0,w.jsxs)(u,{sx:{display:`flex`,alignItems:`center`,justifyContent:`space-between`},children:[T?`Edit State`:`Add State`,(0,w.jsx)(s,{"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)(v,{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)(c,{children:[(0,w.jsxs)(f,{variant:`subtitle2`,sx:{mb:.5,fontWeight:600,color:`text.primary`},children:[`State Name `,(0,w.jsx)(c,{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)(l,{...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)(c,{children:[(0,w.jsx)(f,{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)(l,{...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)(c,{children:[(0,w.jsxs)(f,{variant:`subtitle2`,sx:{mb:.5,fontWeight:600,color:`text.primary`},children:[`Country `,(0,w.jsx)(c,{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)(b,{label:``,apiEndpoint:x.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)(d,{sx:{px:3,pb:2,justifyContent:`flex-end`},children:[(0,w.jsx)(o,{onClick:B,disabled:E,variant:`outlined`,color:`secondary`,children:`Cancel`}),(0,w.jsx)(o,{type:`submit`,variant:`contained`,disabled:E,startIcon:E&&(0,w.jsx)(_,{size:20}),children:E?`Saving...`:`Save`})]})]}),(0,w.jsx)(S,{open:F,onClose:()=>I(!1),onSubmit:V,loading:R.isPending})]})};export{T as t};