import { r as useWorkspace } from "./store-DpSUVhjp.js";
import { a as useLayout } from "./useSidebar-DLTwHDI-.js";
import { t as _plugin_vue_export_helper_default } from "./_plugin-vue_export-helper-BmmBcIzD.js";
import { t as ServerVariablesForm_default } from "./ServerVariablesForm-BRZ9XC8o.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, mergeProps, normalizeClass, openBlock, renderList, toDisplayString, unref, useId, useTemplateRef, watch, withCtx, withModifiers } from "vue";
import { ScalarButton, ScalarDropdownDivider, ScalarFloatingBackdrop, ScalarIcon, ScalarListbox, ScalarListboxCheckbox, ScalarMarkdown, ScalarPopover } from "@scalar/components";
import { emitCustomEvent } from "@scalar/workspace-store/events";
import { ScalarIconCaretDown } from "@scalar/icons";
//#region src/components/Server/ServerDropdownItem.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1$2 = ["aria-expanded"];
var _hoisted_2$2 = { class: "overflow-hidden text-ellipsis whitespace-nowrap" };
var _hoisted_3$1 = ["id"];
var _hoisted_4 = { key: 0 };
var _hoisted_5 = { class: "description text-c-3 px-3 py-1.5" };
//#endregion
//#region src/components/Server/ServerDropdownItem.vue
var ServerDropdownItem_default = /* @__PURE__ */ _plugin_vue_export_helper_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$2, toDisplayString(__props.serverOption.label), 1)], 16, _hoisted_1$2), 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$1)) : createCommentVNode("", true)], 2);
		};
	}
}), [["__scopeId", "data-v-92012388"]]);
//#endregion
//#region src/components/Server/ServerDropdown.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1$1 = ["onClick"];
var _hoisted_2$1 = { class: "flex h-4 w-4 items-center justify-center" };
//#endregion
//#region src/components/Server/ServerDropdown.vue
var ServerDropdown_default = /* @__PURE__ */ defineComponent({
	__name: "ServerDropdown",
	props: {
		collection: {},
		operation: {},
		server: {},
		target: {}
	},
	setup(__props) {
		const { layout } = useLayout();
		const { servers, collectionMutators, events, serverMutators } = useWorkspace();
		const requestServerOptions = computed(() => __props.operation?.servers?.map((serverUid) => ({
			id: serverUid,
			label: servers[serverUid]?.url ?? "Unknown server"
		})));
		const collectionServerOptions = computed(() => __props.collection?.servers?.filter((serverUid) => !__props.operation?.servers?.includes(serverUid)).map((serverUid) => ({
			id: serverUid,
			label: servers[serverUid]?.url ?? "Unknown server"
		})));
		/** If we have both request and collection servers we show the labels */
		const showDropdownLabels = computed(() => requestServerOptions.value?.length && collectionServerOptions.value?.length);
		watch([() => __props.collection, () => __props.operation], ([newCollection, newOperation]) => {
			if (!newCollection || newCollection.selectedServerUid || newOperation?.selectedServerUid) return;
			const firstServer = __props.collection.servers?.[0];
			if (firstServer) collectionMutators.edit(__props.collection.uid, "selectedServerUid", firstServer);
		});
		/** Add server */
		const handleAddServer = () => events.commandPalette.emit({ commandName: "Add Server" });
		const serverUrlWithoutTrailingSlash = computed(() => {
			if (__props.server?.url?.endsWith("/")) return __props.server.url.slice(0, -1);
			return __props.server?.url || "";
		});
		const updateServerVariable = (key, value) => {
			if (!__props.server) return;
			const variables = __props.server.variables || {};
			variables[key] = {
				...variables[key],
				default: value
			};
			serverMutators.edit(__props.server.uid, "variables", variables);
			emitCustomEvent(wrapper.value?.$el, "scalar-update-selected-server-variables", {
				key,
				value
			});
		};
		const wrapper = useTemplateRef("wrapper-ref");
		return (_ctx, _cache) => {
			return openBlock(), createBlock(unref(ScalarPopover), {
				ref: "wrapper-ref",
				class: "max-h-[inherit] p-0 text-base",
				focus: "",
				offset: 0,
				placement: "bottom-start",
				resize: "",
				target: __props.target,
				teleport: `#${__props.target}`
			}, {
				popover: withCtx(({ close }) => [createElementVNode("div", {
					class: "custom-scroll flex max-h-[inherit] flex-col gap-1 border-t p-1",
					onClick: close
				}, [
					(openBlock(true), createElementBlock(Fragment, null, renderList(requestServerOptions.value, (serverOption) => {
						return openBlock(), createBlock(ServerDropdownItem_default, {
							key: serverOption.id,
							collection: __props.collection,
							operation: __props.operation,
							server: __props.server,
							serverOption,
							type: "request",
							"onUpdate:variable": updateServerVariable
						}, null, 8, [
							"collection",
							"operation",
							"server",
							"serverOption"
						]);
					}), 128)),
					showDropdownLabels.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createVNode(unref(ScalarDropdownDivider)), _cache[2] || (_cache[2] = createElementVNode("div", { class: "text-xxs text-c-2 px-2.5 py-1" }, "Collection", -1))], 64)) : createCommentVNode("", true),
					(openBlock(true), createElementBlock(Fragment, null, renderList(collectionServerOptions.value, (serverOption) => {
						return openBlock(), createBlock(ServerDropdownItem_default, {
							key: serverOption.id,
							collection: __props.collection,
							operation: __props.operation,
							server: __props.server,
							serverOption,
							type: "collection",
							"onUpdate:variable": updateServerVariable
						}, null, 8, [
							"collection",
							"operation",
							"server",
							"serverOption"
						]);
					}), 128)),
					unref(layout) !== "modal" ? (openBlock(), createElementBlock("button", {
						key: 1,
						class: "text-xxs hover:bg-b-2 flex cursor-pointer items-center gap-1.5 rounded p-1.75",
						type: "button",
						onClick: handleAddServer
					}, [createElementVNode("div", _hoisted_2$1, [createVNode(unref(ScalarIcon), {
						icon: "Add",
						size: "sm"
					})]), _cache[3] || (_cache[3] = createElementVNode("span", null, "Add Server", -1))])) : createCommentVNode("", true)
				], 8, _hoisted_1$1)]),
				backdrop: withCtx(() => [createVNode(unref(ScalarFloatingBackdrop), { class: "-top-(--scalar-address-bar-height) rounded-lg" })]),
				default: withCtx(() => [createVNode(unref(ScalarButton), {
					class: "z-context-plus hover:bg-b-2 font-code text-c-2 ml-0.75 h-auto gap-0.75 rounded border px-1.5 text-base whitespace-nowrap",
					variant: "ghost"
				}, {
					default: withCtx(() => [__props.operation?.selectedServerUid || __props.collection.selectedServerUid ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [_cache[0] || (_cache[0] = createElementVNode("span", { class: "sr-only" }, "Server:", -1)), createTextVNode(" " + toDisplayString(serverUrlWithoutTrailingSlash.value), 1)], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [_cache[1] || (_cache[1] = createElementVNode("span", { class: "sr-only" }, "Add Server", -1)), createVNode(unref(ScalarIcon), {
						icon: "Add",
						size: "xs"
					})], 64))]),
					_: 1
				})]),
				_: 1
			}, 8, ["target", "teleport"]);
		};
	}
});
//#endregion
//#region src/components/Server/ServerSelector.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "overflow-x-auto" };
var _hoisted_2 = {
	key: 1,
	class: "text-c-1 flex h-auto w-full items-center gap-0.75 rounded-b-xl px-3 py-1.5 text-base/5 whitespace-nowrap"
};
var _hoisted_3 = { class: "overflow-x-auto" };
//#endregion
//#region src/components/Server/ServerSelector.vue
var ServerSelector_default = /* @__PURE__ */ defineComponent({
	__name: "ServerSelector",
	props: {
		collection: {},
		server: {},
		target: {}
	},
	emits: ["updateServer"],
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		const { servers, collectionMutators } = useWorkspace();
		const serverOptions = computed(() => __props.collection?.servers.map((serverUid) => ({
			id: serverUid,
			label: servers[serverUid]?.url ?? "Unknown server"
		})));
		const selectedServer = computed({
			get: () => __props.server ? serverOptions.value.find((option) => option.id === __props.server.uid) : void 0,
			set: (option) => {
				if (!option) return;
				collectionMutators.edit(__props.collection.uid, "selectedServerUid", option.id);
				const serverUrl = servers[option.id]?.url;
				if (serverUrl) emit("updateServer", serverUrl);
			}
		});
		watch(() => __props.collection, (newCollection) => {
			if (!newCollection || newCollection.selectedServerUid) return;
			const firstServer = __props.collection.servers?.[0];
			if (firstServer) {
				collectionMutators.edit(__props.collection.uid, "selectedServerUid", firstServer);
				if (servers[firstServer]?.url) emit("updateServer", servers[firstServer].url);
			}
		});
		const serverUrlWithoutTrailingSlash = computed(() => {
			if (__props.server?.url?.endsWith("/")) return __props.server.url.slice(0, -1);
			return __props.server?.url || "";
		});
		return (_ctx, _cache) => {
			return serverOptions.value.length > 1 ? (openBlock(), createBlock(unref(ScalarListbox), {
				key: 0,
				modelValue: selectedServer.value,
				"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => selectedServer.value = $event),
				class: "group",
				options: serverOptions.value,
				placement: "bottom-start",
				resize: "",
				target: __props.target,
				teleport: `#${__props.target}`
			}, {
				default: withCtx(() => [createVNode(unref(ScalarButton), {
					class: "bg-b-1 text-c-1 h-auto w-full justify-start gap-1.5 overflow-x-auto rounded-t-none rounded-b-xl px-3 py-1.5 text-base font-normal whitespace-nowrap -outline-offset-1",
					variant: "ghost"
				}, {
					default: withCtx(() => [
						_cache[1] || (_cache[1] = createElementVNode("span", { class: "sr-only" }, "Server:", -1)),
						createElementVNode("span", _hoisted_1, toDisplayString(serverUrlWithoutTrailingSlash.value), 1),
						createVNode(unref(ScalarIconCaretDown), {
							class: "text-c-2 ui-open:rotate-180 mt-px size-3 transition-transform duration-100",
							weight: "bold"
						})
					]),
					_: 1
				})]),
				_: 1
			}, 8, [
				"modelValue",
				"options",
				"target",
				"teleport"
			])) : (openBlock(), createElementBlock("div", _hoisted_2, [_cache[2] || (_cache[2] = createElementVNode("span", { class: "sr-only" }, "Server:", -1)), createElementVNode("span", _hoisted_3, toDisplayString(serverUrlWithoutTrailingSlash.value), 1)]));
		};
	}
});
//#endregion
export { ServerDropdown_default as n, ServerSelector_default as t };

//# sourceMappingURL=Server-DnA_BzPS.js.map