import{d as x,r as f,k as _,a as o,u as l}from"../index.js";const A=x("deals",()=>{const t=f([]),u=f(!1),v=f({pipeline_id:null}),m=_(()=>e=>[...t.value].filter(a=>a.stage_id===e).sort((a,s)=>a.position-s.position));async function y(){u.value=!0;try{const e=await o("deals",{params:{...v.value,per_page:500}});t.value=Array.isArray(e)?e:e.items||[]}catch(e){l().error(e.message)}finally{u.value=!1}}async function h(e){try{const a=await o("deals",{method:"POST",body:e});return t.value.unshift(a),a}catch(a){throw l().error(a.message),a}}async function p(e,a){try{const s=await o(`deals/${e}`,{method:"PATCH",body:a}),r=t.value.findIndex(c=>c.id===e);return r>-1&&t.value.splice(r,1,s),s}catch(s){throw l().error(s.message),s}}async function g(e){try{await o(`deals/${e}`,{method:"DELETE"}),t.value=t.value.filter(a=>a.id!==e)}catch(a){throw l().error(a.message),a}}async function w(e,a,s){const r=t.value.findIndex(n=>n.id===e),c=r>-1?{...t.value[r]}:null;r>-1&&(t.value[r]={...t.value[r],stage_id:a,position:s});try{const n=await o(`deals/${e}/move`,{method:"PUT",body:{stage_id:a,position:s}}),i=t.value.findIndex(d=>d.id===e);return i>-1&&t.value.splice(i,1,n),n}catch(n){if(c!==null){const i=t.value.findIndex(d=>d.id===e);i>-1&&t.value.splice(i,1,c)}throw l().error(n.message),n}}return{items:t,loading:u,filters:v,byStage:m,fetchAll:y,create:h,update:p,remove:g,move:w}});export{A as u};
