import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import WatchModeToggle_default from "../CommandPalette/WatchModeToggle.vue.js";
import { useUrlPrefetcher } from "./hooks/useUrlPrefetcher.js";
import { isUrl } from "./utils/is-url.js";
import ImportNowButton_default from "./ImportNowButton.vue.js";
import IntegrationLogo_default from "./IntegrationLogo.vue.js";
import PrefetchError_default from "./PrefetchError.vue.js";
import { isDocument } from "./utils/is-document.js";
import { getOpenApiVersion } from "./utils/get-openapi-version.js";
import WorkspaceSelector_default from "./WorkspaceSelector.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, normalizeClass, onMounted, onUnmounted, openBlock, ref, toDisplayString, unref, watch, withCtx } from "vue";
import { ScalarIcon, ScalarModal, useModal } from "@scalar/components";
import { getThemeStyles, themeIds } from "@scalar/themes";
import { useColorMode } from "@scalar/use-hooks/useColorMode";
import { useRouter } from "vue-router";
import { isLocalUrl } from "@scalar/oas-utils/helpers";
import { normalize } from "@scalar/json-magic/helpers/normalize";
//#region src/components/ImportCollection/ImportCollectionModal.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = ["innerHTML"];
var _hoisted_2 = { class: "relative flex h-screen flex-col justify-center overflow-hidden px-6 md:px-0" };
var _hoisted_3 = {
	key: 0,
	class: "mb-2 flex items-center justify-center p-1"
};
var _hoisted_4 = { class: "size-10 rounded-xl" };
var _hoisted_5 = ["src"];
var _hoisted_6 = {
	key: 2,
	class: "text-md mb-2 line-clamp-1 text-center font-bold"
};
var _hoisted_7 = { class: "z-10 inline-flex w-full flex-col items-center gap-2" };
var _hoisted_8 = { class: "flex justify-center" };
var _hoisted_9 = { class: "text-c-3 inline-flex items-center px-4 py-1 text-xs font-medium" };
var _hoisted_10 = {
	key: 0,
	class: "mt-5 overflow-hidden border-t pt-4 text-sm"
};
var _hoisted_11 = { class: "flex items-center justify-center" };
var _hoisted_12 = { class: "flex flex-col items-center justify-center pb-8" };
var _hoisted_13 = { class: "flex flex-col items-center text-center" };
var _hoisted_14 = { class: "mb-2 flex h-10 w-10 items-center justify-center rounded-[10px] border" };
var _hoisted_15 = {
	href: "https://scalar.com/download",
	target: "_blank"
};
var ImportCollectionModal_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "ImportCollectionModal",
	props: {
		source: {},
		integration: {},
		eventType: {}
	},
	emits: ["importFinished"],
	setup(__props, { emit: __emit }) {
		const props = __props;
		const emits = __emit;
		const { activeWorkspace } = useActiveEntities();
		const { workspaceMutators, events } = useWorkspace();
		const { prefetchResult, prefetchUrl, resetPrefetchResult } = useUrlPrefetcher();
		const modalState = useModal();
		const watchMode = ref(true);
		/** Close modal when a keyboard shortcut is pressed */
		events.hotKeys.on(() => modalState.hide());
		const openApiDocument = computed(() => {
			try {
				return normalize(prefetchResult.content || props.source || "");
			} catch {
				return;
			}
		});
		/** Title from the OpenAPI document */
		const title = computed(() => openApiDocument.value?.info?.title);
		/** The OpenAPI/Swagger version */
		const version = computed(() => getOpenApiVersion(prefetchResult.content || props.source || ""));
		const { darkLightMode } = useColorMode();
		const { currentRoute } = useRouter();
		/** Grab light and dark logos from the url query params */
		const companyLogo = computed(() => {
			try {
				const query = currentRoute.value.query;
				const logo = darkLightMode.value === "dark" ? query.dark_logo : query.light_logo;
				if (logo) return decodeURIComponent(logo);
			} catch {}
			return null;
		});
		/** Open/close modal on events  */
		watch(() => props.source, async (value) => {
			resetPrefetchResult();
			if (isUrl(value)) {
				if (props.eventType && ["paste", "drop"].includes(props.eventType)) {
					const { error } = await prefetchUrl(value, activeWorkspace.value?.proxyUrl);
					if (error) modalState.hide();
					else modalState.show();
					return;
				}
				prefetchUrl(value, activeWorkspace.value?.proxyUrl);
				modalState.show();
				return;
			}
			if (!value) modalState.hide();
			else if (isDocument(value) && getOpenApiVersion(value)) modalState.show();
			else modalState.hide();
		});
		const hasUrl = computed(() => !!props.source && isUrl(props.source));
		const hasContent = computed(() => !!props.source && isDocument(props.source));
		/** Show the integration icon only for local URLs */
		const shouldShowIntegrationIcon = computed(() => {
			return prefetchResult.url && isLocalUrl(prefetchResult.url);
		});
		const toggleBodyClass = (add) => {
			document.body.classList.remove("has-no-import-url");
			if (add && (hasUrl.value || hasContent.value) && modalState.open) document.body.classList.add("has-import-url");
			else document.body.classList.remove("has-import-url");
		};
		/** Toggles the 'has-import-url' class on the body element */
		const closeModal = () => {
			document.body.classList.remove("has-import-url");
			document.body.classList.add("has-no-import-url");
		};
		watch(() => modalState.open, (isOpen) => {
			if (isOpen) toggleBodyClass(true);
			else closeModal();
		});
		watch(() => props.source, () => {
			toggleBodyClass(true);
		});
		onMounted(() => {
			toggleBodyClass(true);
		});
		onUnmounted(() => {
			document.body.classList.remove("has-import-url");
			document.body.classList.remove("has-no-import-url");
		});
		const themeStyleTag = computed(() => activeWorkspace.value && shouldShowIntegrationIcon.value && props.integration && `<style>${getThemeStyles(props.integration)}</style>`);
		function handleImportFinished() {
			const isIntegrationThemeId = (value) => themeIds.includes(value);
			const integrationThemeId = props.integration && isIntegrationThemeId(props.integration) ? props.integration : "default";
			if (shouldShowIntegrationIcon.value) workspaceMutators.edit(activeWorkspace.value?.uid, "themeId", integrationThemeId);
			emits("importFinished");
		}
		return (_ctx, _cache) => {
			return openBlock(), createBlock(unref(ScalarModal), {
				size: "full",
				state: unref(modalState)
			}, {
				default: withCtx(() => [themeStyleTag.value ? (openBlock(), createElementBlock("div", {
					key: 0,
					innerHTML: themeStyleTag.value
				}, null, 8, _hoisted_1)) : createCommentVNode("", true), createElementVNode("div", _hoisted_2, [
					_cache[6] || (_cache[6] = createElementVNode("div", { class: "section-flare" }, [
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" }),
						createElementVNode("div", { class: "section-flare-item" })
					], -1)),
					createElementVNode("div", { class: normalizeClass(["m-auto flex w-full max-w-[380px] flex-col items-center rounded-xl border px-8 py-8 transition-opacity", { "opacity-0": unref(prefetchResult).state === "loading" }]) }, [unref(prefetchResult).error && unref(prefetchResult).state === "idle" && !version.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [_cache[1] || (_cache[1] = createElementVNode("div", { class: "text-md mb-2 line-clamp-1 text-center font-bold" }, " No OpenAPI document found ", -1)), createVNode(PrefetchError_default, { url: unref(prefetchResult)?.input || props.source }, null, 8, ["url"])], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
						shouldShowIntegrationIcon.value ? (openBlock(), createElementBlock("div", _hoisted_3, [createElementVNode("div", _hoisted_4, [createVNode(IntegrationLogo_default, { integration: __props.integration }, null, 8, ["integration"])])])) : companyLogo.value ? (openBlock(), createElementBlock("img", {
							key: 1,
							alt: "Logo",
							class: "mb-2 w-full object-contain",
							src: companyLogo.value
						}, null, 8, _hoisted_5)) : createCommentVNode("", true),
						!companyLogo.value ? (openBlock(), createElementBlock("div", _hoisted_6, toDisplayString(title.value || "Untitled Collection"), 1)) : createCommentVNode("", true),
						_cache[4] || (_cache[4] = createElementVNode("div", { class: "text-c-1 text-center text-sm font-medium text-balance" }, " Import the OpenAPI document to instantly send API requests. No signup required. ", -1)),
						version.value ? (openBlock(), createElementBlock(Fragment, { key: 3 }, [
							createElementVNode("div", _hoisted_7, [createVNode(ImportNowButton_default, {
								source: unref(prefetchResult)?.url ?? unref(prefetchResult)?.content ?? __props.source,
								variant: "button",
								watchMode: watchMode.value,
								onImportFinished: handleImportFinished
							}, null, 8, ["source", "watchMode"])]),
							createElementVNode("div", _hoisted_8, [createElementVNode("div", _hoisted_9, [_cache[2] || (_cache[2] = createTextVNode(" Import to: ", -1)), createVNode(WorkspaceSelector_default)])]),
							unref(prefetchResult)?.url ? (openBlock(), createElementBlock("div", _hoisted_10, [createElementVNode("div", _hoisted_11, [createVNode(WatchModeToggle_default, {
								modelValue: watchMode.value,
								"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => watchMode.value = $event),
								disableToolTip: true
							}, null, 8, ["modelValue"])]), _cache[3] || (_cache[3] = createElementVNode("div", { class: "text-c-3 pt-0 text-center text-xs font-medium text-balance" }, " Automatically update your API client when the OpenAPI document content changes. ", -1))])) : createCommentVNode("", true)
						], 64)) : createCommentVNode("", true)
					], 64))], 2),
					createElementVNode("div", _hoisted_12, [createElementVNode("div", _hoisted_13, [createElementVNode("div", _hoisted_14, [createElementVNode("a", _hoisted_15, [createVNode(unref(ScalarIcon), {
						icon: "Logo",
						size: "xl"
					})])]), _cache[5] || (_cache[5] = createElementVNode("span", { class: "text-c-2 text-sm leading-snug font-medium" }, [
						createElementVNode("a", {
							class: "hover:text-c-1 mb-1 inline-block underline-offset-2",
							href: "https://scalar.com/download",
							target: "_blank"
						}, " Download Desktop App "),
						createElementVNode("br"),
						createTextVNode(" free · open-source · offline first ")
					], -1))])])
				])]),
				_: 1
			}, 8, ["state"]);
		};
	}
});
//#endregion
export { ImportCollectionModal_vue_vue_type_script_setup_true_lang_default as default };

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