import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import { workspaceStoreIsEmpty } from "./utils/workspace-store-is-empty.js";
import DropEventListener_default from "./DropEventListener.vue.js";
import { importCollection } from "./utils/import-collection.js";
import ImportCollectionModal_default from "./ImportCollectionModal.vue.js";
import PasteEventListener_default from "./PasteEventListener.vue.js";
import UrlQueryParameterChecker_default from "./UrlQueryParameterChecker.vue.js";
import { Fragment, createElementBlock, createVNode, defineComponent, nextTick, openBlock, ref, renderSlot } from "vue";
import { useToasts } from "@scalar/use-toasts";
import { useRouter } from "vue-router";
//#region src/components/ImportCollection/ImportCollectionListener.vue?vue&type=script&setup=true&lang.ts
var ImportCollectionListener_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "ImportCollectionListener",
	setup(__props) {
		const store = useWorkspace();
		/** Source to import from */
		const source = ref(null);
		const integration = ref(null);
		const eventType = ref(null);
		/** Reset the data when the modal was closed */
		async function resetData() {
			source.value = null;
			integration.value = null;
			eventType.value = null;
			await nextTick();
		}
		/** Receive data from the paste event listener */
		async function handleInput(newSource, newIntegration = null, newEventType) {
			await resetData();
			if (workspaceStoreIsEmpty(store)) {
				console.info("Workspace store is empty, directly importing:", newSource);
				if (await handleImportCollection(newSource)) return;
				console.warn("Failed to import the collection from:", newSource);
			}
			source.value = newSource;
			integration.value = newIntegration;
			eventType.value = newEventType;
		}
		const router = useRouter();
		const { activeWorkspace } = useActiveEntities();
		const { toast } = useToasts();
		function handleImportCollection(source) {
			if (!source) return Promise.resolve(false);
			return new Promise((resolve) => {
				importCollection({
					store,
					workspace: activeWorkspace.value,
					source,
					watchMode: true,
					onSuccess(collection) {
						if (collection) {
							redirectToFirstRequestInCollection(collection);
							toast("Import successful", "info");
							resolve(true);
							return;
						}
						toast("Import failed: No collection was created", "error");
						resolve(false);
					},
					onError(error) {
						console.error("[importCollection]", error);
						toast(`Import failed: ${error?.message || "Unknown error"}`, "error");
						resolve(false);
					}
				});
			});
		}
		function redirectToFirstRequestInCollection(collection) {
			if (!collection) return;
			router.push({
				name: "request",
				params: {
					workspace: activeWorkspace.value?.uid,
					request: collection?.requests[0]
				}
			});
		}
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock(Fragment, null, [
				createVNode(ImportCollectionModal_default, {
					eventType: eventType.value,
					integration: integration.value,
					source: source.value,
					onImportFinished: resetData
				}, null, 8, [
					"eventType",
					"integration",
					"source"
				]),
				createVNode(PasteEventListener_default, { onInput: handleInput }),
				createVNode(DropEventListener_default, { onInput: handleInput }),
				createVNode(UrlQueryParameterChecker_default, { onInput: handleInput }),
				renderSlot(_ctx.$slots, "default")
			], 64);
		};
	}
});
//#endregion
export { ImportCollectionListener_vue_vue_type_script_setup_true_lang_default as default };

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