import { computed, ref } from "vue";
import { defineStore } from "pinia";
//#region src/stores/objectBuilderStore.ts
var idCounter = 0;
function nextId(prefix) {
	idCounter += 1;
	return `${prefix}-${Date.now()}-${idCounter}`;
}
/** Test helper to make ids deterministic. */
function __resetObjectBuilderIdCounter() {
	idCounter = 0;
}
/** Sensible default values for each entry type. */
function defaultValueForType(type) {
	switch (type) {
		case "string": return "";
		case "number": return 0;
		case "boolean": return false;
		case "array": return [];
		case "object": return {};
		case "null": return null;
	}
}
function emptySchema() {
	return { entries: [] };
}
/**
* Normalise a plain `Record<string, unknown>` into an
* `ObjectBuilderSchema`. Used when consumers pass a raw object via
* `modelValue`.
*/
function recordToSchema(record) {
	const entries = [];
	for (const [key, value] of Object.entries(record)) entries.push({
		id: nextId("obj-entry"),
		key,
		type: detectType(value),
		value
	});
	return { entries };
}
/** Flatten an `ObjectBuilderSchema` into a plain JS object. */
function toRecord(schema) {
	const out = {};
	for (const entry of schema.entries) {
		if (!entry.key) continue;
		out[entry.key] = entry.value;
	}
	return out;
}
function detectType(value) {
	if (value === null) return "null";
	if (Array.isArray(value)) return "array";
	const t = typeof value;
	if (t === "string") return "string";
	if (t === "number") return "number";
	if (t === "boolean") return "boolean";
	return "object";
}
var useObjectBuilderStore = defineStore("gx-modui/objectBuilder", () => {
	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] = { entries: schema.entries.map((entry) => ({ ...entry })) };
		selections.value[builderId] = { id: null };
	}
	function loadRecord(builderId, record) {
		loadSchema(builderId, recordToSchema(record));
	}
	function resetBuilder(builderId) {
		delete builders.value[builderId];
		delete selections.value[builderId];
	}
	/** Append a new entry of `type` and return its id. */
	function addEntry(builderId, type) {
		const schema = ensureBuilder(builderId);
		const id = nextId("obj-entry");
		schema.entries = [...schema.entries, {
			id,
			key: "",
			type,
			value: defaultValueForType(type)
		}];
		return id;
	}
	/** Remove an entry by id. */
	function removeEntry(builderId, entryId) {
		const schema = ensureBuilder(builderId);
		schema.entries = schema.entries.filter((e) => e.id !== entryId);
		if (selections.value[builderId]?.id === entryId) selections.value[builderId] = { id: null };
	}
	/** Move an entry from `fromIndex` to `toIndex`. */
	function reorderEntry(builderId, fromIndex, toIndex) {
		const schema = ensureBuilder(builderId);
		const list = schema.entries;
		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.entries = next;
		}
	}
	/** Patch an entry's fields. When `type` changes, the value resets. */
	function updateEntry(builderId, entryId, patch) {
		const schema = ensureBuilder(builderId);
		const idx = schema.entries.findIndex((e) => e.id === entryId);
		if (idx < 0) return;
		const current = schema.entries[idx];
		const next = {
			...current,
			...patch
		};
		if (patch.type && patch.type !== current.type && patch.value === void 0) next.value = defaultValueForType(patch.type);
		const list = [...schema.entries];
		list[idx] = next;
		schema.entries = 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,
		loadRecord,
		resetBuilder,
		addEntry,
		removeEntry,
		reorderEntry,
		updateEntry,
		select,
		getSelection,
		getSchema
	};
});
/** Composable wrapper around `useObjectBuilderStore` for one builder. */
function useObjectBuilder(builderId) {
	const store = useObjectBuilderStore();
	store.ensureBuilder(builderId);
	return {
		schema: computed(() => store.getSchema(builderId)),
		selection: computed(() => store.getSelection(builderId)),
		addEntry: (type) => store.addEntry(builderId, type),
		removeEntry: (id) => store.removeEntry(builderId, id),
		reorderEntry: (from, to) => store.reorderEntry(builderId, from, to),
		updateEntry: (id, patch) => store.updateEntry(builderId, id, patch),
		select: (id) => store.select(builderId, id),
		loadSchema: (schema) => store.loadSchema(builderId, schema),
		loadRecord: (record) => store.loadRecord(builderId, record),
		reset: () => store.resetBuilder(builderId)
	};
}
//#endregion
export { useObjectBuilder as a, toRecord as i, defaultValueForType as n, useObjectBuilderStore as o, recordToSchema as r, __resetObjectBuilderIdCounter as t };

//# sourceMappingURL=objectBuilderStore-DLGsjOjW.js.map