import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import { importCollection } from "./utils/import-collection.js";
import { Fragment, createBlock, createCommentVNode, createElementBlock, createTextVNode, defineComponent, openBlock, unref, withCtx } from "vue";
import { ScalarButton } from "@scalar/components";
import { useToasts } from "@scalar/use-toasts";
import { useRouter } from "vue-router";
//#region src/components/ImportCollection/ImportNowButton.vue?vue&type=script&setup=true&lang.ts
var ImportNowButton_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "ImportNowButton",
	props: {
		source: {},
		variant: {},
		watchMode: {
			type: Boolean,
			default: true
		}
	},
	emits: ["importFinished"],
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		const router = useRouter();
		const store = useWorkspace();
		const { activeWorkspace } = useActiveEntities();
		const { toast } = useToasts();
		function handleImportCollection() {
			importCollection({
				store,
				workspace: activeWorkspace.value,
				source: __props.source,
				watchMode: __props.watchMode,
				onSuccess(collection) {
					if (collection) {
						redirectToFirstRequestInCollection(collection);
						toast("Import successful", "info");
						emit("importFinished");
					}
				},
				onError(error) {
					console.error("[importCollection]", error);
					toast(`Import failed: ${error?.message || "Unknown error"}`, "error");
				}
			});
		}
		function redirectToFirstRequestInCollection(collection) {
			if (!collection) return;
			router.push({
				name: "request",
				params: {
					workspace: activeWorkspace.value?.uid,
					request: collection?.requests[0]
				}
			});
		}
		return (_ctx, _cache) => {
			return __props.source ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [__props.variant === "button" ? (openBlock(), createBlock(unref(ScalarButton), {
				key: 0,
				class: "mt-3 h-fit w-full rounded-lg px-6 py-2.5 font-bold",
				size: "md",
				type: "button",
				onClick: handleImportCollection
			}, {
				default: withCtx(() => [..._cache[0] || (_cache[0] = [createTextVNode(" Import Collection ", -1)])]),
				_: 1
			})) : (openBlock(), createBlock(unref(ScalarButton), {
				key: 1,
				class: "h-fit rounded-lg px-6 py-2.5 text-[21px] font-bold",
				size: "md",
				type: "button",
				variant: "ghost",
				onClick: handleImportCollection
			}, {
				default: withCtx(() => [..._cache[1] || (_cache[1] = [createTextVNode(" Try it in the browser ", -1)])]),
				_: 1
			}))], 64)) : createCommentVNode("", true);
		};
	}
});
//#endregion
export { ImportNowButton_vue_vue_type_script_setup_true_lang_default as default };

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