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