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 CommandActionInput_default from "./CommandActionInput.vue.js";
import { computed, createBlock, createElementVNode, createTextVNode, createVNode, defineComponent, normalizeClass, openBlock, ref, toDisplayString, unref, useTemplateRef, withCtx } from "vue";
import { ScalarButton, ScalarIcon, ScalarListbox } from "@scalar/components";
import { useToasts } from "@scalar/use-toasts";
import { useRouter } from "vue-router";
import { emitCustomEvent } from "@scalar/workspace-store/events";
//#region src/components/CommandPalette/CommandPaletteServer.vue?vue&type=script&setup=true&lang.ts
var CommandPaletteServer_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "CommandPaletteServer",
	props: { metaData: {} },
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const props = __props;
		const emits = __emit;
		const router = useRouter();
		const { toast } = useToasts();
		const { activeCollection, activeWorkspaceCollections } = useActiveEntities();
		const { collectionMutators, serverMutators, events, collections: _collections } = useWorkspace();
		const url = ref("");
		const collections = computed(() => activeWorkspaceCollections.value.flatMap((collection) => collection.info?.title === "Drafts" ? [] : {
			id: collection.uid,
			label: collection.info?.title ?? "Unititled Collection"
		}));
		/** Currently selected collection with a reasonable default */
		const selectedCollection = ref(props.metaData ? collections.value.find((collection) => collection.id === props.metaData?.itemUid || collection.id === props.metaData?.parentUid) : collections.value.find((collection) => collection.id === activeCollection.value?.uid));
		const handleSubmit = () => {
			if (!url.value.trim()) {
				toast("Please enter a valid url before creating a server.", "error");
				return;
			}
			const collection = _collections[selectedCollection.value?.id ?? ""];
			if (!collection) {
				toast("Please select a collection before creating a server.", "error");
				return;
			}
			const server = serverMutators.add({ url: url.value }, collection.uid);
			collectionMutators.edit(collection.uid, "selectedServerUid", server.uid);
			emitCustomEvent(wrapper.value?.$el, "scalar-add-server", {
				server: { url: url.value },
				options: { disableOldStoreUpdate: true }
			});
			router.push({
				name: "collection.servers.edit",
				params: {
					[PathId.Collection]: collection.uid,
					[PathId.Servers]: server.uid
				}
			});
			emits("close");
		};
		const redirectToCreateCollection = () => {
			events.commandPalette.emit({ commandName: "Create Collection" });
		};
		const wrapper = useTemplateRef("wrapper-ref");
		return (_ctx, _cache) => {
			return openBlock(), createBlock(CommandActionForm_default, {
				ref: "wrapper-ref",
				disabled: !url.value.trim() || !selectedCollection.value,
				onSubmit: handleSubmit
			}, {
				options: withCtx(() => [createVNode(unref(ScalarListbox), {
					modelValue: selectedCollection.value,
					"onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => selectedCollection.value = $event),
					options: collections.value
				}, {
					default: withCtx(() => [collections.value.length > 0 ? (openBlock(), createBlock(unref(ScalarButton), {
						key: 0,
						class: "hover:bg-b-2 max-h-8 w-fit justify-between gap-1 p-2 text-xs",
						variant: "outlined"
					}, {
						default: withCtx(() => [createElementVNode("span", { class: normalizeClass(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
					})) : (openBlock(), createBlock(unref(ScalarButton), {
						key: 1,
						class: "hover:bg-b-2 max-h-8 w-fit justify-between gap-1 p-2 text-xs",
						variant: "outlined",
						onClick: redirectToCreateCollection
					}, {
						default: withCtx(() => [..._cache[3] || (_cache[3] = [createElementVNode("span", { class: "text-c-1" }, "Create Collection", -1)])]),
						_: 1
					}))]),
					_: 1
				}, 8, ["modelValue", "options"])]),
				submit: withCtx(() => [..._cache[4] || (_cache[4] = [createTextVNode(" Create Server ", -1)])]),
				default: withCtx(() => [createVNode(CommandActionInput_default, {
					modelValue: url.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => url.value = $event),
					label: "Server URL",
					placeholder: "https://void.scalar.com",
					onOnDelete: _cache[1] || (_cache[1] = ($event) => emits("back", $event))
				}, null, 8, ["modelValue"])]),
				_: 1
			}, 8, ["disabled"]);
		};
	}
});
//#endregion
export { CommandPaletteServer_vue_vue_type_script_setup_true_lang_default as default };

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