import{computed as S,createVNode as M}from"vue";import x from"../../../_util/hooks/useState.mjs";import A from"../../../vc-util/devWarning.mjs";import{getColumnKey as v,getColumnPos as D,renderColumnTitle as E}from"../../util.mjs";import P from"./FilterDropdown.mjs";function m(o,t,s){let r=[];return(o||[]).forEach((e,d)=>{const n=D(d,s),a=e.filterDropdown||e?.slots?.filterDropdown||e.customFilterDropdown;if(e.filters||a||"onFilter"in e)if("filteredValue"in e){let l=e.filteredValue;a||(l=l?.map(String)??l),r.push({column:e,key:v(e,n),filteredKeys:l,forceFiltered:e.filtered})}else r.push({column:e,key:v(e,n),filteredKeys:t&&e.defaultFilteredValue?e.defaultFilteredValue:void 0,forceFiltered:e.filtered});"children"in e&&(r=[...r,...m(e.children,t,n)])}),r}function V(o,t,s,r,e,d,n,a){return s.map((l,u)=>{const F=D(u,a),{filterMultiple:y=!0,filterMode:w,filterSearch:f}=l;let i=l;const g=l.filterDropdown||l?.slots?.filterDropdown||l.customFilterDropdown;if(i.filters||g){const h=v(i,F),c=r.find(({key:p})=>h===p);i={...i,title:p=>M(P,{tablePrefixCls:o,prefixCls:`${o}-filter`,dropdownPrefixCls:t,column:i,columnKey:h,filterState:c,filterMultiple:y,filterMode:w,filterSearch:f,triggerFilter:d,locale:e,getPopupContainer:n},{default:()=>[E(l.title,p)]})}}return"children"in i&&(i={...i,children:V(o,t,i.children,r,e,d,n,F)}),i})}function C(o){let t=[];return(o||[]).forEach(({value:s,children:r})=>{t.push(s),r&&(t=[...t,...C(r)])}),t}function K(o){const t={};return o.forEach(({key:s,filteredKeys:r,column:e})=>{const d=e.filterDropdown||e?.slots?.filterDropdown||e.customFilterDropdown,{filters:n}=e;if(d)t[s]=r||null;else if(Array.isArray(r)){const a=C(n);t[s]=a.filter(l=>r.includes(String(l)))}else t[s]=null}),t}function W(o,t){return t.reduce((s,r)=>{const{column:{onFilter:e,filters:d},filteredKeys:n}=r;return e&&n&&n.length?s.filter(a=>n.some(l=>{const u=C(d),F=u.findIndex(w=>String(w)===String(l)),y=F!==-1?u[F]:l;return e(y,a)})):s},o)}function k(o){return o.flatMap(t=>"children"in t?[t,...k(t.children||[])]:[t])}function $({prefixCls:o,dropdownPrefixCls:t,mergedColumns:s,locale:r,onFilterChange:e,getPopupContainer:d}){const n=S(()=>k(s.value)),[a,l]=x(m(n.value,!0)),u=S(()=>{const f=m(n.value,!1);if(f.length===0)return f;let i=!0,g=!0;if(f.forEach(({filteredKeys:h})=>{h!==void 0?i=!1:g=!1}),i){const h=(n.value||[]).map((c,p)=>v(c,D(p)));return a.value.filter(({key:c})=>h.includes(c)).map(c=>{const p=n.value[h.findIndex(I=>I===c.key)];return{...c,column:{...c.column,...p},forceFiltered:p.filtered}})}return A(g,"Table","Columns should all contain `filteredValue` or not contain `filteredValue`."),f}),F=S(()=>K(u.value)),y=f=>{const i=u.value.filter(({key:g})=>g!==f.key);i.push(f),l(i),e(K(i),i)};return[f=>V(o.value,t.value,f,u.value,r.value,y,d.value),u,F]}export{$ as default,C as flattenKeys,W as getFilterData};
