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 { getOperationFromCurl } from "../helpers/get-operation-from-curl.js";
import { computed, createBlock, createElementVNode, createTextVNode, createVNode, defineComponent, normalizeClass, openBlock, ref, toDisplayString, unref, withCtx } from "vue";
import { ScalarButton, ScalarIcon, ScalarListbox } from "@scalar/components";
import { useRouter } from "vue-router";
//#region src/v2/features/command-palette/components/CommandPaletteImportCurl.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex flex-1 flex-col gap-2" };
var _hoisted_2 = { class: "flex h-9 flex-row items-center gap-2 rounded border p-[3px] text-sm" };
var _hoisted_3 = { class: "scroll-timeline-x whitespace-nowrap" };
var _hoisted_4 = { class: "flex items-center gap-2" };
var CommandPaletteImportCurl_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	name: "CommandPaletteImportCurl",
	props: {
		workspaceStore: {},
		eventBus: {},
		inputValue: {}
	},
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		const router = useRouter();
		const exampleKey = ref("");
		/** Trimmed version of the example key for validation and submission */
		const exampleKeyTrimmed = computed(() => exampleKey.value.trim());
		/** Parse the cURL command to extract path, method, and operation details */
		const { path, method, operation } = getOperationFromCurl(__props.inputValue);
		/** List of all available documents (collections) in the workspace */
		const documents = computed(() => Object.keys(__props.workspaceStore.workspace.documents).map((document) => ({
			id: document,
			label: document
		})));
		const selectedDocument = ref(documents.value[0]);
		/**
		* Check if the form should be disabled.
		* Disabled when:
		* - Example key is empty
		* - No document is selected
		* - An operation with the same path and method already exists in the selected document
		*/
		const isDisabled = computed(() => {
			if (!exampleKeyTrimmed.value || !selectedDocument.value) return true;
			if (__props.workspaceStore.workspace.documents[selectedDocument.value.id]?.paths?.[path]?.[method]) return true;
			return false;
		});
		/**
		* Handle the import submission.
		* Creates a new operation in the selected document from the parsed cURL command.
		*/
		const handleImportClick = () => {
			const documentName = selectedDocument.value;
			if (isDisabled.value || !documentName) return;
			/** Re-parse with the example key to include it in the operation */
			const result = getOperationFromCurl(__props.inputValue, exampleKeyTrimmed.value);
			__props.eventBus.emit("operation:create:operation", {
				documentName: documentName.id,
				path: result.path,
				method: result.method,
				operation: result.operation,
				exampleKey: exampleKeyTrimmed.value,
				callback: (success) => {
					if (success) {
						__props.workspaceStore.buildSidebar(documentName.id);
						const path = result.path.startsWith("/") ? result.path : `/${result.path}`;
						router.push({
							name: "example",
							params: {
								documentSlug: documentName.id,
								pathEncoded: encodeURIComponent(path),
								method: result.method,
								exampleName: exampleKeyTrimmed.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: handleImportClick
			}, {
				options: withCtx(() => [createElementVNode("div", _hoisted_4, [createVNode(unref(ScalarListbox), {
					modelValue: selectedDocument.value,
					"onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => selectedDocument.value = $event),
					options: documents.value
				}, {
					default: withCtx(() => [createVNode(unref(ScalarButton), {
						class: "hover:bg-b-2 max-h-8 w-full justify-between gap-1 p-2 text-xs",
						variant: "outlined"
					}, {
						default: withCtx(() => [createElementVNode("span", { class: normalizeClass(["whitespace-nowrap", 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"])])]),
				submit: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode("Import Request", -1)])]),
				default: withCtx(() => [createVNode(CommandActionInput_default, {
					modelValue: exampleKey.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => exampleKey.value = $event),
					placeholder: "Curl example key (e.g., example-1)",
					onDelete: handleBack
				}, null, 8, ["modelValue"]), createElementVNode("div", _hoisted_1, [createElementVNode("div", _hoisted_2, [createVNode(HttpMethod_default, {
					class: "border-r-1 px-1",
					method: unref(method)
				}, null, 8, ["method"]), createElementVNode("span", _hoisted_3, toDisplayString(unref(operation).servers?.[0]?.url || "") + toDisplayString(unref(path)), 1)])])]),
				_: 1
			}, 8, ["disabled"]);
		};
	}
});
//#endregion
export { CommandPaletteImportCurl_vue_vue_type_script_setup_true_lang_default as default };

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