import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import CommandActionForm_default from "./CommandActionForm.vue.js";
import HttpMethod_default from "../HttpMethod/HttpMethod.vue.js";
import { Fragment, computed, createElementBlock, createElementVNode, createVNode, defineComponent, normalizeClass, openBlock, ref, toDisplayString, unref, useTemplateRef, withCtx } from "vue";
import { ScalarButton, ScalarIcon, ScalarListbox } from "@scalar/components";
import { useRouter } from "vue-router";
import { REGEX } from "@scalar/oas-utils/helpers";
import { emitCustomEvent } from "@scalar/workspace-store/events";
//#region src/components/CommandPalette/CommandPaletteImportCurl.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex h-9 flex-row items-center gap-2 rounded border p-[3px] text-sm" };
var _hoisted_2 = { class: "flex h-full" };
var _hoisted_3 = { class: "scroll-timeline-x whitespace-nowrap" };
var _hoisted_4 = { class: "flex" };
var CommandPaletteImportCurl_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "CommandPaletteImportCurl",
	props: { metaData: {} },
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const emits = __emit;
		const workspaceContext = useWorkspace();
		const { activeWorkspaceCollections, activeCollection, activeWorkspace } = useActiveEntities();
		const { requestMutators, serverMutators, servers } = workspaceContext;
		const selectedServerUid = ref("");
		const router = useRouter();
		const collections = computed(() => activeWorkspaceCollections.value.map((collection) => ({
			id: collection.uid,
			label: collection.info?.title ?? "Unititled Collection"
		})));
		const selectedCollection = ref(__props.metaData.collectionUid ? collections.value.find((collection) => collection.id === __props.metaData.collectionUid) : collections.value.find((collection) => collection.id === activeCollection.value?.uid));
		function createRequestFromCurl({ collectionUid }) {
			if (!__props.metaData.parsedCurl) return;
			const collection = activeWorkspaceCollections.value.find((c) => c.uid === collectionUid);
			if (!collection) return;
			const isDrafts = collection?.info?.title === "Drafts";
			if (!isDrafts && __props.metaData.parsedCurl.servers) {
				const existingServer = Object.values(servers).find((s) => s.url === __props.metaData.parsedCurl?.servers?.[0]);
				if (existingServer) selectedServerUid.value = existingServer.uid;
				else {
					selectedServerUid.value = serverMutators.add({ url: __props.metaData.parsedCurl.servers[0] ?? "/" }, collection.uid).uid;
					emitCustomEvent(wrapper.value?.$el, "scalar-add-server", {
						server: { url: __props.metaData.parsedCurl.servers[0] ?? "/" },
						options: { disableOldStoreUpdate: true }
					});
				}
			}
			const newRequest = requestMutators.add({
				summary: isDrafts ? __props.metaData.parsedCurl?.url?.replace(REGEX.PROTOCOL, "") : __props.metaData.parsedCurl?.path,
				path: isDrafts ? __props.metaData.parsedCurl?.url : __props.metaData.parsedCurl?.path,
				method: __props.metaData.parsedCurl?.method,
				parameters: __props.metaData.parsedCurl?.parameters,
				selectedServerUid: isDrafts ? void 0 : selectedServerUid.value,
				requestBody: __props.metaData.parsedCurl?.requestBody
			}, collection.uid);
			if (newRequest && activeWorkspace.value?.uid) router.push({
				name: "request",
				params: {
					[PathId.Workspace]: activeWorkspace.value.uid,
					[PathId.Collection]: collection.uid,
					[PathId.Request]: newRequest.uid
				}
			});
			emits("close");
		}
		const handleImportClick = () => {
			createRequestFromCurl({ collectionUid: selectedCollection.value?.id ?? "" });
		};
		const wrapper = useTemplateRef("wrapper-ref");
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock(Fragment, null, [_cache[1] || (_cache[1] = createElementVNode("div", { class: "text-c-2 flex-center py-1.5 text-sm" }, "Import cURL", -1)), createVNode(CommandActionForm_default, {
				ref: "wrapper-ref",
				class: "mt-1.5 min-h-fit",
				onSubmit: handleImportClick
			}, {
				options: withCtx(() => [createElementVNode("div", _hoisted_4, [createVNode(unref(ScalarListbox), {
					modelValue: selectedCollection.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => selectedCollection.value = $event),
					options: collections.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", selectedCollection.value ? "text-c-1" : "text-c-3"]) }, toDisplayString(selectedCollection.value ? selectedCollection.value.label : "Select Collection"), 3), createVNode(unref(ScalarIcon), {
							class: "text-c-3",
							icon: "ChevronDown",
							size: "md"
						})]),
						_: 1
					})]),
					_: 1
				}, 8, ["modelValue", "options"])])]),
				submit: withCtx(() => [createElementVNode("span", { onClick: handleImportClick }, "Import Request")]),
				default: withCtx(() => [createElementVNode("div", _hoisted_1, [createElementVNode("div", _hoisted_2, [createVNode(HttpMethod_default, {
					isEditable: false,
					isSquare: "",
					method: __props.metaData.parsedCurl?.method || "get"
				}, null, 8, ["method"])]), createElementVNode("span", _hoisted_3, toDisplayString(__props.metaData.parsedCurl?.servers?.[0] || "") + toDisplayString(__props.metaData.parsedCurl?.path || ""), 1)])]),
				_: 1
			}, 512)], 64);
		};
	}
});
//#endregion
export { CommandPaletteImportCurl_vue_vue_type_script_setup_true_lang_default as default };

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