import { n as elementDefaultStyle } from "./registry-CjoawJ_B.js";
import { computed, ref } from "vue";
import { defineStore } from "pinia";
//#region src/stores/elementListBuilderStore.ts
var idCounter = 0;
function nextId(prefix) {
	idCounter += 1;
	return `${prefix}-${Date.now()}-${idCounter}`;
}
/** Test helper to make ids deterministic. */
function __resetElementListBuilderIdCounter() {
	idCounter = 0;
}
function emptySchema() {
	return { elements: [] };
}
var useElementListBuilderStore = defineStore("gx-modui/elementListBuilder", () => {
	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] = { elements: schema.elements.map((el) => ({ ...el })) };
		selections.value[builderId] = { id: null };
	}
	function resetBuilder(builderId) {
		delete builders.value[builderId];
		delete selections.value[builderId];
	}
	/**
	* Append a fresh element of `type` (dot-notation or category) to
	* the flat list. Returns the new element's id.
	*/
	function addElement(builderId, type) {
		const schema = ensureBuilder(builderId);
		const dotType = type.includes(".") ? type : `${type}.${elementDefaultStyle[type] ?? "text"}`;
		const id = nextId(`el-${dotType.replace(".", "-")}`);
		const element = {
			id,
			type: dotType,
			label: `New ${dotType}`
		};
		schema.elements = [...schema.elements, element];
		return id;
	}
	/** Remove an element by id. */
	function removeElement(builderId, elementId) {
		const schema = ensureBuilder(builderId);
		schema.elements = schema.elements.filter((el) => el.id !== elementId);
		if (selections.value[builderId]?.id === elementId) selections.value[builderId] = { id: null };
	}
	/** Move an element by index within the list. */
	function reorderElement(builderId, fromIndex, toIndex) {
		const schema = ensureBuilder(builderId);
		const list = schema.elements;
		if (fromIndex < 0 || fromIndex >= list.length || toIndex < 0 || toIndex >= list.length || fromIndex === toIndex) return;
		const next = [...list];
		const [moved] = next.splice(fromIndex, 1);
		if (moved !== void 0) {
			next.splice(toIndex, 0, moved);
			schema.elements = next;
		}
	}
	/** Patch an element's properties (used by the Inspector). */
	function updateElement(builderId, elementId, patch) {
		const schema = ensureBuilder(builderId);
		const idx = schema.elements.findIndex((el) => el.id === elementId);
		if (idx < 0) return;
		const next = {
			...schema.elements[idx],
			...patch
		};
		const list = [...schema.elements];
		list[idx] = next;
		schema.elements = list;
	}
	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,
		addElement,
		removeElement,
		reorderElement,
		updateElement,
		select,
		getSelection,
		getSchema
	};
});
/**
* Composable wrapper around `useElementListBuilderStore` for a single
* builder instance.
*/
function useElementListBuilder(builderId) {
	const store = useElementListBuilderStore();
	store.ensureBuilder(builderId);
	return {
		schema: computed(() => store.getSchema(builderId)),
		selection: computed(() => store.getSelection(builderId)),
		addElement: (type) => store.addElement(builderId, type),
		removeElement: (id) => store.removeElement(builderId, id),
		reorderElement: (from, to) => store.reorderElement(builderId, from, to),
		updateElement: (id, patch) => store.updateElement(builderId, id, patch),
		select: (id) => store.select(builderId, id),
		loadSchema: (schema) => store.loadSchema(builderId, schema),
		reset: () => store.resetBuilder(builderId)
	};
}
//#endregion
export { useElementListBuilder as n, useElementListBuilderStore as r, __resetElementListBuilderIdCounter as t };

//# sourceMappingURL=elementListBuilderStore-C7trqNLO.js.map