import { useWorkspace } from "../../store/store.js";
import ServerVariablesForm_default from "./ServerVariablesForm.vue.js";
import { computed, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, mergeProps, normalizeClass, openBlock, toDisplayString, unref, useId, useTemplateRef, withModifiers } from "vue";
import { ScalarListboxCheckbox, ScalarMarkdown } from "@scalar/components";
import { emitCustomEvent } from "@scalar/workspace-store/events";
//#region src/components/Server/ServerDropdownItem.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = ["aria-expanded"];
var _hoisted_2 = { class: "overflow-hidden text-ellipsis whitespace-nowrap" };
var _hoisted_3 = ["id"];
var _hoisted_4 = { key: 0 };
var _hoisted_5 = { class: "description text-c-3 px-3 py-1.5" };
var ServerDropdownItem_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "ServerDropdownItem",
	props: {
		collection: {},
		operation: {},
		server: {},
		serverOption: {},
		type: {}
	},
	emits: ["update:variable"],
	setup(__props, { emit: __emit }) {
		const props = __props;
		const emit = __emit;
		const formId = useId();
		const { collectionMutators, requestMutators, servers } = useWorkspace();
		/** Update the currently selected server on the collection or request */
		const updateSelectedServer = (serverUid, event) => {
			if (hasVariables(serverUid)) event?.stopPropagation();
			if (isSelectedServer.value) {
				if (props.operation?.servers?.length) requestMutators.edit(props.operation.uid, "selectedServerUid", null);
				if (props.type === "collection") {
					collectionMutators.edit(props.collection.uid, "selectedServerUid", void 0);
					emitCustomEvent(wrapper.value, "scalar-update-selected-server", {
						value: void 0,
						options: { disableOldStoreUpdate: true }
					});
				} else if (props.type === "request" && props.operation) requestMutators.edit(props.operation.uid, "selectedServerUid", null);
				return;
			}
			if (props.type === "collection" && props.collection) {
				if (props.operation?.servers?.length) requestMutators.edit(props.operation.uid, "selectedServerUid", null);
				collectionMutators.edit(props.collection.uid, "selectedServerUid", serverUid);
				emitCustomEvent(wrapper.value, "scalar-update-selected-server", {
					value: servers[serverUid]?.url,
					options: { disableOldStoreUpdate: true }
				});
			} else if (props.type === "request" && props.operation) requestMutators.edit(props.operation.uid, "selectedServerUid", serverUid);
		};
		/** Set server checkbox in the dropdown */
		const isSelectedServer = computed(() => {
			if (props.type === "collection") return props.collection.selectedServerUid === props.serverOption.id && !props.operation?.selectedServerUid;
			if (props.type === "request" && props.operation) return props.operation.selectedServerUid === props.serverOption.id;
			return false;
		});
		const hasVariables = (serverUid) => {
			if (!serverUid) return false;
			const server = servers[serverUid];
			return Object.keys(server?.variables ?? {}).length > 0;
		};
		const isExpanded = computed(() => isSelectedServer.value && hasVariables(props.server?.uid ?? ""));
		const updateServerVariable = (key, value) => {
			emit("update:variable", key, value);
		};
		const wrapper = useTemplateRef("wrapper-ref");
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock("div", {
				ref: "wrapper-ref",
				class: normalizeClass(["group/item flex min-h-fit flex-col rounded border", { "border-transparent": !isSelectedServer.value }])
			}, [createElementVNode("button", mergeProps(isExpanded.value ? { "aria-controls": unref(formId) } : {}, {
				"aria-expanded": isExpanded.value,
				class: ["flex min-h-8 cursor-pointer items-center gap-1.5 rounded px-1.5", isSelectedServer.value ? "text-c-1 bg-b-2" : "hover:bg-b-2"],
				type: "button",
				onClick: _cache[0] || (_cache[0] = (e) => updateSelectedServer(__props.serverOption.id, e))
			}), [createVNode(unref(ScalarListboxCheckbox), { selected: isSelectedServer.value }, null, 8, ["selected"]), createElementVNode("span", _hoisted_2, toDisplayString(__props.serverOption.label), 1)], 16, _hoisted_1), isExpanded.value ? (openBlock(), createElementBlock("div", {
				key: 0,
				id: unref(formId),
				class: "bg-b-2 divide divide-y rounded-b border-t *:pl-4",
				onClick: _cache[1] || (_cache[1] = withModifiers(() => {}, ["stop"]))
			}, [createVNode(ServerVariablesForm_default, {
				variables: __props.server?.variables,
				"onUpdate:variable": updateServerVariable
			}, null, 8, ["variables"]), __props.server?.description ? (openBlock(), createElementBlock("div", _hoisted_4, [createElementVNode("div", _hoisted_5, [createVNode(unref(ScalarMarkdown), { value: __props.server.description }, null, 8, ["value"])])])) : createCommentVNode("", true)], 8, _hoisted_3)) : createCommentVNode("", true)], 2);
		};
	}
});
//#endregion
export { ServerDropdownItem_vue_vue_type_script_setup_true_lang_default as default };

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