/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{ref as e,computed as t,watch as a}from"vue";import{useVueTable as l}from"../../../node_modules/@tanstack/vue-table/build/lib/index.js";import{getPaginationRowModel as n,getFilteredRowModel as i,getSortedRowModel as o,getCoreRowModel as u,functionalUpdate as r}from"../../../node_modules/@tanstack/table-core/build/lib/index.js";function g(e=[]){return(e||[]).map(e=>({key:e.key,title:e.title??e.label??e.key,width:e.width,minWidth:e.minWidth,fixed:e.fixed,sortable:!!e.sortable,filterable:!!e.filterable,filterType:e.filterType,filterOptions:e.filterOptions,formatter:e.formatter,editor:e.editor,options:e.options}))}function v(v={}){const{data:s=[],request:c,mode:d="server",pageSize:m=10,initialQuery:f={},columns:p=[],rowKey:y="id",enableRowSelection:b=!1}=v,h=e(p||[]),S=e(Array.isArray(s)?(s||[]).slice():(s?.value||[]).slice()),w=e({...f||{}}),x=e(0),C=e(!1),z=e({pageIndex:0,pageSize:m}),R=e([]),M=e({}),k=e({left:[],right:[]}),P=e({}),I=function(e){return t(()=>g(e.value).map(e=>({id:e.key,accessorKey:e.key,header:e.title,size:e.width,minSize:e.minWidth??40,enableSorting:e.sortable,enableResizing:!0,enableHiding:!0,enablePinning:!0,meta:{col:e},cell:t=>{const a=t.getValue();if("function"==typeof e.formatter)try{return e.formatter(a,{row:t.row.original,index:t.row.index})}catch{return String(a??"")}return String(a??"")}})))}(h),T=t(()=>"server"===d&&"function"==typeof c);let j=null,O=!0;function V(){O||(j&&clearTimeout(j),j=setTimeout(()=>{K()},100))}const q=l({get data(){return[...S.value]},get columns(){return I.value},getRowId:(e,t)=>{const a=e&&e[y];return String(0===a||a?a:t)},state:{get sorting(){return R.value},get pagination(){return z.value},get columnVisibility(){return M.value},get columnPinning(){return k.value},get rowSelection(){return P.value}},onSortingChange:e=>{R.value=r(e,R.value),T.value&&(z.value={...z.value,pageIndex:0})},onPaginationChange:e=>{z.value=r(e,z.value)},onColumnVisibilityChange:e=>{M.value=r(e,M.value)},onColumnPinningChange:e=>{k.value=r(e,k.value)},enableRowSelection:!!b,onRowSelectionChange:e=>{P.value=r(e,P.value)},manualPagination:T.value,manualSorting:T.value,manualFiltering:T.value,pageCount:t(()=>{const e=z.value.pageSize||1;return Math.max(1,Math.ceil((x.value||0)/e))}),enableColumnResizing:!0,columnResizeMode:"onChange",getCoreRowModel:u(),getSortedRowModel:o(),getFilteredRowModel:i(),getPaginationRowModel:n()}),N=t({get:()=>(z.value.pageIndex||0)+1,set:e=>{const t=Math.max(1,Number(e||1));z.value={...z.value,pageIndex:t-1}}}),W=t({get:()=>z.value.pageSize,set:e=>{const t=Math.max(1,Number(e||m));z.value={pageIndex:0,pageSize:t}}}),A=t(()=>{const e=R.value&&R.value[0];return{id:e?.id??null,desc:!!e?.desc}}),F=t(()=>{const e=q.getRowModel();return(e?.rows||[]).map(e=>e.original)});async function K(){if(T.value){C.value=!0;try{const e=await c({page:N.value,pageSize:W.value,query:w.value,sort:A.value,sorts:R.value});S.value=(e?.records||[]).slice(),x.value=Number(e?.total??S.value.length)}finally{C.value=!1}}else x.value=S.value.length}function Q(e=[]){S.value=(e||[]).slice(),T.value||(x.value=S.value.length)}return s&&"object"==typeof s&&Object.prototype.hasOwnProperty.call(s,"value")&&a(()=>s.value,e=>{Q(e||[])},{deep:!0}),a(()=>[z.value.pageIndex,z.value.pageSize,A.value.id,A.value.desc],()=>{T.value&&V()}),a(()=>w.value,()=>{T.value&&V()},{deep:!0}),T.value?(K(),setTimeout(()=>{O=!1},100)):(x.value=S.value.length,O=!1),{table:q,loading:C,query:w,page:N,pageSize:W,total:x,rows:F,sort:function(e){if(!e)return;const t=q.getColumn(e);if(t&&"function"==typeof t.toggleSorting)return void t.toggleSorting(void 0,!1);const a=R.value?.[0];R.value=a?.id===e?[{id:e,desc:!a.desc}]:[{id:e,desc:!1}]},sortState:A,sorting:R,columnVisibility:M,columnPinning:k,rowSelection:P,load:K,setQuery:function(e={}){w.value={...e||{}},T.value&&(z.value={...z.value,pageIndex:0})},setData:Q,setColumns:function(e=[]){h.value=(e||[]).slice();try{const t=g(e||[]),a=t.filter(e=>"left"===e.fixed).map(e=>e.key),l=t.filter(e=>"right"===e.fixed).map(e=>e.key);q.setColumnPinning?.({left:a,right:l})}catch{}},mode:d}}export{v as useGridTable};