import{c as d}from"./B-La3K3C.js";import{p as O,a4 as b,c as l,j as F,aW as k,n as s,k as x}from"./JQJBiNWj.js";import{u as D}from"./F98GHXdS.js";import{u as T}from"./DvBDpJyt.js";function J(S,q,r){const z=O(),u=b(),a=l(()=>({...u.query,page:h(u.query.page),size:P(u.query.size),sort:u.query.sort?[u.query.sort]:void 0})),i=F(!1),m=F(v()==="stored"),c=D(S,async()=>{var t;if(m.value)return m.value=!1,d();i.value=!0;const e=await q({...r==null?void 0:r.defaultFilters,...a.value,...r==null?void 0:r.fixedFilters,page:a.value.page-1,sort:(t=a.value.sort)==null?void 0:t[0]});return e||d()},{default:()=>d(),...r,lazy:!0,immediate:!1,watch:[a,...(r==null?void 0:r.watch)||[]]});function w(e){f({page:e.page<1||a.value.size!==e.rows?void 0:e.page+1,size:e.rows===((r==null?void 0:r.initialRows)||10)?void 0:e.rows})}function N(e){f({page:void 0,sort:e.sortField?[e.sortField+","+(e.sortOrder===1?"asc":"desc")]:void 0})}function f(e){var t;s({query:{...u.query,...e,sort:(t=e.sort)!=null&&t[0]?e.sort[0]:void 0}})}function h(e){if(!e||e==="0")return 1;const t=Number(e);return isNaN(t)||t<2?1:t}function P(e){const t=(r==null?void 0:r.initialRows)||10;if(!e)return t;const n=Number(e);return isNaN(n)||n===t?t:n}function R(){const e=g(a.value),t="filters."+u.name;if(!e){localStorage.removeItem(t);return}localStorage.setItem(t,JSON.stringify(a.value))}function g(e){const t={...e};return t.page&&Number(t.page)<2&&delete t.page,t.size&&t.size===((r==null?void 0:r.initialRows)||10)&&delete t.size,Object.keys(t).length?t:null}function y(){const e=localStorage.getItem("filters."+u.name);if(!e)return null;try{return g(JSON.parse(e))}catch{return null}}T(c.error,e=>{z.add({severity:"error",life:1e4,summary:(r==null?void 0:r.errorMessage)||"Erro ao buscar items.",detail:e.message})});function v(){return Object.keys(u.query).length?"url":y()?"stored":null}return k(()=>{const e=y(),t=v();t==="url"?s({query:{...u.query},replace:!0}):t==="stored"&&e?s({query:e,replace:!0}):r!=null&&r.initialFilters&&s({query:{...r==null?void 0:r.initialFilters},replace:!0}),setTimeout(()=>{i.value||c.execute()},100)}),x(a,()=>{R()},{immediate:!1}),{...c,page:l(()=>a.value.page-1),size:l(()=>a.value.size),sort:l(()=>a.value.sort),updatePageable:w,updateSortable:N,updatePageParams:f,initialRows:r==null?void 0:r.initialRows,executed:i}}export{J as u};
