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