import { n as __exportAll } from "./chunk-B-1-B7_t.js";
import { r as useFormBuilder } from "./formBuilderStore-DAIxcb_9.js";
import { n as cn } from "./utils-DXzSsU1e.js";
import { n as Badge_default } from "./badge-BbQrSqE-.js";
import { n as CardTitle_default, o as CardContent_default, r as CardHeader_default, s as Card_default } from "./card-X74FuL7B.js";
import { t as Input_default } from "./input-CwDp-vE8.js";
import { n as Label_default } from "./label-C5K3zowH.js";
import { t as Textarea_default } from "./textarea-Bi7Nstc4.js";
import { n as usePresetBuilder } from "./presetBuilderStore-BfY6aqur.js";
import { t as FieldFrame_default } from "./field-frame-DhCOWcMP.js";
import { t as FormBuilder_default } from "./FormBuilder-CCntZveE.js";
import { computed, createBlock, createElementVNode, createTextVNode, createVNode, defineComponent, mergeModels, normalizeClass, onMounted, openBlock, ref, unref, useModel, watch, withCtx } from "vue";
//#region src/components/compositions/elements/builder/PresetBuilder.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex items-center justify-between gap-2" };
var _hoisted_2 = { class: "grid gap-3 sm:grid-cols-2" };
var _hoisted_3 = { class: "flex flex-col gap-1" };
var _hoisted_4 = { class: "flex flex-col gap-1" };
var PresetBuilder_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "PresetBuilder",
	props: /* @__PURE__ */ mergeModels({
		modelValue: {},
		builderId: {},
		name: {},
		item: { default: () => ({}) },
		classList: {},
		label: {},
		hideLabel: { type: Boolean },
		helpText: {},
		errors: { default: () => [] }
	}, {
		"modelValue": {},
		"modelModifiers": {}
	}),
	emits: ["update:modelValue"],
	setup(__props) {
		/**
		* PresetBuilder — wraps a FormBuilder schema with a named, described
		* preset envelope.
		*
		* Layout:
		*   - Canvas header: preset name + description inputs.
		*   - Canvas body:   embedded FormBuilder bound to the inner schema.
		*
		* State for the name + description lives in `presetBuilderStore`. The
		* inner schema is owned by `formBuilderStore` (keyed off a derived
		* `innerBuilderId`) so all existing FormBuilder behaviours work
		* unchanged.
		*
		* Two-way binds the full `PresetBuilderSchema` via `defineModel`.
		*/
		const props = __props;
		const model = useModel(__props, "modelValue");
		let nextAutoId = 0;
		function makeAutoId() {
			nextAutoId += 1;
			return `preset-builder-${Date.now()}-${nextAutoId}`;
		}
		const fallbackId = ref(makeAutoId());
		const builder = usePresetBuilder(computed(() => props.builderId ?? fallbackId.value).value);
		const innerBuilderId = computed(() => builder.state.value.innerBuilderId);
		const inner = useFormBuilder(innerBuilderId.value);
		function isPresetSchema(value) {
			if (typeof value !== "object" || value === null) return false;
			const v = value;
			return typeof v.presetName === "string" && typeof v.schema === "object" && v.schema !== null;
		}
		/** Seed both stores from the initial modelValue. */
		onMounted(() => {
			const seed = model.value;
			if (isPresetSchema(seed)) {
				builder.loadMeta({
					presetName: seed.presetName,
					presetDescription: seed.presetDescription
				});
				inner.loadSchema(seed.schema);
			}
		});
		const innerSchemaRef = computed(() => inner.schema.value);
		const stateRef = computed(() => builder.state.value);
		/** Push schema changes back via v-model. */
		watch([stateRef, innerSchemaRef], ([state, schema]) => {
			model.value = {
				presetName: state.presetName,
				presetDescription: state.presetDescription,
				schema
			};
		}, { deep: true });
		function onNameInput(e) {
			builder.setName(e.target.value);
		}
		function onDescriptionInput(e) {
			builder.setDescription(e.target.value);
		}
		return (_ctx, _cache) => {
			return openBlock(), createBlock(unref(FieldFrame_default), {
				label: props.label,
				"hide-label": props.hideLabel,
				"help-text": props.helpText,
				errors: props.errors,
				class: normalizeClass(props.classList)
			}, {
				default: withCtx(() => [createVNode(unref(Card_default), {
					class: "gxp-preset-builder",
					"data-testid": "preset-builder",
					"data-element-name": props.name ?? void 0
				}, {
					default: withCtx(() => [createVNode(unref(CardHeader_default), null, {
						default: withCtx(() => [createElementVNode("div", _hoisted_1, [createVNode(unref(CardTitle_default), { class: "text-base" }, {
							default: withCtx(() => [..._cache[0] || (_cache[0] = [createTextVNode("Preset", -1)])]),
							_: 1
						}), createVNode(unref(Badge_default), {
							variant: "outline",
							class: "font-mono text-[10px]"
						}, {
							default: withCtx(() => [..._cache[1] || (_cache[1] = [createTextVNode(" builder/preset-builder ", -1)])]),
							_: 1
						})])]),
						_: 1
					}), createVNode(unref(CardContent_default), { class: "flex flex-col gap-4" }, {
						default: withCtx(() => [createElementVNode("div", _hoisted_2, [createElementVNode("div", _hoisted_3, [createVNode(unref(Label_default), { for: "preset-builder-name" }, {
							default: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode("Preset name", -1)])]),
							_: 1
						}), createVNode(unref(Input_default), {
							id: "preset-builder-name",
							"model-value": stateRef.value.presetName,
							placeholder: "My preset",
							"data-testid": "preset-builder-name",
							onInput: onNameInput
						}, null, 8, ["model-value"])]), createElementVNode("div", _hoisted_4, [createVNode(unref(Label_default), { for: "preset-builder-description" }, {
							default: withCtx(() => [..._cache[3] || (_cache[3] = [createTextVNode("Description", -1)])]),
							_: 1
						}), createVNode(unref(Textarea_default), {
							id: "preset-builder-description",
							"model-value": stateRef.value.presetDescription,
							placeholder: "Optional description",
							"data-testid": "preset-builder-description",
							onInput: onDescriptionInput
						}, null, 8, ["model-value"])])]), createElementVNode("div", {
							class: normalizeClass(unref(cn)("rounded-md border bg-card/40 p-2")),
							"data-testid": "preset-builder-inner"
						}, [createVNode(FormBuilder_default, {
							"builder-id": innerBuilderId.value,
							item: props.item
						}, null, 8, ["builder-id", "item"])], 2)]),
						_: 1
					})]),
					_: 1
				}, 8, ["data-element-name"])]),
				_: 1
			}, 8, [
				"label",
				"hide-label",
				"help-text",
				"errors",
				"class"
			]);
		};
	}
});
//#endregion
//#region src/components/compositions/elements/builder/PresetBuilder.vue
var PresetBuilder_exports = /* @__PURE__ */ __exportAll({ default: () => PresetBuilder_default });
var PresetBuilder_default = PresetBuilder_vue_vue_type_script_setup_true_lang_default;
//#endregion
export { PresetBuilder_exports as n, PresetBuilder_default as t };

//# sourceMappingURL=PresetBuilder-C7lFfxys.js.map