import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { useLayout } from "../../hooks/useLayout.js";
import { useWorkspace } from "../../store/store.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, onMounted, openBlock, renderList, toDisplayString, unref, watch, withCtx, withModifiers } from "vue";
import { ScalarButton, ScalarDropdown, ScalarDropdownDivider, ScalarDropdownItem, ScalarIcon, ScalarListboxCheckbox } from "@scalar/components";
import { useRouter } from "vue-router";
//#region src/components/EnvironmentSelector/EnvironmentSelector.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "m-0 flex items-center gap-1.5 font-medium whitespace-nowrap" };
var _hoisted_2 = { class: "flex h-4 w-4 items-center justify-center" };
var EnvironmentSelector_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "EnvironmentSelector",
	setup(__props) {
		const { activeCollection, activeWorkspace, activeEnvironment } = useActiveEntities();
		const { collectionMutators } = useWorkspace();
		const { layout } = useLayout();
		const router = useRouter();
		const updateSelected = (uid) => {
			if (activeCollection.value && activeWorkspace.value) {
				collectionMutators.edit(activeCollection.value.uid, "x-scalar-active-environment", uid);
				activeWorkspace.value.activeEnvironmentId = uid;
			}
		};
		const redirectToEnvironments = () => router.push({
			name: "environment.default",
			params: { [PathId.Workspace]: activeWorkspace.value?.uid }
		});
		const selectedEnvironment = computed(() => {
			const { value: environment } = activeEnvironment;
			const { value: collection } = activeCollection;
			return environment?.name || collection?.["x-scalar-active-environment"] || "No Environment";
		});
		const availableEnvironments = computed(() => {
			const { value: collection } = activeCollection;
			const environments = collection?.["x-scalar-environments"];
			return environments ? Object.entries(environments).map(([key, env]) => ({
				...env,
				uid: key,
				name: key
			})) : [];
		});
		const selectLatestEnvironment = () => {
			const environments = availableEnvironments.value;
			if (environments.length > 0) {
				const latestEnvironment = environments[environments.length - 1];
				if (latestEnvironment?.uid) updateSelected(latestEnvironment.uid);
			}
		};
		watch(availableEnvironments, (newEnvs, oldEnvs) => {
			if (newEnvs.length > oldEnvs.length) selectLatestEnvironment();
		});
		const setInitialEnvironment = (collection) => {
			const activeEnv = collection["x-scalar-active-environment"];
			if (activeEnv && activeCollection.value && activeWorkspace.value) {
				activeCollection.value["x-scalar-active-environment"] = activeEnv;
				activeWorkspace.value.activeEnvironmentId = activeEnv;
			} else if (activeWorkspace.value) activeWorkspace.value.activeEnvironmentId = "";
		};
		watch(activeCollection, (newCollection) => newCollection && setInitialEnvironment(newCollection));
		onMounted(() => {
			activeCollection.value && setInitialEnvironment(activeCollection.value);
		});
		return (_ctx, _cache) => {
			return openBlock(), createBlock(unref(ScalarDropdown), { teleport: "" }, {
				items: withCtx(() => [
					(openBlock(true), createElementBlock(Fragment, null, renderList(availableEnvironments.value, (environment) => {
						return openBlock(), createBlock(unref(ScalarDropdownItem), {
							key: environment.uid,
							class: "group/item flex items-center gap-1.5 overflow-hidden text-ellipsis whitespace-nowrap",
							onClick: withModifiers(($event) => updateSelected(environment.uid), ["stop"])
						}, {
							default: withCtx(() => [createVNode(unref(ScalarListboxCheckbox), { selected: unref(activeCollection)?.["x-scalar-active-environment"] === environment.uid }, null, 8, ["selected"]), createTextVNode(" " + toDisplayString(environment.name), 1)]),
							_: 2
						}, 1032, ["onClick"]);
					}), 128)),
					createVNode(unref(ScalarDropdownItem), {
						class: "group/item flex items-center gap-1.5 overflow-hidden text-ellipsis whitespace-nowrap",
						onClick: _cache[0] || (_cache[0] = withModifiers(($event) => updateSelected(""), ["stop"]))
					}, {
						default: withCtx(() => [createVNode(unref(ScalarListboxCheckbox), { selected: unref(activeEnvironment)?.uid === "" && unref(activeCollection)?.["x-scalar-active-environment"] === "" || unref(activeEnvironment)?.name === "No Environment" }, null, 8, ["selected"]), _cache[1] || (_cache[1] = createTextVNode(" No Environment ", -1))]),
						_: 1
					}),
					createVNode(unref(ScalarDropdownDivider)),
					unref(layout) !== "modal" ? (openBlock(), createBlock(unref(ScalarDropdownItem), {
						key: 0,
						class: "flex items-center gap-1.5",
						onClick: redirectToEnvironments
					}, {
						default: withCtx(() => [createElementVNode("div", _hoisted_2, [createVNode(unref(ScalarIcon), {
							icon: "Brackets",
							size: "sm"
						})]), _cache[2] || (_cache[2] = createElementVNode("span", { class: "leading-none" }, "Manage Environments", -1))]),
						_: 1
					})) : createCommentVNode("", true)
				]),
				default: withCtx(() => [createVNode(unref(ScalarButton), {
					class: "text-c-1 hover:bg-b-2 line-clamp-1 h-auto w-fit justify-start px-1.5 py-1.5 font-normal",
					variant: "ghost"
				}, {
					default: withCtx(() => [createElementVNode("h2", _hoisted_1, toDisplayString(selectedEnvironment.value), 1)]),
					_: 1
				})]),
				_: 1
			});
		};
	}
});
//#endregion
export { EnvironmentSelector_vue_vue_type_script_setup_true_lang_default as default };

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