"use client";
import { getListMultiSelectFilterOptions as v, getListSingleSelectFilterOptions as D, getDateTimeFilterOptions as b, getDateFilterOptions as A, getBooleanFilterOptions as F, getNumberFilterOptions as k, getStringFilterOptions as x } from "./data-table-constants.js";
import { defaultDataTableI18n as d } from "./data-table-i18n.js";
function C(e, a = "string") {
  const t = (s) => {
    switch (s) {
      case "string":
        return x(d)?.[0]?.value ?? "like";
      case "number":
        return k(d)?.[0]?.value ?? "eq";
      case "boolean":
        return F(d)?.[0]?.value ?? "eq";
      case "date":
        return A(d)?.[0]?.value ?? "eq";
      case "datetime":
        return b(d)?.[0]?.value ?? "date_time_before";
      case "list_single_select":
        return D(d)?.[0]?.value ?? "array_overlap";
      case "list_multi_select":
        return v(d)?.[0]?.value ?? "array_overlap";
      default:
        return "eq";
    }
  };
  return {
    id: crypto.randomUUID(),
    columnId: e?.id || "",
    columnLabel: e?.column.columnDef.meta?.headerLabel || (typeof e?.column.columnDef.header == "string" ? e.column.columnDef.header : "") || e?.id || "",
    columnAccessorKey: (e?.column.columnDef && "accessorKey" in e.column.columnDef && typeof e.column.columnDef.accessorKey == "string" ? e.column.columnDef.accessorKey : e?.id) || "",
    columnType: e?.column.columnDef.meta?.type || a,
    operator: t(
      e?.column.columnDef.meta?.type
    ),
    listOptions: e?.column.columnDef.meta?.listOptions
  };
}
function L() {
  const e = () => crypto.randomUUID();
  return {
    id: e(),
    rootGroup: {
      id: e(),
      logicalOperator: "AND",
      conditions: [
        {
          id: e(),
          columnId: "",
          columnAccessorKey: "",
          columnLabel: "",
          columnType: "string",
          operator: "eq",
          value: void 0
        }
      ],
      groups: []
    }
  };
}
function I(e, a) {
  const t = e.filter((n) => n.operator === "eq_null" || n.operator === "n_eq_null" || n.operator === "checked" || n.operator === "unchecked" ? !0 : n.value === void 0 || n.value === "" ? !1 : n.columnType === "list_multi_select" ? (Array.isArray(n.value) ? n.value.filter((i) => i !== void 0) : [n.value].filter((i) => i !== void 0)).length > 0 : !0), o = a ? ((n) => {
    const p = (i) => {
      const g = i.conditions.filter((u) => !u.columnId || !u.operator ? !1 : u.operator === "eq_null" || u.operator === "n_eq_null" || u.operator === "checked" || u.operator === "unchecked" ? !0 : u.value === void 0 ? !1 : u.columnType === "list_multi_select" ? (Array.isArray(u.value) ? u.value.filter((_) => _ !== void 0) : [u.value].filter((_) => _ !== void 0)).length > 0 : !0).map((u) => ({
        [u.columnAccessorKey]: {
          operator: u.operator,
          value: u.operator === "eq_null" || u.operator === "n_eq_null" || u.operator === "checked" || u.operator === "unchecked" ? !0 : u.columnType === "list_single_select" ? Array.isArray(u.value) ? u.value.filter((f) => f !== void 0) : [u.value].filter((f) => f !== void 0) : u.value
        }
      })), y = i.groups?.map(p).filter(Boolean) || [], m = [
        ...g,
        ...y
      ];
      return m.length === 0 ? null : m.length === 1 ? m[0] : i.logicalOperator === "AND" ? { _and: m } : { _or: m };
    };
    return p(n.rootGroup);
  })(a) : null, c = t.map(
    (n) => ({
      [n.columnAccessorKey]: {
        operator: n.operator,
        value: n.operator === "eq_null" || n.operator === "n_eq_null" || n.operator === "checked" || n.operator === "unchecked" ? !0 : n.columnType === "list_single_select" ? Array.isArray(n.value) ? n.value.filter((p) => p !== void 0) : [n.value].filter((p) => p !== void 0) : n.value ?? ""
      }
    })
  ), r = [
    ...o ? [o] : [],
    ...c
  ];
  return r.length > 0 ? r.length === 1 ? r[0] : { _and: r } : {};
}
function O(e, a) {
  const {
    rowHeight: t = 1,
    headerHeight: s = 1,
    containerHeight: o = 1,
    onPageChange: c,
    setPagination: r
  } = a, l = e?.offsetHeight || o, n = Math.floor(
    (l - s) / t
  ), p = n > 1 ? n : 1;
  r((i) => {
    if (i.pageSize !== p) {
      if (c) {
        const g = Math.max(
          0,
          Math.ceil(i.pageSize * (i.pageIndex + 1) / p) - 1
        ), y = Math.min(i.pageIndex, g);
        c(y, p);
      }
      return { ...i, pageSize: p };
    }
    return i;
  });
}
function T(e, a, t) {
  if (e == null)
    switch (a) {
      case "eq_null":
        return !0;
      case "n_eq_null":
        return !1;
      default:
        return !1;
    }
  const s = (r) => r == null || typeof r == "string" ? r : String(r), o = s(e), c = s(t);
  switch (a) {
    case "eq":
      return o === c;
    case "ne":
      return o !== c;
    case "lt":
      return typeof e == "number" && typeof t == "number" ? e < t : o < c;
    case "le":
      return typeof e == "number" && typeof t == "number" ? e <= t : o <= c;
    case "gt":
      return typeof e == "number" && typeof t == "number" ? e > t : o > c;
    case "ge":
      return typeof e == "number" && typeof t == "number" ? e >= t : o >= c;
    case "like":
      return o?.toLowerCase().includes(c?.toLowerCase());
    case "n_like":
      return !o?.toLowerCase().includes(c?.toLowerCase());
    case "starts_with":
      return o?.toLowerCase().startsWith(c?.toLowerCase());
    case "ends_with":
      return o?.toLowerCase().endsWith(c?.toLowerCase());
    case "eq_null":
      return e == null || e === "";
    case "n_eq_null":
      return e != null && e !== "";
    case "array_overlap":
      return Array.isArray(e) && Array.isArray(t) ? t.length === 0 ? !0 : e.length === 0 ? !1 : t.some(
        (r) => e.some(
          (l) => s(l) === s(r)
        )
      ) : !1;
    case "n_array_overlap":
      return Array.isArray(e) && Array.isArray(t) ? t.length === 0 || e.length === 0 ? !0 : !t.some(
        (r) => e.some(
          (l) => s(l) === s(r)
        )
      ) : !0;
    case "checked":
      return e === !0;
    case "unchecked":
      return e === !1;
    case "date_before":
      try {
        const r = new Date(e), l = new Date(t);
        return r.setHours(0, 0, 0, 0), l.setHours(0, 0, 0, 0), r < l;
      } catch {
        return !1;
      }
    case "date_after":
      try {
        const r = new Date(e), l = new Date(t);
        return r.setHours(0, 0, 0, 0), l.setHours(0, 0, 0, 0), r > l;
      } catch {
        return !1;
      }
    case "date_time_before":
      try {
        const r = new Date(e), l = new Date(t);
        return r < l;
      } catch {
        return !1;
      }
    case "date_time_after":
      try {
        const r = new Date(e), l = new Date(t);
        return r > l;
      } catch {
        return !1;
      }
    default:
      return console.warn(`Unknown filter operator: ${a}`), !0;
  }
}
function h(e, a) {
  if (!a || Object.keys(a).length === 0)
    return !0;
  if ("_and" in a && Array.isArray(a._and))
    return a._and.every(
      (t) => h(e, t)
    );
  if ("_or" in a && Array.isArray(a._or))
    return a._or.some(
      (t) => h(e, t)
    );
  for (const [t, s] of Object.entries(a))
    if (!t.startsWith("_") && typeof s == "object" && s !== null && "operator" in s) {
      const o = e.getValue(t);
      if (!T(
        o,
        s.operator,
        s.value
      )) return !1;
    }
  return !0;
}
function z(e, a) {
  let t;
  return function(...s) {
    clearTimeout(t), t = setTimeout(() => {
      e(...s);
    }, a);
  };
}
function H(e) {
  switch (e) {
    case "string":
      return "CaseUpper";
    case "number":
      return "Binary";
    case "boolean":
      return "ToggleRight";
    case "date":
      return "Calendar1";
    case "datetime":
      return "CalendarClock";
    case "list_single_select":
      return "List";
    case "list_multi_select":
      return "ListTodo";
    case "other":
      return "FileQuestionMark";
    default:
      return "FileQuestionMark";
  }
}
function K(e, a, t, s) {
  const o = a[e];
  if (!o || !t || o.side !== t) return;
  const c = o.side === "left" ? `${o.offset}px` : void 0, r = o.side === "right" ? `${o.offset}px` : void 0;
  return {
    position: "sticky",
    left: c,
    right: r,
    zIndex: s ? 49 : 48,
    background: "var(--d-background)",
    // Use separate shadow for pinned columns to avoid conflicts
    boxShadow: t === "left" ? `inset -1px ${s ? "0" : "-1px"} 0 var(--d-border), 2px 0 4px -2px rgba(0,0,0,0.1)` : `inset 1px ${s ? "0" : "-1px"} 0 var(--d-border), -2px 0 4px -2px rgba(0,0,0,0.1)`
  };
}
export {
  T as applyClientSideFilter,
  h as applySearchFilterToRow,
  I as buildSearchFilter,
  C as createBadgeFilterFromHeader,
  L as createDefaultAdvancedFilter,
  z as debounce,
  H as getIconFromColumnType,
  K as getPinnedStyle,
  O as updatePageSizeFromContainer
};
