import { computed, ref } from "vue";
import { defineStore } from "pinia";
//#region src/stores/gridBuilderStore.ts
var idCounter = 0;
function nextId(prefix) {
	idCounter += 1;
	return `${prefix}-${Date.now()}-${idCounter}`;
}
/** Test helper to make ids deterministic. */
function __resetGridBuilderIdCounter() {
	idCounter = 0;
}
function emptySchema() {
	return {
		cols: 12,
		rowHeight: 60,
		items: []
	};
}
var useGridBuilderStore = defineStore("gx-modui/gridBuilder", () => {
	/**
	* Per-builder state, keyed by `builderId`. Lets multiple builders
	* coexist on the same page without colliding.
	*/
	const builders = ref({});
	const selections = ref({});
	function ensureBuilder(builderId) {
		if (!builders.value[builderId]) {
			builders.value[builderId] = emptySchema();
			selections.value[builderId] = { id: null };
		}
		return builders.value[builderId];
	}
	function loadSchema(builderId, schema) {
		builders.value[builderId] = {
			cols: schema.cols,
			rowHeight: schema.rowHeight,
			items: schema.items.map((it) => ({ ...it }))
		};
		selections.value[builderId] = { id: null };
	}
	function resetBuilder(builderId) {
		delete builders.value[builderId];
		delete selections.value[builderId];
	}
	/**
	* Append a fresh grid item at the next available row, sized to
	* `template`. Returns the new item's id.
	*/
	function addItem(builderId, template) {
		const schema = ensureBuilder(builderId);
		const w = clampColumns(template?.w ?? 1, schema.cols);
		const h = Math.max(1, template?.h ?? 1);
		const y = nextFreeRow(schema);
		const id = nextId("gi");
		const item = {
			id,
			label: template?.label ?? `Cell ${schema.items.length + 1}`,
			x: 0,
			y,
			w,
			h
		};
		schema.items = [...schema.items, item];
		return id;
	}
	/** Remove an item by id. */
	function removeItem(builderId, itemId) {
		const schema = ensureBuilder(builderId);
		schema.items = schema.items.filter((it) => it.id !== itemId);
		if (selections.value[builderId]?.id === itemId) selections.value[builderId] = { id: null };
	}
	/** Patch a grid item's properties. */
	function updateItem(builderId, itemId, patch) {
		const schema = ensureBuilder(builderId);
		const idx = schema.items.findIndex((it) => it.id === itemId);
		if (idx < 0) return;
		const merged = {
			...schema.items[idx],
			...patch
		};
		merged.w = clampColumns(merged.w, schema.cols);
		merged.h = Math.max(1, merged.h);
		merged.x = Math.max(0, Math.min(merged.x, schema.cols - merged.w));
		merged.y = Math.max(0, merged.y);
		const next = [...schema.items];
		next[idx] = merged;
		schema.items = next;
	}
	/** Update the grid-level `cols` value. */
	function setCols(builderId, cols) {
		const schema = ensureBuilder(builderId);
		const safe = Math.max(1, Math.floor(cols));
		schema.cols = safe;
		schema.items = schema.items.map((it) => {
			const w = clampColumns(it.w, safe);
			const x = Math.max(0, Math.min(it.x, safe - w));
			return {
				...it,
				w,
				x
			};
		});
	}
	/** Update the grid-level `rowHeight` value. */
	function setRowHeight(builderId, rowHeight) {
		const schema = ensureBuilder(builderId);
		schema.rowHeight = Math.max(1, Math.floor(rowHeight));
	}
	function select(builderId, id) {
		selections.value[builderId] = { id };
	}
	function getSelection(builderId) {
		return selections.value[builderId] ?? { id: null };
	}
	function getSchema(builderId) {
		return ensureBuilder(builderId);
	}
	return {
		builders,
		selections,
		ensureBuilder,
		loadSchema,
		resetBuilder,
		addItem,
		removeItem,
		updateItem,
		setCols,
		setRowHeight,
		select,
		getSelection,
		getSchema
	};
});
function clampColumns(w, cols) {
	if (!Number.isFinite(w)) return 1;
	return Math.max(1, Math.min(Math.floor(w), cols));
}
/**
* Returns the first row index where a new full-width item could be
* placed without overlapping. The MVP only cares about appending —
* we use the max occupied row + height.
*/
function nextFreeRow(schema) {
	if (schema.items.length === 0) return 0;
	return schema.items.reduce((max, it) => Math.max(max, it.y + it.h), 0);
}
/** Composable wrapping store access for a single builder instance. */
function useGridBuilder(builderId) {
	const store = useGridBuilderStore();
	store.ensureBuilder(builderId);
	return {
		schema: computed(() => store.getSchema(builderId)),
		selection: computed(() => store.getSelection(builderId)),
		addItem: (template) => store.addItem(builderId, template),
		removeItem: (id) => store.removeItem(builderId, id),
		updateItem: (id, patch) => store.updateItem(builderId, id, patch),
		setCols: (cols) => store.setCols(builderId, cols),
		setRowHeight: (rowHeight) => store.setRowHeight(builderId, rowHeight),
		select: (id) => store.select(builderId, id),
		loadSchema: (schema) => store.loadSchema(builderId, schema),
		reset: () => store.resetBuilder(builderId)
	};
}
//#endregion
export { useGridBuilder as n, useGridBuilderStore as r, __resetGridBuilderIdCounter as t };

//# sourceMappingURL=gridBuilderStore-D1Gd1Hlj.js.map