import{D as u}from"./CX2DUt_P.js";import{p as $,$ as L,c as m,j as D,a1 as U,i as v,Q as I,a0 as n,k as V,n as M,D as T}from"./dVzHJMF1.js";import{u as W}from"./yHxjwlra.js";import{u as k}from"./COliOQKD.js";import{s as q}from"./-CGeDbWH.js";import{u as P}from"./DzXlli-V.js";const ee=(i,O,R)=>{const d=$(),f=L(),c=m(()=>f.params.empresa),{data:s,refresh:j,error:b}=W(_,{immediate:!1},"$VzNeWDiHAC"),E=D(!1),F=m(()=>J(s.value)),g=D(0),o=k({allow:[u],mountFn:j,notFoundMessage:"O ativo não foi encontrado.",beforeMountFn:e=>{(e===u?O:O||R)||U()}}),r=D(f.params.id===u);function x(){r.value=!r.value,r.value||(g.value=new Date().getTime())}function J(e){return e?JSON.parse(JSON.stringify(e)):s.value?JSON.parse(JSON.stringify(s.value)):void 0}async function _(){var e,t;if(!(!o.value||Array.isArray(o.value)||isNaN(Number(o.value))))try{const a=await v.ativos.cadastros.findById(c.value,i,Number(o.value));if(!((e=a==null?void 0:a.id)!=null&&e.idElemento))throw{status:404,message:"O elemento não foi encontrado."};return g.value=new Date().getTime(),a}catch(a){const l=a;d.add({severity:"error",life:1e4,detail:((t=l==null?void 0:l.data)==null?void 0:t.message)||"Erro ao buscar elemento."})}}async function z(e){var N,S,h,w,A;if((N=e.errors)!=null&&N.length||!e.valid||!e.values){q();return}const t={tipoAtivo:i,...s.value,...e.values},a=!!((h=(S=s.value)==null?void 0:S.id)!=null&&h.idElemento),l=()=>a?v.ativos.cadastros.update(c.value,i,s.value.id.idElemento,t):v.ativos.cadastros.save(c.value,i,t);E.value=!0;try{const y=await l();d.add({severity:"success",life:1e4,detail:`Elemento ${a?"atualizado":"criado"} com sucesso.`}),a?r.value=!1:M("./"+((w=y.id)==null?void 0:w.idElemento))}catch(y){const p=y,H=(A=p==null?void 0:p.data)==null?void 0:A.message;d.add({severity:"error",life:1e4,detail:H})}finally{E.value=!1}}const B=P(()=>{var e;if(!((e=s.value)!=null&&e.id.idElemento))throw new Error("ID do Elemento não encontrado.");return v.ativos.cadastros.delete(c.value,i,s.value.id.idElemento)},{dialogHeader:"Excluir Elemento",confirmMessage:"Tem certeza que deseja excluir este elemento?",successMessage:"Elemento excluído com sucesso.",acceptLabel:"Confirmar",onSuccess:()=>{M("./")}});I("elemento",n(s)),I("isEditMode",n(r));const C=m(()=>{var e,t;return f.params.id===u?"- Adicionar":(t=(e=s.value)==null?void 0:e.id)!=null&&t.idElemento?s.value.codigo:""});return V(b,()=>{var a;const e=b.value;let t=(a=e==null?void 0:e.data)==null?void 0:a.message;(e.status===404||e.status===400)&&(t="O elemento não foi encontrado."),d.add({severity:"error",life:1e4,detail:t}),M("./")}),{ready:m(()=>o.value===u?!0:!!F.value),elementoId:o,elemento:n(s),initialValues:F,idTitle:C,isEditMode:n(r),refreshForm:n(g),isSubmiting:n(E),toggleEditMode:x,handleSubmitForm:z,deleteElemento:B}},ae=()=>T("elemento"),se=()=>T("isEditMode");export{ee as a,ae as b,se as u};
