"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('../../assets/index3.css');const f=require("react/jsx-runtime"),i=require("react"),k=require("../icon/index.cjs"),z="_table_1egqd_1",D="_sortable_1egqd_27",h={table:z,sortable:D},m=r=>i.isValidElement(r),q=r=>i.Children.toArray(r).reduce((e,t)=>m(t)?e+(t.type==="td"||t.type==="th"?1:q(t.props.children)):e,0),v=r=>{for(const e of i.Children.toArray(r)){if(!m(e))continue;if(e.type==="tr")return q(e.props.children);const t=v(e.props.children);if(t!==null)return t}return null},S=(r,e)=>i.Children.map(r,t=>{if(!m(t))return t;let s=t.props.children===void 0?t.props.children:S(t.props.children,e),n;return t.type==="td"&&t.props["data-span"]==="all"&&(n={colSpan:e}),t.type==="th"&&t.props["data-type"]&&(s=f.jsxs("span",{className:h.sortable,children:[s,f.jsx(k.Icon,{name:"updown",variant:"primary"})]})),n||s!==t.props.children?i.cloneElement(t,n,s):t}),I=(r,e,t,s,n=1)=>{if(t==="text")return r.localeCompare(e,s,{sensitivity:"base"})*n;const l=t==="date"?Date.parse(r):Number.parseFloat(r),c=t==="date"?Date.parse(e):Number.parseFloat(e);return Number.isNaN(l)&&Number.isNaN(c)?0:Number.isNaN(l)?1:Number.isNaN(c)?-1:(l-c)*n},P=({children:r,locale:e,...t})=>{const s=i.useRef(null),n=v(r),l=n&&n>0?S(r,n):r,c=(d,u,o)=>{d.querySelectorAll("th[data-type]").forEach(a=>{a===u?(a.dataset.dir=o,a.setAttribute("aria-sort",o==="asc"?"ascending":"descending")):(delete a.dataset.dir,a.setAttribute("aria-sort","none"))})},j=i.useCallback(d=>{var x,g;const u=s.current;if(!u)return;const o=(x=d.target)==null?void 0:x.closest("th");if(!o)return;const p=u.tHead;if(!p||!p.contains(o))return;const a=o.cellIndex;if(a<0)return;const b=(g=u.tBodies)==null?void 0:g[0];if(!b)return;const y=o.getAttribute("data-type");if(!y)return;const N=o.dataset.dir==="asc"?"desc":"asc";c(p,o,N);const w=N==="asc"?1:-1,C=Array.from(b.rows);C.sort((E,R)=>{var _,A;const T=(((_=E.cells[a])==null?void 0:_.textContent)??"").trim(),F=(((A=R.cells[a])==null?void 0:A.textContent)??"").trim();return I(T,F,y,e,w)}),b.append(...C)},[e]);return f.jsx("table",{className:h.table,onClick:j,ref:s,...t,"data-component":"table",children:l})};exports.Table=P;
//# sourceMappingURL=index.cjs.map
