import { computed, ref } from "vue";
import { defineStore } from "pinia";
//#region src/stores/dependencyListBuilderStore.ts
var idCounter = 0;
function nextId(prefix) {
	idCounter += 1;
	return `${prefix}-${Date.now()}-${idCounter}`;
}
/** Test helper to make ids deterministic. */
function __resetDependencyListBuilderIdCounter() {
	idCounter = 0;
}
function emptySchema() {
	return { rules: [] };
}
function defaultCondition() {
	return {
		field: "",
		operator: "equals",
		value: ""
	};
}
function defaultAction() {
	return {
		type: "show",
		target: "",
		payload: void 0
	};
}
function buildRuleFromTemplate(template, id) {
	switch (template) {
		case "show-field": return {
			id,
			name: "Show field",
			enabled: true,
			conditions: [defaultCondition()],
			conditionsLogic: "AND",
			actions: [{
				type: "show",
				target: ""
			}]
		};
		case "hide-field": return {
			id,
			name: "Hide field",
			enabled: true,
			conditions: [defaultCondition()],
			conditionsLogic: "AND",
			actions: [{
				type: "hide",
				target: ""
			}]
		};
		case "set-value": return {
			id,
			name: "Set value",
			enabled: true,
			conditions: [defaultCondition()],
			conditionsLogic: "AND",
			actions: [{
				type: "set_value",
				target: "",
				payload: ""
			}]
		};
		default: return {
			id,
			name: "New rule",
			enabled: true,
			conditions: [],
			conditionsLogic: "AND",
			actions: []
		};
	}
}
var useDependencyListBuilderStore = defineStore("gx-modui/dependencyListBuilder", () => {
	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] = { rules: schema.rules.map((rule) => ({
			...rule,
			conditions: rule.conditions.map((c) => ({ ...c })),
			actions: rule.actions.map((a) => ({ ...a }))
		})) };
		selections.value[builderId] = { id: null };
	}
	function resetBuilder(builderId) {
		delete builders.value[builderId];
		delete selections.value[builderId];
	}
	/** Append a new rule built from `template` and return its id. */
	function addRule(builderId, template = "blank") {
		const schema = ensureBuilder(builderId);
		const id = nextId("rule");
		const rule = buildRuleFromTemplate(template, id);
		schema.rules = [...schema.rules, rule];
		return id;
	}
	function removeRule(builderId, ruleId) {
		const schema = ensureBuilder(builderId);
		schema.rules = schema.rules.filter((r) => r.id !== ruleId);
		if (selections.value[builderId]?.id === ruleId) selections.value[builderId] = { id: null };
	}
	function reorderRule(builderId, fromIndex, toIndex) {
		const schema = ensureBuilder(builderId);
		const list = schema.rules;
		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.rules = next;
		}
	}
	function updateRule(builderId, ruleId, patch) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		const list = [...schema.rules];
		list[idx] = {
			...current,
			...patch
		};
		schema.rules = list;
	}
	function addCondition(builderId, ruleId) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		const list = [...schema.rules];
		list[idx] = {
			...current,
			conditions: [...current.conditions, defaultCondition()]
		};
		schema.rules = list;
	}
	function updateCondition(builderId, ruleId, conditionIndex, patch) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		if (conditionIndex < 0 || conditionIndex >= current.conditions.length) return;
		const conditions = [...current.conditions];
		conditions[conditionIndex] = {
			...conditions[conditionIndex],
			...patch
		};
		const list = [...schema.rules];
		list[idx] = {
			...current,
			conditions
		};
		schema.rules = list;
	}
	function removeCondition(builderId, ruleId, conditionIndex) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		if (conditionIndex < 0 || conditionIndex >= current.conditions.length) return;
		const conditions = current.conditions.filter((_, i) => i !== conditionIndex);
		const list = [...schema.rules];
		list[idx] = {
			...current,
			conditions
		};
		schema.rules = list;
	}
	function addAction(builderId, ruleId) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		const list = [...schema.rules];
		list[idx] = {
			...current,
			actions: [...current.actions, defaultAction()]
		};
		schema.rules = list;
	}
	function updateAction(builderId, ruleId, actionIndex, patch) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		if (actionIndex < 0 || actionIndex >= current.actions.length) return;
		const actions = [...current.actions];
		actions[actionIndex] = {
			...actions[actionIndex],
			...patch
		};
		const list = [...schema.rules];
		list[idx] = {
			...current,
			actions
		};
		schema.rules = list;
	}
	function removeAction(builderId, ruleId, actionIndex) {
		const schema = ensureBuilder(builderId);
		const idx = schema.rules.findIndex((r) => r.id === ruleId);
		if (idx < 0) return;
		const current = schema.rules[idx];
		if (actionIndex < 0 || actionIndex >= current.actions.length) return;
		const actions = current.actions.filter((_, i) => i !== actionIndex);
		const list = [...schema.rules];
		list[idx] = {
			...current,
			actions
		};
		schema.rules = 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,
		addRule,
		removeRule,
		reorderRule,
		updateRule,
		addCondition,
		updateCondition,
		removeCondition,
		addAction,
		updateAction,
		removeAction,
		select,
		getSelection,
		getSchema
	};
});
/** Composable wrapper around `useDependencyListBuilderStore`. */
function useDependencyListBuilder(builderId) {
	const store = useDependencyListBuilderStore();
	store.ensureBuilder(builderId);
	return {
		schema: computed(() => store.getSchema(builderId)),
		selection: computed(() => store.getSelection(builderId)),
		addRule: (template) => store.addRule(builderId, template),
		removeRule: (id) => store.removeRule(builderId, id),
		reorderRule: (from, to) => store.reorderRule(builderId, from, to),
		updateRule: (id, patch) => store.updateRule(builderId, id, patch),
		addCondition: (id) => store.addCondition(builderId, id),
		updateCondition: (id, index, patch) => store.updateCondition(builderId, id, index, patch),
		removeCondition: (id, index) => store.removeCondition(builderId, id, index),
		addAction: (id) => store.addAction(builderId, id),
		updateAction: (id, index, patch) => store.updateAction(builderId, id, index, patch),
		removeAction: (id, index) => store.removeAction(builderId, id, index),
		select: (id) => store.select(builderId, id),
		loadSchema: (schema) => store.loadSchema(builderId, schema),
		reset: () => store.resetBuilder(builderId)
	};
}
//#endregion
export { useDependencyListBuilderStore as i, buildRuleFromTemplate as n, useDependencyListBuilder as r, __resetDependencyListBuilderIdCounter as t };

//# sourceMappingURL=dependencyListBuilderStore-DTxW2nCm.js.map