import CommandActionForm_default from "./CommandActionForm.vue.js";
import CommandActionInput_default from "./CommandActionInput.vue.js";
import { computed, createBlock, createCommentVNode, createElementVNode, createTextVNode, createVNode, defineComponent, normalizeClass, openBlock, ref, toDisplayString, unref, withCtx } from "vue";
import { ScalarButton, ScalarIcon, ScalarListbox } from "@scalar/components";
var CommandPaletteTag_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	name: "CommandPaletteTag",
	props: {
		workspaceStore: {},
		eventBus: {},
		documentName: {},
		tag: {}
	},
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		const isEditMode = computed(() => __props.tag !== void 0);
		const name = ref(__props.tag?.name ?? "");
		const nameTrimmed = computed(() => name.value.trim());
		/** All available documents (collections) in the workspace */
		const availableDocuments = computed(() => Object.entries(__props.workspaceStore.workspace.documents).map(([name, document]) => ({
			id: name,
			label: document.info.title || name
		})));
		const selectedDocument = ref(__props.documentName ? availableDocuments.value.find((document) => document.id === __props.documentName) : availableDocuments.value[0] ?? void 0);
		/**
		* Check if the form should be disabled.
		*
		* In edit mode, disabled when:
		* - Tag name is empty
		* - Name is unchanged from the original
		* - The new name conflicts with an existing tag in the same document
		*
		* In create mode, disabled when:
		* - Tag name is empty
		* - No collection is selected
		* - The selected document does not exist
		* - A tag with the same name already exists in the selected document
		*/
		const isDisabled = computed(() => {
			const document = __props.workspaceStore.workspace.documents[selectedDocument.value?.id ?? ""];
			if (!nameTrimmed.value || !selectedDocument.value || !document) return true;
			if (isEditMode.value) {
				if (nameTrimmed.value === __props.tag?.name) return true;
			}
			if (document.tags?.some((tag) => tag.name === nameTrimmed.value)) return true;
			return false;
		});
		/**
		* Handle form submission.
		* In edit mode, emits the new name. In create mode, creates the tag via the event bus.
		*/
		const handleSubmit = () => {
			if (isDisabled.value || !selectedDocument.value) return;
			if (isEditMode.value && __props.tag) {
				__props.eventBus.emit("tag:edit:tag", {
					tag: __props.tag,
					documentName: selectedDocument.value.id,
					newName: nameTrimmed.value
				}, { skipUnpackProxy: true });
				emit("close");
				return;
			}
			__props.eventBus.emit("tag:create:tag", {
				name: nameTrimmed.value,
				documentName: selectedDocument.value.id
			});
			emit("close");
		};
		/** Handle back navigation when user presses backspace on empty input */
		const handleBack = (event) => {
			if (isEditMode.value) return;
			emit("back", event);
		};
		/** Handle cancel action in edit mode */
		const handleCancel = () => {
			emit("close");
		};
		return (_ctx, _cache) => {
			return openBlock(), createBlock(CommandActionForm_default, {
				disabled: isDisabled.value,
				onSubmit: handleSubmit
			}, {
				options: withCtx(() => [!isEditMode.value ? (openBlock(), createBlock(unref(ScalarListbox), {
					key: 0,
					modelValue: selectedDocument.value,
					"onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => selectedDocument.value = $event),
					options: availableDocuments.value
				}, {
					default: withCtx(() => [createVNode(unref(ScalarButton), {
						class: "hover:bg-b-2 max-h-8 w-fit justify-between gap-1 p-2 text-xs",
						variant: "outlined"
					}, {
						default: withCtx(() => [createElementVNode("span", { class: normalizeClass(selectedDocument.value ? "text-c-1" : "text-c-3") }, toDisplayString(selectedDocument.value ? selectedDocument.value.label : "Select Collection"), 3), createVNode(unref(ScalarIcon), {
							class: "text-c-3",
							icon: "ChevronDown",
							size: "md"
						})]),
						_: 1
					})]),
					_: 1
				}, 8, ["modelValue", "options"])) : createCommentVNode("", true), isEditMode.value ? (openBlock(), createBlock(unref(ScalarButton), {
					key: 1,
					class: "max-h-8 px-3 text-xs",
					variant: "outlined",
					onClick: handleCancel
				}, {
					default: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode(" Cancel ", -1)])]),
					_: 1
				})) : createCommentVNode("", true)]),
				submit: withCtx(() => [createTextVNode(toDisplayString(isEditMode.value ? "Save" : "Create Tag"), 1)]),
				default: withCtx(() => [createVNode(CommandActionInput_default, {
					modelValue: name.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => name.value = $event),
					label: "Tag Name",
					placeholder: "Tag Name",
					onDelete: handleBack
				}, null, 8, ["modelValue"])]),
				_: 1
			}, 8, ["disabled"]);
		};
	}
});
//#endregion
export { CommandPaletteTag_vue_vue_type_script_setup_true_lang_default as default };

//# sourceMappingURL=CommandPaletteTag.vue.script.js.map