import IconSelector_default from "../../../../components/IconSelector.vue.js";
import CommandActionForm_default from "./CommandActionForm.vue.js";
import CommandActionInput_default from "./CommandActionInput.vue.js";
import { computed, createBlock, createTextVNode, createVNode, defineComponent, openBlock, ref, unref, withCtx } from "vue";
import { ScalarButton } from "@scalar/components";
import { useRouter } from "vue-router";
import { LibraryIcon } from "@scalar/icons/library";
var CommandPaletteOpenApiDocument_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "CommandPaletteOpenApiDocument",
	props: {
		workspaceStore: {},
		eventBus: {}
	},
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		const router = useRouter();
		const documentName = ref("");
		const documentNameTrimmed = computed(() => documentName.value.trim());
		/** Default icon for new documents (folder icon) */
		const documentIcon = ref("interface-content-folder");
		/**
		* Check if the form should be disabled.
		* Disabled when the name is empty or when a document with that name already exists.
		*/
		const isDisabled = computed(() => {
			if (!documentNameTrimmed.value) return true;
			/** Prevent duplicate document names in the workspace */
			if (__props.workspaceStore.workspace.documents[documentNameTrimmed.value] !== void 0) return true;
			return false;
		});
		/** Handle form submission to create a new document */
		const handleSubmit = () => {
			if (isDisabled.value) return;
			__props.eventBus.emit("document:create:empty-document", {
				name: documentNameTrimmed.value,
				icon: documentIcon.value,
				callback: (success) => {
					if (success) router.push({
						name: "document.overview",
						params: { documentSlug: documentNameTrimmed.value }
					});
				}
			});
			emit("close");
		};
		/** Handle back navigation when user presses backspace on empty input */
		const handleBack = (event) => {
			emit("back", event);
		};
		return (_ctx, _cache) => {
			return openBlock(), createBlock(CommandActionForm_default, {
				disabled: isDisabled.value,
				onSubmit: handleSubmit
			}, {
				options: withCtx(() => [createVNode(IconSelector_default, {
					modelValue: documentIcon.value,
					"onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => documentIcon.value = $event),
					placement: "bottom-start"
				}, {
					default: withCtx(() => [createVNode(unref(ScalarButton), {
						class: "aspect-square h-auto px-0",
						variant: "outlined"
					}, {
						default: withCtx(() => [createVNode(unref(LibraryIcon), {
							class: "text-c-2 size-4 stroke-[1.75]",
							src: documentIcon.value
						}, null, 8, ["src"])]),
						_: 1
					})]),
					_: 1
				}, 8, ["modelValue"])]),
				submit: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode("Create Document", -1)])]),
				default: withCtx(() => [createVNode(CommandActionInput_default, {
					modelValue: documentName.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => documentName.value = $event),
					label: "Document Name",
					placeholder: "Document Name",
					onDelete: handleBack
				}, null, 8, ["modelValue"])]),
				_: 1
			}, 8, ["disabled"]);
		};
	}
});
//#endregion
export { CommandPaletteOpenApiDocument_vue_vue_type_script_setup_true_lang_default as default };

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