import HttpMethod_default from "../../../blocks/operation-code-sample/components/HttpMethod.vue.js";
import CommandActionForm_default from "./CommandActionForm.vue.js";
import CommandActionInput_default from "./CommandActionInput.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, normalizeClass, openBlock, ref, renderList, toDisplayString, unref, watch, withCtx } from "vue";
import { ScalarButton, ScalarDropdown, ScalarDropdownItem, ScalarIcon, ScalarListbox } from "@scalar/components";
import { useRouter } from "vue-router";
import { HTTP_METHODS } from "@scalar/helpers/http/http-methods";
//#region src/v2/features/command-palette/components/CommandPaletteRequest.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex flex-1 gap-1" };
var _hoisted_2 = { class: "flex items-center gap-2" };
var _hoisted_3 = { class: "custom-scroll max-h-40" };
var _hoisted_4 = { class: "custom-scroll max-h-40" };
var _hoisted_5 = { class: "truncate" };
var CommandPaletteRequest_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	name: "CommandPaletteRequest",
	props: {
		workspaceStore: {},
		eventBus: {},
		documentName: {},
		tagId: {}
	},
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		/** HTTP method option type for selectors */
		const router = useRouter();
		const requestPath = ref("/");
		const requestPathTrimmed = computed(() => requestPath.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
		})));
		/** Available HTTP methods for the dropdown (GET, POST, PUT, etc.) */
		const availableMethods = HTTP_METHODS.map((method) => ({
			id: method,
			label: method.toUpperCase(),
			method
		}));
		const selectedDocument = ref(__props.documentName ? availableDocuments.value.find((document) => document.id === __props.documentName) : availableDocuments.value[0] ?? void 0);
		const selectedMethod = ref(availableMethods.find((method) => method.method === "get"));
		/**
		* All available tags for the selected document.
		* Includes a "No Tag" option for operations without a tag assignment.
		*/
		const availableTags = computed(() => {
			if (!selectedDocument.value) return [];
			const document = __props.workspaceStore.workspace.documents[selectedDocument.value.id];
			if (!document) return [];
			return [{
				id: "",
				label: "No Tag"
			}, ...document.tags?.map((tag) => ({
				id: tag.name,
				label: tag.name
			})) ?? []];
		});
		const selectedTag = ref(__props.tagId ? availableTags.value.find((tag) => tag.id === __props.tagId) : void 0);
		watch(selectedDocument, () => {
			selectedTag.value = availableTags.value.find((tag) => tag.id === "");
		});
		/**
		* Check if an operation with the same path and method already exists.
		* Used to prevent creating duplicate operations.
		*/
		const operationExists = computed(() => {
			if (!selectedDocument.value || !selectedMethod.value || !requestPathTrimmed.value) return false;
			const document = __props.workspaceStore.workspace.documents[selectedDocument.value.id];
			/** Ensure path starts with '/' for consistent lookup */
			const normalizedPath = requestPathTrimmed.value.startsWith("/") ? requestPathTrimmed.value : `/${requestPathTrimmed.value}`;
			return !!document?.paths?.[normalizedPath]?.[selectedMethod.value.method];
		});
		/**
		* Check if the form should be disabled.
		* Disabled when any required field is missing or operation already exists.
		*/
		const isDisabled = computed(() => {
			if (!requestPathTrimmed.value || !selectedDocument.value || !selectedMethod.value) return true;
			/** Prevent creating duplicate operations */
			if (operationExists.value) return true;
			return false;
		});
		/** Handle HTTP method selection from dropdown */
		const handleSelectMethod = (method) => {
			if (method) selectedMethod.value = method;
		};
		/** Handle tag selection from dropdown */
		const handleSelectTag = (tag) => {
			if (tag) selectedTag.value = tag;
		};
		/**
		* Create the request and close the command palette.
		* Emits an event to create a new operation with the specified details.
		*/
		const handleSubmit = () => {
			if (isDisabled.value || !selectedDocument.value || !selectedMethod.value) return;
			if (!__props.workspaceStore.workspace.documents[selectedDocument.value.id]) return;
			__props.eventBus.emit("operation:create:operation", {
				documentName: selectedDocument.value.id,
				path: requestPathTrimmed.value,
				method: selectedMethod.value.method,
				operation: { tags: selectedTag.value?.id ? [selectedTag.value.id] : void 0 },
				callback: (success) => {
					if (success) {
						/** Build the sidebar */
						__props.workspaceStore.buildSidebar(selectedDocument.value?.id ?? "");
						const path = requestPathTrimmed.value.startsWith("/") ? requestPathTrimmed.value : `/${requestPathTrimmed.value}`;
						/** Navigate to the example */
						router.push({
							name: "example",
							params: {
								documentSlug: selectedDocument.value?.id,
								pathEncoded: encodeURIComponent(path),
								method: selectedMethod.value?.method,
								exampleName: "default"
							}
						});
					}
				}
			});
			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(() => [createElementVNode("div", _hoisted_1, [
					createVNode(unref(ScalarListbox), {
						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-[150px] min-w-[150px] justify-between gap-1 p-2 text-xs",
							variant: "outlined"
						}, {
							default: withCtx(() => [createElementVNode("span", { class: normalizeClass(selectedDocument.value ? "text-c-1 truncate" : "text-c-3") }, toDisplayString(selectedDocument.value ? selectedDocument.value.label : "Select Document"), 3), createVNode(unref(ScalarIcon), {
								class: "text-c-3",
								icon: "ChevronDown",
								size: "md"
							})]),
							_: 1
						})]),
						_: 1
					}, 8, ["modelValue", "options"]),
					createVNode(unref(ScalarDropdown), {
						placement: "bottom",
						resize: ""
					}, {
						items: withCtx(() => [createElementVNode("div", _hoisted_3, [(openBlock(true), createElementBlock(Fragment, null, renderList(unref(availableMethods), (method) => {
							return openBlock(), createBlock(unref(ScalarDropdownItem), {
								key: method.id,
								class: "flex h-7 w-full items-center justify-center px-1",
								onClick: ($event) => handleSelectMethod(method)
							}, {
								default: withCtx(() => [createVNode(HttpMethod_default, { method: method.method }, null, 8, ["method"])]),
								_: 2
							}, 1032, ["onClick"]);
						}), 128))])]),
						default: withCtx(() => [createVNode(unref(ScalarButton), {
							class: "hover:bg-b-2 max-h-8 w-[100px] min-w-[100px] justify-between gap-1 p-2 text-xs",
							variant: "outlined"
						}, {
							default: withCtx(() => [createElementVNode("div", _hoisted_2, [selectedMethod.value ? (openBlock(), createBlock(HttpMethod_default, {
								key: 0,
								method: selectedMethod.value.method
							}, null, 8, ["method"])) : createCommentVNode("", true), createVNode(unref(ScalarIcon), {
								class: "text-c-3",
								icon: "ChevronDown",
								size: "md"
							})])]),
							_: 1
						})]),
						_: 1
					}),
					createVNode(unref(ScalarDropdown), {
						placement: "bottom",
						resize: ""
					}, {
						items: withCtx(() => [createElementVNode("div", _hoisted_4, [(openBlock(true), createElementBlock(Fragment, null, renderList(availableTags.value, (tag) => {
							return openBlock(), createBlock(unref(ScalarDropdownItem), {
								key: tag.id,
								class: "flex h-7 w-full items-center px-1",
								onClick: ($event) => handleSelectTag(tag)
							}, {
								default: withCtx(() => [createElementVNode("span", _hoisted_5, toDisplayString(tag.label), 1)]),
								_: 2
							}, 1032, ["onClick"]);
						}), 128))])]),
						default: withCtx(() => [createVNode(unref(ScalarButton), {
							class: "hover:bg-b-2 max-h-8 w-full justify-between gap-1 p-2 text-xs",
							disabled: !availableTags.value.length,
							variant: "outlined"
						}, {
							default: withCtx(() => [createElementVNode("span", { class: normalizeClass(selectedTag.value ? "text-c-1 truncate" : "text-c-3") }, toDisplayString(selectedTag.value ? selectedTag.value.label : "Select Tag (Optional)"), 3), createVNode(unref(ScalarIcon), {
								class: "text-c-3",
								icon: "ChevronDown",
								size: "md"
							})]),
							_: 1
						}, 8, ["disabled"])]),
						_: 1
					})
				])]),
				submit: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode("Create Request", -1)])]),
				default: withCtx(() => [createVNode(CommandActionInput_default, {
					modelValue: requestPath.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => requestPath.value = $event),
					label: "Request Path",
					placeholder: "/users",
					onDelete: handleBack
				}, null, 8, ["modelValue"])]),
				_: 1
			}, 8, ["disabled"]);
		};
	}
});
//#endregion
export { CommandPaletteRequest_vue_vue_type_script_setup_true_lang_default as default };

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