import{D as d}from"./DKM0wLqG.js";import{p as L,a4 as U,c as m,j as D,a6 as V,i as v,Q as I,a5 as n,k as W,n as M,D as T}from"./JQJBiNWj.js";import{u as $}from"./F98GHXdS.js";import{u as k}from"./D7gSulp_.js";import{s as q}from"./-CGeDbWH.js";import{u as P}from"./ChMy_Kon.js";const ee=(i,O,R)=>{const c=L(),f=U(),l=m(()=>f.params.empresa),{data:t,refresh:j,error:b}=$(_,{immediate:!1},"$VzNeWDiHAC"),E=D(!1),F=m(()=>J(t.value)),g=D(0),o=k({allow:[d],mountFn:j,notFoundMessage:"O ativo não foi encontrado.",beforeMountFn:e=>{(e===d?O:O||R)||V()}}),r=D(f.params.id===d);function x(){r.value=!r.value,r.value||(g.value=new Date().getTime())}function J(e){return e?JSON.parse(JSON.stringify(e)):t.value?JSON.parse(JSON.stringify(t.value)):void 0}async function _(){var e,s;if(!(!o.value||Array.isArray(o.value)||isNaN(Number(o.value))))try{const a=await v.ativos.cadastros.findById(l.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 u=a;c.add({severity:"error",life:1e4,detail:((s=u==null?void 0:u.data)==null?void 0:s.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 s={tipoAtivo:i,...t.value,...e.values},a=!!((h=(S=t.value)==null?void 0:S.id)!=null&&h.idElemento),u=()=>a?v.ativos.cadastros.update(l.value,i,t.value.id.idElemento,s):v.ativos.cadastros.save(l.value,i,s);E.value=!0;try{const y=await u();c.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;c.add({severity:"error",life:1e4,detail:H})}finally{E.value=!1}}const B=P(()=>{var e;if(!((e=t.value)!=null&&e.id.idElemento))throw new Error("ID do Elemento não encontrado.");return v.ativos.cadastros.delete(l.value,i,t.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(t)),I("isEditMode",n(r));const C=m(()=>{var e,s;return f.params.id===d?"- Adicionar":(s=(e=t.value)==null?void 0:e.id)!=null&&s.idElemento?"- "+l.value+"/"+t.value.id.idElemento:""});return W(b,()=>{var a;const e=b.value;let s=(a=e==null?void 0:e.data)==null?void 0:a.message;(e.status===404||e.status===400)&&(s="O elemento não foi encontrado."),c.add({severity:"error",life:1e4,detail:s}),M("./")}),{ready:m(()=>o.value===d?!0:!!F.value),elementoId:o,elemento:n(t),initialValues:F,idTitle:C,isEditMode:n(r),refreshForm:n(g),isSubmiting:n(E),toggleEditMode:x,handleSubmitForm:z,deleteElemento:B}},ae=()=>T("elemento"),te=()=>T("isEditMode");export{ee as a,ae as b,te as u};
