const e=require("../../_util/classNames.js"),t=require("../../_util/KeyCode.js"),n=require("../../_util/hooks/useState.js"),r=require("../../tooltip/index.js"),i=require("../util.js");let a=require("vue"),o=require("@antsoo-lib/icons");var s=`ascend`,c=`descend`;function l(e){return typeof e.sorter==`object`&&typeof e.sorter.multiple==`number`&&e.sorter.multiple}function u(e){return typeof e==`function`?e:e&&typeof e==`object`&&e.compare?e.compare:!1}function d(e,t){return t?e[e.indexOf(t)+1]:e[0]}function f(e,t,n){let r=[];function a(e,t){r.push({column:e,key:i.getColumnKey(e,t),multiplePriority:l(e),sortOrder:e.sortOrder})}return(e||[]).forEach((e,o)=>{let s=i.getColumnPos(o,n);e.children?(`sortOrder`in e&&a(e,s),r=[...r,...f(e.children,t,s)]):e.sorter&&(`sortOrder`in e?a(e,s):t&&e.defaultSortOrder&&r.push({column:e,key:i.getColumnKey(e,s),multiplePriority:l(e),sortOrder:e.defaultSortOrder}))}),r}function p(n,u,f,m,h,g,_,v){return(u||[]).map((u,y)=>{let b=i.getColumnPos(y,v),x=u;if(x.sorter){let p=x.sortDirections||h,v=x.showSorterTooltip===void 0?_:x.showSorterTooltip,y=i.getColumnKey(x,b),S=f.find(({key:e})=>e===y),C=S?S.sortOrder:null,w=d(p,C),T=p.includes(s)&&(0,a.createVNode)(o.CaretUpOutlined,{class:e.default(`${n}-column-sorter-up`,{active:C===s}),role:`presentation`},null),E=p.includes(c)&&(0,a.createVNode)(o.CaretDownOutlined,{role:`presentation`,class:e.default(`${n}-column-sorter-down`,{active:C===c})},null),{cancelSort:D,triggerAsc:O,triggerDesc:k}=g||{},A=D;w===c?A=k:w===s&&(A=O);let j=typeof v==`object`?v:{title:A};x={...x,className:e.default(x.className,{[`${n}-column-sort`]:C}),title:t=>{let o=(0,a.createVNode)(`div`,{class:`${n}-column-sorters`},[(0,a.createVNode)(`span`,{class:`${n}-column-title`},[i.renderColumnTitle(u.title,t)]),(0,a.createVNode)(`span`,{class:e.default(`${n}-column-sorter`,{[`${n}-column-sorter-full`]:!!(T&&E)})},[(0,a.createVNode)(`span`,{class:`${n}-column-sorter-inner`},[T,E])])]);return v?(0,a.createVNode)(r.default,j,{default:()=>[o]}):o},customHeaderCell:r=>{let i=u.customHeaderCell&&u.customHeaderCell(r)||{},a=i.onClick,o=i.onKeydown;return i.onClick=e=>{m({column:u,key:y,sortOrder:w,multiplePriority:l(u)}),a&&a(e)},i.onKeydown=e=>{e.keyCode===t.default.ENTER&&(m({column:u,key:y,sortOrder:w,multiplePriority:l(u)}),o?.(e))},C&&(i[`aria-sort`]=C===`ascend`?`ascending`:`descending`),i.class=e.default(i.class,`${n}-column-has-sorters`),i.tabindex=0,i}}}return`children`in x&&(x={...x,children:p(n,x.children,f,m,h,g,_,b)}),x})}function m(e){let{column:t,sortOrder:n}=e;return{column:t,order:n,field:t.dataIndex,columnKey:t.key}}function h(e){let t=e.filter(({sortOrder:e})=>e).map(m);return t.length===0&&e.length?{...m(e[e.length-1]),column:void 0}:t.length<=1?t[0]||{}:t}function g(e,t,n){let r=t.slice().sort((e,t)=>t.multiplePriority-e.multiplePriority),i=e.slice(),a=r.filter(({column:{sorter:e},sortOrder:t})=>u(e)&&t);return a.length?i.sort((e,t)=>{for(let n=0;n<a.length;n+=1){let{column:{sorter:r},sortOrder:i}=a[n],o=u(r);if(o&&i){let n=o(e,t,i);if(n!==0)return i===s?n:-n}}return 0}).map(e=>{let r=e[n];return r?{...e,[n]:g(r,t,n)}:e}):i}function _({prefixCls:e,mergedColumns:t,onSorterChange:r,sortDirections:i,tableLocale:o,showSorterTooltip:s}){let[c,l]=n.default(f(t.value,!0)),u=(0,a.computed)(()=>{let e=!0,n=f(t.value,!1);if(!n.length)return c.value;let r=[];function i(t){e?r.push(t):r.push({...t,sortOrder:null})}let a=null;return n.forEach(t=>{a===null?(i(t),t.sortOrder&&(t.multiplePriority===!1?e=!1:a=!0)):(a&&t.multiplePriority!==!1||(e=!1),i(t))}),r}),d=(0,a.computed)(()=>{let e=u.value.map(({column:e,sortOrder:t})=>({column:e,order:t}));return{sortColumns:e,sortColumn:e[0]&&e[0].column,sortOrder:e[0]&&e[0].order}});function m(e){let t;t=e.multiplePriority===!1||!u.value.length||u.value[0].multiplePriority===!1?[e]:[...u.value.filter(({key:t})=>t!==e.key),e],l(t),r(h(t),t)}return[t=>p(e.value,t,u.value,m,i.value,o.value,s.value),u,d,(0,a.computed)(()=>h(u.value))]}exports.default=_,exports.getSortData=g;