import { reactive as S, watch as u, computed as c, provide as L } from "vue";
import { deepClone as h, tap as m, clamp as p, deepEqual as R, delay as C, resolveAwaited as v } from "@milaboratories/helpers";
import { stateKey as x } from "./keys.js";
import { useTableColumns as D } from "./composition/useTableColumns.js";
import { useTableRows as j } from "./composition/useTableRows.js";
import { GAP as w, WINDOW_DELTA as g } from "./constants.js";
const A = async (l, t) => {
  const { scrollTop: r, bodyHeight: i } = l;
  return await C(0), v({
    rows: t.getRows(r, i),
    dataWindow: l
  });
};
function q(l) {
  const t = S({
    rowIndex: -1,
    columns: [],
    pendingLoads: 0,
    currentWindow: void 0,
    rows: [],
    resize: !1,
    resizeTh: void 0,
    dataHeight: 0,
    bodyHeight: l.settings.height,
    bodyWidth: 0,
    scrollTop: 0,
    scrollLeft: 0,
    selectedRows: /* @__PURE__ */ new Set(),
    selectedColumns: /* @__PURE__ */ new Set()
  });
  u(
    () => l.settings,
    (e) => {
      t.columns = h(e.columns), t.currentWindow = void 0, e.dataSource.getHeight().then((o) => {
        t.dataHeight = o, d.updateBodyHeight();
      }).catch((o) => t.error = o);
    },
    { immediate: !0 }
  );
  const r = c(() => l.settings), i = c(() => t.columns.reduce((e, o) => e + o.width + w, 0)), f = c(() => m(d.data.dataHeight - d.data.bodyHeight, (e) => e > 0 ? e : 0)), y = c(() => m(i.value - d.data.bodyWidth, (e) => e > 0 ? e : 0)), T = c(() => ({
    bodyHeight: t.bodyHeight,
    scrollTop: t.scrollTop,
    current: t.currentWindow
  })), n = D({
    data: t,
    settings: r
  }), b = j(t, n), d = {
    data: t,
    settings: r,
    tableColumns: n,
    tableRows: b,
    adjustWidth: () => {
      const e = t.columns.reduce((s, a) => s + a.width + w, 0), o = t.bodyWidth + t.scrollLeft;
      if (e < o) {
        const s = t.columns[t.columns.length - 1];
        s.width = s.width + (o - e);
      }
    },
    updateOffsets(e) {
      this.updateScrollTop(t.scrollTop + e.deltaY), this.updateScrollLeft(t.scrollLeft + e.deltaX);
    },
    getSelectedRows() {
      return t.rows.filter((e) => t.selectedRows.has(e.primaryKey));
    },
    getSelectedColumns() {
      return t.columns.filter((e) => t.selectedColumns.has(e.id));
    },
    selectRow(e) {
      t.selectedRows.add(e);
    },
    unselectRow(e) {
      t.selectedRows.delete(e);
    },
    selectColumn(e) {
      t.selectedColumns.add(e);
    },
    unselectColumn(e) {
      t.selectedColumns.delete(e);
    },
    updateScrollTop(e) {
      t.scrollTop = p(e, 0, f.value);
    },
    updateScrollLeft(e) {
      t.scrollLeft = p(e, 0, y.value);
    },
    updateBodyHeight() {
      const { height: e } = l.settings, { dataHeight: o } = t, s = e > o ? o : e;
      t.bodyHeight = s;
    },
    updateDimensions(e) {
      this.updateBodyHeight(), d.data.bodyWidth = e.width, d.adjustWidth(), t.rows = [], t.currentWindow = void 0;
    }
  };
  return u(
    T,
    (e, o) => {
      const s = e.current;
      (!s || e.scrollTop < s.scrollTop || e.scrollTop + e.bodyHeight > s.bodyHeight + s.scrollTop) && (t.currentWindow = {
        scrollTop: e.scrollTop - g,
        bodyHeight: e.bodyHeight + g * 2
      }, A(h(t.currentWindow), r.value.dataSource).then(({ rows: H, dataWindow: W }) => {
        R(t.currentWindow, W) && (t.rows = H);
      }));
    },
    { deep: !0, immediate: !0 }
  ), L(x, d), d;
}
export {
  q as createState
};
//# sourceMappingURL=state.js.map
