1import{j as e,ab as w,a7 as b,z as m,a6 as $,T as E,N as k,a2 as O,ag as B,ae as L,am as Q,ao as P,aA as G}from"./mui-CRsYZxVY.js";import{b as p}from"./vendor-Dwao9eNC.js";import{y as H,z as g,bx as K,C as Y,F as Z,D as u,el as J,b3 as _,d as X,w as A,b as V,eE as ee,T as re}from"./index-TALscxEc.js";import{u as oe}from"./useViaCep-BAAnqyZn.js";import{a as ae,b as se}from"./query-8ViTdhdk.js";import"./datePickers-BIJXYOKG.js";import"./icons-D8aatgQ4.js";import"./lab-DSem1Osk.js";(function(){try{var s=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{};s.SENTRY_RELEASE={id:"2ad85febf9735404429b0670d4acf56a0af5a0a2"}}catch{}})();try{(function(){var s=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{},r=new s.Error().stack;r&&(s._sentryDebugIds=s._sentryDebugIds||{},s._sentryDebugIds[r]="f8d4cff1-b7fe-4bbb-be5e-8e0a64fb5c6b",s._sentryDebugIdIdentifier="sentry-dbid-f8d4cff1-b7fe-4bbb-be5e-8e0a64fb5c6b")})()}catch{}const ne=H({cep:g().required("CEP é obrigatório").matches(/^\d{5}-?\d{3}$/,"CEP inválido"),logradouro:g().required("Endereço é obrigatório"),numero:g().required("Número é obrigatório"),complemento:g().nullable().transform(s=>s??""),bairro:g().required("Bairro é obrigatório"),localidade:g().required("Cidade é obrigatória"),uf:g().required("Estado é obrigatório").matches(/^[A-Z]{2}$/,"Estado inválido"),nome_remetente:g().nullable().transform(s=>s??"").max(50,"Máximo 50 caracteres").test("nome-completo","Informe nome e sobrenome",s=>!s||s.trim().split(/\s+/).filter(r=>r.length>=2).length>=2),ocultar_logo_etiqueta:K().nullable().transform(s=>s??!1)}),te=({onSubmit:s,defaultValues:r={cep:"",logradouro:"",numero:"",complemento:"",bairro:"",localidade:"",uf:"",nome_remetente:"",ocultar_logo_etiqueta:!1}})=>{var R;const{control:i,handleSubmit:x,watch:f,setValue:d,formState:{isSubmitting:t}}=Y({resolver:Z(ne),defaultValues:r}),[c,y]=p.useState({logradouro:!1,bairro:!1,localidade:!1,uf:!1}),[C,j]=p.useState(!1),[D,U]=p.useState(!!(r!=null&&r.nome_remetente)),F=(R=f("cep"))==null?void 0:R.replace(/\D/g,""),{data:h,isLoading:S,isError:T,error:q}=oe(F),M=T&&(q==null?void 0:q.kind)==="network",v=o=>{y(a=>({...a,[o]:!0}))};p.useEffect(()=>{h&&(c.logradouro||d("logradouro",h.logradouro),c.bairro||d("bairro",h.bairro),c.localidade||d("localidade",h.localidade),c.uf||d("uf",h.uf))},[h,d,c]),p.useEffect(()=>{y({logradouro:!1,bairro:!1,localidade:!1,uf:!1})},[F]);const N=o=>{o.preventDefault(),j(!0)},z=!!(r!=null&&r.logradouro),W=C||z||!!(h&&!S);return e.jsx("form",{onSubmit:x(s),children:e.jsxs(w,{spacing:3,children:[e.jsxs(b,{children:[e.jsx(u,{name:"cep",control:i,render:({field:{onChange:o,value:a,ref:n},fieldState:l})=>e.jsx(J,{mask:"99999-999",value:a,onChange:o,disabled:t,maskChar:null,children:()=>{var I;return e.jsx(m,{inputRef:n,label:"CEP",fullWidth:!0,error:!!l.error,helperText:((I=l.error)==null?void 0:I.message)||"",disabled:t,inputProps:{inputMode:"numeric",autoComplete:"postal-code"}})}})}),S&&e.jsxs(b,{display:"flex",alignItems:"center",gap:1,mt:1,children:[e.jsx($,{size:20}),e.jsx(E,{variant:"body2",color:"text.secondary",children:"Buscando CEP..."})]}),T&&!C&&e.jsx(_,{severity:M?"warning":"error",sx:{mt:2},action:e.jsx(k,{size:"small",onClick:N,children:"Digitar"}),children:M?"Não conseguimos buscar o CEP agora. Confira sua conexão ou digite o endereço.":"CEP não encontrado. Confira o número ou digite o endereço."}),!W&&!S&&!T&&e.jsx(b,{display:"flex",justifyContent:"flex-start",mt:1,children:e.jsx(k,{onClick:N,sx:{textTransform:"none",p:0,minWidth:"auto",fontWeight:500,textDecoration:"underline"},children:"Não sei meu CEP, quero digitar o endereço"})})]}),W&&e.jsxs(e.Fragment,{children:[e.jsx(u,{name:"logradouro",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"Endereço",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"",disabled:t,fullWidth:!0,onChange:l=>{o.onChange(l),v("logradouro")}})}}),e.jsx(u,{name:"numero",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"Número",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"",disabled:t,inputProps:{inputMode:"text"},fullWidth:!0})}}),e.jsx(u,{name:"complemento",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"Complemento",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"",disabled:t,inputProps:{inputMode:"text"},fullWidth:!0})}}),e.jsx(u,{name:"bairro",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"Bairro",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"",disabled:t,fullWidth:!0,onChange:l=>{o.onChange(l),v("bairro")}})}}),e.jsxs(w,{direction:"row",spacing:2,children:[e.jsx(u,{name:"localidade",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"Cidade",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"",disabled:t,fullWidth:!0,onChange:l=>{o.onChange(l),v("localidade")}})}}),e.jsx(u,{name:"uf",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"UF",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"",disabled:t,sx:{width:100}
1,inputProps:{maxLength:2,style:{textTransform:"uppercase"}},onChange:l=>{o.onChange(l.target.value.toUpperCase()),v("uf")}})}})]}),e.jsx(O,{sx:{mt:1}}),e.jsxs(b,{children:[e.jsx(E,{variant:"subtitle2",mb:1,children:"Privacidade no envio"}),e.jsx(B,{control:e.jsx(L,{checked:D,onChange:o=>{const a=o.target.checked;U(a),a||d("nome_remetente","")},disabled:t}),label:e.jsx(E,{variant:"body2",children:"Usar nome do remetente diferente do nome da loja"})}),e.jsx(Q,{in:D,children:e.jsx(b,{sx:{mt:1},children:e.jsx(u,{name:"nome_remetente",control:i,render:({field:o,fieldState:a})=>{var n;return e.jsx(m,{...o,label:"Nome do remetente",placeholder:"Ex: Maria Silva",error:!!a.error,helperText:((n=a.error)==null?void 0:n.message)||"Use nome e sobrenome. Será usado nas etiquetas no lugar do nome da loja",disabled:t,fullWidth:!0,inputProps:{maxLength:50}})}})})}),e.jsx(u,{name:"ocultar_logo_etiqueta",control:i,render:({field:o})=>e.jsx(B,{control:e.jsx(L,{checked:o.value||!1,onChange:o.onChange,disabled:t}),label:e.jsx(E,{variant:"body2",children:"Ocultar logotipo nas etiquetas de envio"})})})]}),e.jsx(k,{type:"submit",variant:"contained",color:"primary",disabled:t,fullWidth:!0,sx:{mt:2},children:"Salvar"})]})]})})},ie=()=>{const s=ae(),{data:r}=X();return se({mutationFn:async i=>{if(!(r!=null&&r.id))throw new Error("Loja não encontrada");const{data:x,error:f}=await A.from("endereco_loja").select("id").eq("catalogo_web_id",r.id).single();if(f&&f.code!=="PGRST116")throw f;if(x){const{data:d,error:t}=await A.from("endereco_loja").update({...i,atualizado_em:new Date().toISOString()}).eq("id",x.id).select().single();if(t)throw t;return d}else{const{data:d,error:t}=await A.from("endereco_loja").insert({...i,catalogo_web_id:r.id}).select().single();if(t)throw t;return d}},onSuccess:()=>{r!=null&&r.id&&s.invalidateQueries({queryKey:["storeAddress",r.id]})}})},he=()=>{const s=V(),{data:r,isLoading:i,isError:x,refetch:f}=ee(),d=ie(),[t,c]=p.useState(null),y=async C=>{try{await d.mutateAsync(C),s(-1)}catch(j){console.error("Error updating address:",j),c(j instanceof Error?j.message:"Erro ao salvar o endereço. Por favor, tente novamente.")}};return e.jsxs(b,{sx:{pb:4},children:[e.jsx(re,{title:"Endereço de origem",navigateBack:!0}),e.jsxs(w,{spacing:2,sx:{p:2},children:[e.jsx(_,{severity:"info",children:"à de onde seus pedidos saem. Usamos para calcular o frete, saber quais clientes estão na sua região e imprimir as etiquetas de envio."}),x&&e.jsx(_,{severity:"error",action:e.jsx(b,{component:"button",type:"button",onClick:()=>f(),sx:{border:"none",background:"none",font:"inherit",fontWeight:600,color:"primary.main",cursor:"pointer",p:.5},children:"Tentar de novo"}),children:"Não conseguimos carregar seu endereço. Verifique sua conexão."}),i?e.jsxs(w,{spacing:2,children:[e.jsx(P,{variant:"rounded",height:56,sx:{borderRadius:1}}),e.jsx(P,{variant:"rounded",height:56,sx:{borderRadius:1}}),e.jsx(P,{variant:"rounded",height:56,sx:{borderRadius:1}})]}):e.jsx(te,{onSubmit:y,defaultValues:r?{...r,complemento:r.complemento??"",nome_remetente:r.nome_remetente??"",ocultar_logo_etiqueta:r.ocultar_logo_etiqueta??!1}:void 0})]}),e.jsx(G,{open:!!t,autoHideDuration:6e3,onClose:()=>c(null),children:e.jsx(b,{children:e.jsx(_,{severity:"error",onClose:()=>c(null),children:t})})})]})};export{he as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.