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