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

//# sourceMappingURL=state.js.map