import { useState as z, useRef as R, useLayoutEffect as L, useEffect as I, useMemo as X } from "react";
import { DEFAULT_MIN_RESIZE_WIDTH_PX as g, MIN_COLUMN_WIDTH_PX as b, DEFAULT_MAX_RESIZE_WIDTH_PX as D } from "../tableSizing.constants.js";
const E = (l) => {
  const u = (typeof l == "number" ? `${l}px` : l?.trim())?.match(/^(-?\d+(?:\.\d+)?)(px|%)$/);
  if (u)
    return {
      unit: u[2],
      value: Number(u[1])
    };
}, f = (l, r) => {
  if (l === r)
    return !0;
  const u = E(l), h = E(r);
  return u !== void 0 && h !== void 0 && u.unit === h.unit ? u.value === h.value : !1;
}, P = (l) => {
  const { columns: r, enabled: u, onColumnResize: h } = l, [C, W] = z({}), [c, p] = z(void 0), m = R({}), y = R({}), w = R(!1);
  r.forEach((e) => {
    e.key in m.current || (m.current[e.key] = e.width);
  }), L(() => {
    const e = Object.fromEntries(r.map((t) => [t.key, t.width]));
    if (r.length === 0)
      return;
    if (!w.current) {
      y.current = e, w.current = !0;
      return;
    }
    const i = y.current, a = new Set(r.map((t) => t.key)), s = Object.keys(i);
    (r.some((t) => !f(t.width, i[t.key])) || s.some((t) => !a.has(t))) && (W((t) => {
      let n = t;
      return r.forEach((d) => {
        const S = y.current[d.key];
        if (f(d.width, S))
          return;
        const _ = m.current[d.key];
        if (f(d.width, _)) {
          if (!(d.key in n))
            return;
          n = { ...n }, delete n[d.key];
          return;
        }
        n[d.key] !== d.width && (n = {
          ...n,
          [d.key]: d.width
        });
      }), Object.keys(n).forEach((d) => {
        a.has(d) || (n === t && (n = { ...n }), delete n[d]);
      }), n;
    }), y.current = e);
  }, [r]), I(() => {
    if (!c)
      return;
    const e = document.body.style.cursor, i = document.body.style.userSelect;
    document.body.style.cursor = "col-resize", document.body.style.userSelect = "none";
    const a = (o) => {
      const t = Math.min(
        c.maxResizeWidth,
        Math.max(
          c.minResizeWidth,
          Math.round(c.startWidth + o.clientX - c.startX)
        )
      );
      h?.(c.columnKey, t), W(
        (n) => n[c.columnKey] === t ? n : {
          ...n,
          [c.columnKey]: t
        }
      );
    }, s = () => {
      p(void 0);
    };
    return window.addEventListener("pointermove", a), window.addEventListener("pointerup", s), window.addEventListener("pointercancel", s), () => {
      document.body.style.cursor = e, document.body.style.userSelect = i, window.removeEventListener("pointermove", a), window.removeEventListener("pointerup", s), window.removeEventListener("pointercancel", s);
    };
  }, [h, c]);
  const k = (e, i) => {
    if (!u || i.button !== 0)
      return;
    const a = r.find((n) => n.key === e), s = i.currentTarget.closest(".table-head-cell");
    if (!a || !s)
      return;
    i.preventDefault(), i.stopPropagation();
    const o = Math.max(a.minResizeWidth ?? g, b), t = Math.max(a.maxResizeWidth ?? D, o);
    p({
      columnKey: e,
      maxResizeWidth: t,
      minResizeWidth: o,
      startWidth: s.getBoundingClientRect().width,
      startX: i.clientX
    });
  }, v = (e) => {
    const i = r.find((o) => o.key === e), s = m.current[e] ?? (i?.minResizeWidth !== void 0 ? Math.max(i.minResizeWidth, b) : void 0);
    p(void 0), h?.(e, s), W((o) => {
      if (s === void 0) {
        if (!(e in o))
          return o;
        const t = { ...o };
        return delete t[e], t;
      }
      return o[e] === s ? o : {
        ...o,
        [e]: s
      };
    });
  }, M = r.map((e) => {
    const i = C[e.key];
    return i === void 0 ? e : {
      ...e,
      width: i
    };
  }), x = X(
    () => /* @__PURE__ */ new Set([
      ...r.flatMap(
        (e) => f(e.width, m.current[e.key]) ? [] : [e.key]
      ),
      ...Object.entries(C).flatMap(
        ([e, i]) => f(i, m.current[e]) ? [] : [e]
      )
    ]),
    [r, C]
  );
  return {
    handleColumnResizeReset: v,
    handleColumnResizeStart: k,
    lastResizableColumnKey: r.at(-1)?.key,
    resettableColumnKeys: x,
    resizedColumns: M
  };
};
export {
  P as useResizableColumns
};
//# sourceMappingURL=useResizableColumns.js.map
