import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { useLayout } from "../../hooks/useLayout.js";
import { useSidebar } from "../../hooks/useSidebar.js";
import { getModifiers } from "../../libs/hot-keys.js";
import { useWorkspace } from "../../store/store.js";
import HttpMethod_default from "../../components/HttpMethod/HttpMethod.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, nextTick, normalizeClass, openBlock, ref, renderList, renderSlot, resolveComponent, toDisplayString, unref, useCssVars, withCtx, withModifiers } from "vue";
import { ScalarButton, ScalarIcon, ScalarSidebarGroupToggle, ScalarTooltip } from "@scalar/components";
import { RouterLink, useRouter } from "vue-router";
import { shouldIgnoreEntity } from "@scalar/oas-utils/helpers";
import { Draggable } from "@scalar/draggable";
//#region src/views/Request/RequestSidebarItem.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "line-clamp-1 w-full pl-2 break-all" };
var _hoisted_2 = { class: "flex flex-row items-center gap-1" };
var _hoisted_3 = { class: "relative" };
var _hoisted_4 = { class: "flex items-start" };
var _hoisted_5 = { class: "flex h-5 max-w-[14px] shrink-0 items-center justify-center" };
var _hoisted_6 = { class: "flex flex-1 flex-row justify-between font-medium" };
var _hoisted_7 = { class: "line-clamp-1 w-full text-left break-all" };
var _hoisted_8 = { class: "relative flex h-fit items-center justify-end gap-0.5" };
var _hoisted_9 = {
	class: "flex items-center justify-center",
	type: "button"
};
var _hoisted_10 = ["aria-expanded"];
var _hoisted_11 = { class: "flex h-5 max-w-[14px] shrink-0 items-center justify-center" };
var _hoisted_12 = { class: "flex flex-1 flex-row justify-between" };
var _hoisted_13 = { class: "line-clamp-1 w-full text-left font-medium break-all" };
var _hoisted_14 = { class: "relative flex h-fit items-center justify-end gap-0.5" };
var _hoisted_15 = {
	class: "flex items-center justify-center",
	type: "button"
};
var _hoisted_16 = { key: 3 };
var highlightClasses = "hover:bg-sidebar-b-active indent-padding-left";
/** Due to the nesting, we need a dynamic left offset for hover and active backgrounds */
var RequestSidebarItem_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "RequestSidebarItem",
	props: {
		isDraggable: {
			type: Boolean,
			default: false
		},
		isDroppable: {
			type: [Boolean, Function],
			default: false
		},
		parentUids: {},
		uid: {},
		menuItem: {}
	},
	emits: [
		"onDragEnd",
		"newTab",
		"openMenu"
	],
	setup(__props, { emit: __emit }) {
		useCssVars((_ctx) => ({
			"v2bb71a6d": leftOffset.value,
			"b961af9e": paddingOffset.value
		}));
		const emit = __emit;
		const { activeCollection, activeRequest, activeRouterParams, activeWorkspace } = useActiveEntities();
		const { collections, tags, requests, requestExamples, collectionMutators, tagMutators, requestMutators, requestExampleMutators, events } = useWorkspace();
		const router = useRouter();
		const { collapsedSidebarFolders, toggleSidebarFolder } = useSidebar();
		const { layout } = useLayout();
		/** Normalize properties across different types for easy consumption */
		const item = computed(() => {
			const collection = collections[__props.uid];
			const tag = tags[__props.uid];
			const request = requests[__props.uid];
			const requestExample = requestExamples[__props.uid];
			if (collection) return {
				title: collection.info?.title || "Untitled Collection",
				entity: collection,
				resourceTitle: "Collection",
				children: collection.children,
				icon: collection["x-scalar-icon"],
				documentUrl: collection.documentUrl,
				watchMode: collection.watchMode,
				to: collection.uid && collection?.info?.title !== "Drafts" ? {
					name: "collection",
					params: {
						[PathId.Workspace]: activeWorkspace.value?.uid,
						[PathId.Collection]: collection.uid
					}
				} : void 0,
				warning: "This cannot be undone. You're about to delete the collection and all folders and requests inside it.",
				edit: (name, icon) => {
					collectionMutators.edit(collection.uid, "info.title", name);
					if (icon) collectionMutators.edit(collection.uid, "x-scalar-icon", icon);
				},
				delete: () => {
					if (activeWorkspace.value) collectionMutators.delete(collection, activeWorkspace.value);
				}
			};
			if (tag) return {
				title: tag.name,
				entity: tag,
				resourceTitle: "Tag",
				children: tag.children,
				warning: "This cannot be undone. You're about to delete the tag and all requests inside it",
				edit: (name) => tagMutators.edit(tag.uid, "name", name),
				delete: () => __props.parentUids[0] && tagMutators.delete(tag, __props.parentUids[0])
			};
			if (request) return {
				title: request.summary ?? request.path,
				to: {
					name: "request",
					params: {
						workspace: activeWorkspace.value?.uid,
						request: request.uid
					}
				},
				method: request.method,
				entity: request,
				resourceTitle: "Request",
				warning: "This cannot be undone. You're about to delete the request.",
				children: request.examples.slice(1),
				edit: (name) => requestMutators.edit(request.uid, "summary", name),
				delete: () => __props.parentUids[0] && requestMutators.delete(request, __props.parentUids[0])
			};
			if (requestExample?.requestUid) return {
				title: requestExample.name,
				to: {
					name: "request.examples",
					params: {
						workspace: activeWorkspace.value?.uid,
						request: requestExample.requestUid,
						examples: requestExample.uid
					}
				},
				method: requests[requestExample.requestUid]?.method,
				entity: requestExample,
				resourceTitle: "Example",
				warning: "This cannot be undone. You're about to delete the example from the request.",
				children: [],
				edit: (name) => requestExampleMutators.edit(requestExample.uid, "name", name),
				delete: () => requestExampleMutators.delete(requestExample)
			};
			return {
				title: "Unknown",
				entity: {
					uid: "",
					type: "unknown"
				},
				resourceTitle: "Unknown",
				children: [],
				edit: () => null,
				delete: () => null
			};
		});
		/** Checks to see if it is a draft collection with the title of Drafts */
		const isDraftCollection = computed(() => item.value.entity.type === "collection" && item.value.title === "Drafts");
		const leftOffset = computed(() => {
			if (!__props.parentUids.length) return "12px";
			if (layout === "modal") return `${(__props.parentUids.length - 1) * 12}px`;
			return `${__props.parentUids.length * 12}px`;
		});
		const paddingOffset = computed(() => {
			if (!__props.parentUids.length) return "0px";
			if (layout === "modal") return `${(__props.parentUids.length - 1) * 12}px`;
			return `${__props.parentUids.length * 12}px`;
		});
		/**
		* Show folders if they are open,
		* show examples if there are more than one and the request is active
		*/
		const showChildren = computed(() => collapsedSidebarFolders[__props.uid] || activeRequest.value?.uid === __props.uid && item.value.entity.examples.length > 1);
		/** Since we have exact routing, we should check if the default request is active */
		const isDefaultActive = computed(() => typeof router.currentRoute.value.name === "string" && router.currentRoute.value.name.startsWith("request") && activeRouterParams.value[PathId.Request] === "default" && activeRequest.value?.uid === __props.uid);
		/** The draggable component */
		const draggableRef = ref(null);
		/** Calculate offsets which change a little depending on whats being dragged and hovered over */
		const getDraggableOffsets = computed(() => {
			let ceiling = .5;
			let floor = .5;
			if (!draggableRef.value) return {
				ceiling,
				floor
			};
			const { draggingItem } = draggableRef.value;
			if (!collections[draggingItem?.id] && item.value.entity.type === "collection") {
				ceiling = 1;
				floor = 0;
			} else if (item.value.entity.type === "tag") {
				ceiling = .8;
				floor = .2;
			}
			return {
				ceiling,
				floor
			};
		});
		/** Guard to check if an element is able to be dropped on */
		const _isDroppable = (draggingItem, hoveredItem) => {
			if (layout === "modal") return false;
			if (requestExamples[hoveredItem.id]) return false;
			if (collections[draggingItem.id]) return false;
			return true;
		};
		const handleNavigation = (event, _item) => {
			if (event) {
				if (getModifiers(["default"]).some((key) => event[key])) emit("newTab", _item.title || "", _item.entity.uid);
				else if (_item.to) router.push(_item.to);
				nextTick(() => events.focusAddressBar.emit());
			}
		};
		function addRequest(entityUid) {
			const collectionUid = __props.parentUids[0] ? collections[__props.parentUids[0]]?.uid || "" : entityUid;
			const requestData = __props.parentUids[0] && tags[entityUid]?.name ? { tags: [tags[entityUid].name] } : {};
			const newRequest = requestMutators.add(requestData, collectionUid);
			if (newRequest) {
				router.push({
					name: "request",
					params: {
						workspace: activeWorkspace.value?.uid,
						request: newRequest.uid
					}
				});
				events.hotKeys.emit({ focusAddressBar: new KeyboardEvent("keydown", { key: "l" }) });
			}
		}
		const watchIconColor = computed(() => {
			const { uid: _uid, watchModeStatus } = activeCollection.value || {};
			if (_uid !== item.value.entity.uid) return "text-c-3";
			if (watchModeStatus === "WATCHING") return "text-c-1";
			if (watchModeStatus === "ERROR") return "text-red";
			return "text-c-3";
		});
		const hasDraftRequests = computed(() => {
			return item.value.title === "Drafts" && layout !== "modal" && item.value.children.length > 0;
		});
		/**
		* Check if the item should be shown.
		* This is used to hide items that are marked as hidden/internal.
		*/
		const shouldShowItem = computed(() => {
			const request = requests[__props.uid];
			if (request) return !shouldIgnoreEntity(request);
			const tag = tags[__props.uid];
			if (tag) return !shouldIgnoreEntity(tag);
			return true;
		});
		return (_ctx, _cache) => {
			const _component_RequestSidebarItem = resolveComponent("RequestSidebarItem", true);
			return shouldShowItem.value ? (openBlock(), createElementBlock("li", {
				key: 0,
				class: normalizeClass(["relative flex flex-row", [unref(layout) === "modal" && __props.parentUids.length > 1 || unref(layout) !== "modal" && __props.parentUids.length ? "before:bg-border indent-border-line-offset mb-[.5px] before:pointer-events-none before:absolute before:top-0 before:left-[calc(.75rem_+_.5px)] before:z-1 before:h-[calc(100%_+_.5px)] before:w-[.5px] last:mb-0 last:before:h-full" : ""]])
			}, [createVNode(unref(Draggable), {
				id: item.value.entity.uid,
				ref_key: "draggableRef",
				ref: draggableRef,
				ceiling: getDraggableOffsets.value.ceiling,
				class: "gap-1/2 flex flex-1 flex-col text-base/5",
				floor: getDraggableOffsets.value.floor,
				isDraggable: __props.isDraggable,
				isDroppable: __props.isDroppable,
				parentIds: __props.parentUids,
				onOnDragEnd: _cache[13] || (_cache[13] = (...args) => _ctx.$emit("onDragEnd", ...args))
			}, {
				default: withCtx(() => [(item.value.entity.type === "request" || item.value.entity.type === "requestExample") && item.value.to ? (openBlock(), createBlock(unref(RouterLink), {
					key: 0,
					class: "group no-underline",
					to: item.value.to,
					onClick: _cache[1] || (_cache[1] = withModifiers((event) => handleNavigation(event, item.value), ["prevent"]))
				}, {
					default: withCtx(({ isExactActive }) => [createElementVNode("div", { class: normalizeClass(["relative flex min-h-8 w-full cursor-pointer flex-row items-start justify-between gap-0.5 rounded py-1.5 pr-2", [highlightClasses, isExactActive || isDefaultActive.value ? "bg-sidebar-b-active text-sidebar-c-active font-medium transition-none" : "text-sidebar-c-2"]]) }, [createElementVNode("span", _hoisted_1, toDisplayString(item.value.title || "Untitled"), 1), createElementVNode("div", _hoisted_2, [createElementVNode("div", _hoisted_3, [unref(layout) !== "modal" ? (openBlock(), createBlock(unref(ScalarButton), {
						key: 0,
						class: normalizeClass(["hover:bg-b-3 hidden aspect-square h-fit px-0.5 py-0 opacity-0 group-hover:flex group-hover:opacity-100 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100", { flex: __props.menuItem?.item?.entity.uid === item.value.entity.uid && __props.menuItem.open }]),
						size: "sm",
						type: "button",
						variant: "ghost",
						onClick: _cache[0] || (_cache[0] = withModifiers((ev) => _ctx.$emit("openMenu", {
							item: item.value,
							parentUids: __props.parentUids,
							targetRef: ev.currentTarget,
							open: !__props.menuItem.open
						}), ["stop", "prevent"]))
					}, {
						default: withCtx(() => [createVNode(unref(ScalarIcon), {
							icon: "Ellipses",
							size: "md"
						})]),
						_: 1
					}, 8, ["class"])) : createCommentVNode("", true)]), createElementVNode("span", _hoisted_4, [
						_cache[14] || (_cache[14] = createTextVNode("   ", -1)),
						_cache[15] || (_cache[15] = createElementVNode("span", { class: "sr-only" }, "HTTP Method:", -1)),
						item.value.method ? (openBlock(), createBlock(unref(HttpMethod_default), {
							key: 0,
							class: "font-bold",
							method: item.value.method
						}, null, 8, ["method"])) : createCommentVNode("", true)
					])])], 2)]),
					_: 1
				}, 8, ["to"])) : (unref(layout) !== "modal" || __props.parentUids.length) && item.value.entity.type === "collection" && item.value.to ? (openBlock(), createBlock(unref(RouterLink), {
					key: 1,
					"aria-expanded": Boolean(unref(collapsedSidebarFolders)[item.value.entity.uid]),
					class: normalizeClass(["hover:bg-b-2 group relative flex w-full flex-row justify-start gap-1.5 rounded p-1.5 no-underline focus-visible:z-10", [highlightClasses, {
						"bg-sidebar-b-active text-sidebar-c-active transition-none": typeof unref(router).currentRoute.value.name === "string" && unref(router).currentRoute.value.name.startsWith("collection") && unref(router).currentRoute.value.params[unref(PathId).Collection] === item.value.entity.uid,
						"text-c-2": item.value.title === "Untitled Collection"
					}]]),
					to: item.value.to
				}, {
					default: withCtx(() => [createElementVNode("span", _hoisted_5, [renderSlot(_ctx.$slots, "leftIcon", {}, void 0, true), _cache[16] || (_cache[16] = createTextVNode("   ", -1))]), createElementVNode("div", _hoisted_6, [createElementVNode("span", _hoisted_7, toDisplayString(item.value.title), 1), createElementVNode("div", _hoisted_8, [
						createElementVNode("div", { class: normalizeClass(["items-center gap-px opacity-0 group-hover:flex group-hover:opacity-100 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100", {
							flex: __props.menuItem.open,
							hidden: !__props.menuItem.open || __props.menuItem.item?.entity.uid !== item.value.entity.uid
						}]) }, [unref(layout) !== "modal" && !isDraftCollection.value || isDraftCollection.value && hasDraftRequests.value ? (openBlock(), createBlock(unref(ScalarButton), {
							key: 0,
							class: "hover:bg-b-3 hover:text-c-1 aspect-square h-fit px-0.5 py-0 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100",
							size: "sm",
							variant: "ghost",
							onClick: _cache[2] || (_cache[2] = withModifiers((ev) => _ctx.$emit("openMenu", {
								item: item.value,
								parentUids: __props.parentUids,
								targetRef: ev.currentTarget?.parentNode,
								open: true
							}), ["stop", "prevent"]))
						}, {
							default: withCtx(() => [createVNode(unref(ScalarIcon), {
								icon: "Ellipses",
								size: "md"
							})]),
							_: 1
						})) : createCommentVNode("", true), unref(layout) !== "modal" ? (openBlock(), createBlock(unref(ScalarButton), {
							key: 1,
							class: "hover:bg-b-3 hover:text-c-1 aspect-square h-fit px-0.5 py-0 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100",
							size: "sm",
							variant: "ghost",
							onClick: _cache[3] || (_cache[3] = withModifiers(($event) => addRequest(item.value.entity.uid), ["stop", "prevent"]))
						}, {
							default: withCtx(() => [createVNode(unref(ScalarIcon), {
								icon: "Add",
								size: "md",
								thickness: "2"
							})]),
							_: 1
						})) : createCommentVNode("", true)], 2),
						item.value.watchMode ? (openBlock(), createBlock(unref(ScalarTooltip), {
							key: 0,
							content: `Watching: ${item.value.documentUrl}`,
							offset: 12,
							placement: "right"
						}, {
							default: withCtx(() => [createElementVNode("button", _hoisted_9, [createVNode(unref(ScalarIcon), {
								class: normalizeClass(["ml-0.5 text-sm", watchIconColor.value]),
								icon: "Watch",
								size: "md",
								thickness: "2"
							}, null, 8, ["class"])])]),
							_: 1
						}, 8, ["content"])) : createCommentVNode("", true),
						createElementVNode("span", {
							class: "flex cursor-pointer items-center justify-center",
							onClick: _cache[4] || (_cache[4] = withModifiers(($event) => unref(toggleSidebarFolder)(item.value.entity.uid), ["stop", "prevent"]))
						}, [createVNode(unref(ScalarSidebarGroupToggle), {
							class: "text-c-3 hover:text-c-1 shrink-0",
							open: Boolean(unref(collapsedSidebarFolders)[item.value.entity.uid])
						}, null, 8, ["open"])])
					])])]),
					_: 3
				}, 8, [
					"aria-expanded",
					"class",
					"to"
				])) : unref(layout) !== "modal" || __props.parentUids.length ? (openBlock(), createElementBlock("button", {
					key: 2,
					"aria-expanded": Boolean(unref(collapsedSidebarFolders)[item.value.entity.uid]),
					class: normalizeClass(["hover:bg-b-2 group relative flex w-full flex-row justify-start gap-1.5 rounded p-1.5 focus-visible:z-10", [highlightClasses]]),
					type: "button",
					onClick: _cache[8] || (_cache[8] = ($event) => unref(toggleSidebarFolder)(item.value.entity.uid))
				}, [createElementVNode("span", _hoisted_11, [renderSlot(_ctx.$slots, "leftIcon", {}, void 0, true), _cache[17] || (_cache[17] = createTextVNode("   ", -1))]), createElementVNode("div", _hoisted_12, [createElementVNode("span", _hoisted_13, toDisplayString(item.value.title), 1), createElementVNode("div", _hoisted_14, [
					createElementVNode("div", { class: normalizeClass(["items-center gap-px opacity-0 group-hover:flex group-hover:opacity-100 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100", {
						flex: __props.menuItem.open,
						hidden: !__props.menuItem.open || __props.menuItem.item?.entity.uid !== item.value.entity.uid
					}]) }, [unref(layout) !== "modal" && !isDraftCollection.value || isDraftCollection.value && hasDraftRequests.value ? (openBlock(), createBlock(unref(ScalarButton), {
						key: 0,
						class: "hover:bg-b-3 hover:text-c-1 aspect-square h-fit px-0.5 py-0 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100",
						size: "sm",
						variant: "ghost",
						onClick: _cache[5] || (_cache[5] = withModifiers((ev) => _ctx.$emit("openMenu", {
							item: item.value,
							parentUids: __props.parentUids,
							targetRef: ev.currentTarget?.parentNode,
							open: true
						}), ["stop", "prevent"]))
					}, {
						default: withCtx(() => [createVNode(unref(ScalarIcon), {
							icon: "Ellipses",
							size: "md"
						})]),
						_: 1
					})) : createCommentVNode("", true), unref(layout) !== "modal" ? (openBlock(), createBlock(unref(ScalarButton), {
						key: 1,
						class: "hover:bg-b-3 hover:text-c-1 aspect-square h-fit px-0.5 py-0 group-focus-visible:opacity-100 group-has-[:focus-visible]:opacity-100",
						size: "sm",
						variant: "ghost",
						onClick: _cache[6] || (_cache[6] = withModifiers(($event) => addRequest(item.value.entity.uid), ["stop", "prevent"]))
					}, {
						default: withCtx(() => [createVNode(unref(ScalarIcon), {
							icon: "Add",
							size: "md",
							thickness: "2"
						})]),
						_: 1
					})) : createCommentVNode("", true)], 2),
					item.value.watchMode ? (openBlock(), createBlock(unref(ScalarTooltip), {
						key: 0,
						content: "Watching: {{ item.documentUrl }}",
						offset: 12,
						placement: "right"
					}, {
						default: withCtx(() => [createElementVNode("button", _hoisted_15, [createVNode(unref(ScalarIcon), {
							class: normalizeClass(["ml-0.5 text-sm", watchIconColor.value]),
							icon: "Watch",
							size: "md",
							thickness: "2"
						}, null, 8, ["class"])])]),
						_: 1
					})) : createCommentVNode("", true),
					createElementVNode("span", {
						class: "flex cursor-pointer items-center justify-center",
						onClick: _cache[7] || (_cache[7] = withModifiers(($event) => unref(toggleSidebarFolder)(item.value.entity.uid), ["stop", "prevent"]))
					}, [createVNode(unref(ScalarSidebarGroupToggle), {
						class: "text-c-3 hover:text-c-1 shrink-0",
						open: Boolean(unref(collapsedSidebarFolders)[item.value.entity.uid])
					}, null, 8, ["open"])])
				])])], 10, _hoisted_10)) : createCommentVNode("", true), showChildren.value ? (openBlock(), createElementBlock("ul", _hoisted_16, [(openBlock(true), createElementBlock(Fragment, null, renderList(item.value.children, (childUid) => {
					return openBlock(), createBlock(_component_RequestSidebarItem, {
						key: childUid,
						isDraggable: !unref(requestExamples)[childUid],
						isDroppable: _isDroppable,
						menuItem: __props.menuItem,
						parentUids: [...__props.parentUids, __props.uid],
						uid: childUid,
						onNewTab: _cache[9] || (_cache[9] = (name, uid) => _ctx.$emit("newTab", name, uid)),
						onOnDragEnd: _cache[10] || (_cache[10] = (...args) => _ctx.$emit("onDragEnd", ...args)),
						onOpenMenu: _cache[11] || (_cache[11] = (item) => _ctx.$emit("openMenu", item))
					}, null, 8, [
						"isDraggable",
						"menuItem",
						"parentUids",
						"uid"
					]);
				}), 128)), item.value.children.length === 0 ? (openBlock(), createBlock(unref(ScalarButton), {
					key: 0,
					class: normalizeClass(["text-c-1 hover:bg-b-2 flex h-8 w-full justify-start gap-1.5 py-0 text-xs", __props.parentUids.length ? "pl-9" : ""]),
					variant: "ghost",
					onClick: _cache[12] || (_cache[12] = ($event) => addRequest(item.value.entity.uid))
				}, {
					default: withCtx(() => [createVNode(unref(ScalarIcon), {
						icon: "Add",
						size: "sm"
					}), _cache[18] || (_cache[18] = createElementVNode("span", null, "Add Request", -1))]),
					_: 1
				}, 8, ["class"])) : createCommentVNode("", true)])) : createCommentVNode("", true)]),
				_: 3
			}, 8, [
				"id",
				"ceiling",
				"floor",
				"isDraggable",
				"isDroppable",
				"parentIds"
			])], 2)) : createCommentVNode("", true);
		};
	}
});
//#endregion
export { RequestSidebarItem_vue_vue_type_script_setup_true_lang_default as default };

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