import ScalarAsciiArt_default from "../../../../components/ScalarAsciiArt.vue.js";
import Sidebar_default from "../../../components/sidebar/Sidebar.vue.js";
import rabbit_default from "../../../../assets/rabbit.ascii.virtual.js";
import rabbitjump_default from "../../../../assets/rabbitjump.ascii.virtual.js";
import DeleteSidebarListElement_default from "../../../../components/Sidebar/Actions/DeleteSidebarListElement.vue.js";
import DownloadAppButton_default from "./DownloadAppButton.vue.js";
import SidebarItemMenu_default from "./SidebarItemMenu.vue.js";
import { dragHandleFactory } from "../../../helpers/drag-handle-factory.js";
import { Fragment, capitalize, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createSlots, createTextVNode, createVNode, defineComponent, mergeModels, nextTick, normalizeClass, openBlock, ref, renderSlot, toDisplayString, unref, useModel, withCtx, withKeys, withModifiers } from "vue";
import { ScalarButton, ScalarIconButton, ScalarModal, ScalarSidebarItem, useModal } from "@scalar/components";
import { LibraryIcon } from "@scalar/icons/library";
import { isMacOS } from "@scalar/helpers/general/is-mac-os";
import { ScalarIconDotsThree, ScalarIconGearSix, ScalarIconPlus } from "@scalar/icons";
import { getParentEntry } from "@scalar/workspace-store/navigation";
//#region src/v2/features/app/components/AppSidebar.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex" };
var _hoisted_2 = { class: "flex items-center gap-0.5" };
var _hoisted_3 = {
	key: 0,
	class: "relative flex items-center"
};
var _hoisted_4 = {
	key: 0,
	class: "empty-sidebar-item-content overflow-hidden px-2.5 py-2.5"
};
var _hoisted_5 = { class: "rabbit-ascii relative m-auto mt-2 h-[68px] w-[60px]" };
var _hoisted_6 = { class: "flex flex-col gap-1.5 p-2" };
var _hoisted_7 = { class: "text-sidebar-c-2 rounded border px-1.25 py-1 text-xs leading-none font-medium uppercase" };
var AppSidebar_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "AppSidebar",
	props: /* @__PURE__ */ mergeModels({
		layout: {},
		sidebarState: {},
		isWorkspaceOpen: { type: Boolean },
		activeWorkspace: {},
		workspaces: {},
		eventBus: {},
		store: {}
	}, {
		"isSidebarOpen": {
			type: Boolean,
			required: true
		},
		"isSidebarOpenModifiers": {},
		"sidebarWidth": {
			required: true,
			default: 288
		},
		"sidebarWidthModifiers": {}
	}),
	emits: /* @__PURE__ */ mergeModels([
		"click:workspace",
		"selectItem",
		"select:workspace",
		"create:workspace"
	], ["update:isSidebarOpen", "update:sidebarWidth"]),
	setup(__props, { emit: __emit }) {
		const emit = __emit;
		/** The label for the workspace button in the sidebar */
		const workspaceLabel = computed(() => capitalize(__props.activeWorkspace.label));
		/** Controls the visibility of the sidebar */
		const isSidebarOpen = useModel(__props, "isSidebarOpen");
		/** Controls the width of the sidebar */
		const sidebarWidth = useModel(__props, "sidebarWidth");
		/** Calculate if we should show the getting started section */
		const showGettingStarted = computed(() => __props.sidebarState.items.value.length <= 1);
		const dragHandlers = computed(() => dragHandleFactory({
			store: __props.store,
			sidebarState: __props.sidebarState
		}));
		const handleDragEnd = (draggingItem, hoveredItem) => {
			return dragHandlers.value.handleDragEnd(draggingItem, hoveredItem);
		};
		const isDroppable = (draggingItem, hoveredItem) => {
			return dragHandlers.value.isDroppable(draggingItem, hoveredItem);
		};
		/** The current target for the dropdown menu */
		const menuTarget = ref(null);
		const deleteModalState = useModal();
		/** Computes the message for the delete modal */
		const deleteMessage = computed(() => {
			const item = menuTarget.value?.item;
			if (item?.type === "document") return "This cannot be undone. You're about to delete the document and all tags and operations inside it.";
			return `Are you sure you want to delete this ${item?.type ?? "item"}? This action cannot be undone.`;
		});
		/** Deletes an item from the sidebar by emitting the appropriate event */
		const handleDelete = () => {
			const item = menuTarget.value?.item;
			if (!item) return;
			const result = __props.sidebarState.getEntryById(item.id);
			const document = getParentEntry("document", result);
			const operation = getParentEntry("operation", result);
			if (!document) return;
			if (item.type === "document") __props.eventBus.emit("document:delete:document", { name: document.name });
			else if (item.type === "tag") __props.eventBus.emit("tag:delete:tag", {
				documentName: document.name,
				name: item.name
			});
			else if (item.type === "operation") {
				if (!operation) return;
				__props.eventBus.emit("operation:delete:operation", {
					meta: {
						method: operation.method,
						path: operation.path
					},
					documentName: document.name
				});
			} else if (item.type === "example") {
				if (!operation) return;
				__props.eventBus.emit("operation:delete:example", {
					meta: {
						method: operation.method,
						path: operation.path,
						exampleKey: item.name
					},
					documentName: document.name
				});
			}
			/** Clean up after deletion */
			deleteModalState.hide();
			menuTarget.value = null;
		};
		/** Opens the dropdown menu for the given item */
		const openMenu = async (event, item) => {
			if (menuTarget.value?.showMenu) return;
			menuTarget.value = {
				item,
				el: event.currentTarget,
				showMenu: true
			};
			await nextTick();
			const cloned = event instanceof MouseEvent ? new MouseEvent(event.type, event) : new KeyboardEvent(event.type, event);
			menuTarget.value?.el.dispatchEvent(cloned);
		};
		/** Closes the dropdown menu */
		const closeMenu = () => {
			if (menuTarget.value) menuTarget.value.showMenu = false;
		};
		/** Opens the command palette with the payload needed to create a request */
		const handleAddEmptyFolder = (item) => {
			const itemWithParent = __props.sidebarState.getEntryById(item.id);
			const documentEntry = getParentEntry("document", itemWithParent);
			const tag = getParentEntry("tag", itemWithParent);
			__props.eventBus.emit("ui:open:command-palette", {
				action: "create-request",
				payload: {
					documentName: documentEntry?.name,
					tagId: tag?.name
				}
			});
		};
		/**
		* Navigates to the operations page for the provided operation item.
		* Emits a navigation event for the operation overview page.
		*/
		const navigateToOperationsPage = (item) => {
			const documentSlug = getParentEntry("document", __props.sidebarState.getEntryById(item.id))?.name;
			__props.eventBus.emit("ui:navigate", {
				page: "operation",
				path: "overview",
				operationPath: item.path,
				method: item.method,
				documentSlug
			});
		};
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock("div", _hoisted_1, [
				createVNode(unref(Sidebar_default), {
					sidebarWidth: sidebarWidth.value,
					"onUpdate:sidebarWidth": _cache[3] || (_cache[3] = ($event) => sidebarWidth.value = $event),
					activeWorkspace: __props.activeWorkspace,
					class: normalizeClass(["max-md:inset-y-0 max-md:z-2 max-md:w-full!", isSidebarOpen.value ? "max-md:fixed! max-md:flex!" : "max-md:hidden!"]),
					documents: Object.values(__props.store.workspace.documents),
					isDroppable,
					layout: __props.layout,
					sidebarState: __props.sidebarState,
					workspaces: __props.workspaces,
					"onCreate:workspace": _cache[4] || (_cache[4] = ($event) => emit("create:workspace")),
					"onNavigate:to:settings": _cache[5] || (_cache[5] = ($event) => __props.eventBus.emit("ui:navigate", {
						page: "workspace",
						path: "settings"
					})),
					onReorder: _cache[6] || (_cache[6] = (draggingItem, hoveredItem) => handleDragEnd(draggingItem, hoveredItem)),
					"onSelect:workspace": _cache[7] || (_cache[7] = (id) => emit("select:workspace", id)),
					onSelectItem: _cache[8] || (_cache[8] = (id) => emit("selectItem", id))
				}, createSlots({
					sidebarMenuActions: withCtx(() => [renderSlot(_ctx.$slots, "sidebarMenuActions", {}, void 0, true)]),
					workspaceButton: withCtx(() => [createVNode(unref(ScalarSidebarItem), {
						is: "button",
						active: __props.isWorkspaceOpen,
						onClick: _cache[0] || (_cache[0] = ($event) => emit("click:workspace"))
					}, {
						default: withCtx(() => [createTextVNode(toDisplayString(workspaceLabel.value), 1)]),
						_: 1
					}, 8, ["active"])]),
					decorator: withCtx(({ item }) => [createElementVNode("div", _hoisted_2, [item.type === "operation" ? (openBlock(), createBlock(unref(ScalarIconButton), {
						key: 0,
						icon: unref(ScalarIconGearSix),
						label: "Operation settings",
						size: "sm",
						weight: "bold",
						onClick: withModifiers(($event) => navigateToOperationsPage(item), ["stop"]),
						onKeydown: [withKeys(withModifiers(($event) => navigateToOperationsPage(item), ["stop"]), ["enter"]), withKeys(withModifiers(($event) => navigateToOperationsPage(item), ["stop"]), ["space"])]
					}, null, 8, [
						"icon",
						"onClick",
						"onKeydown"
					])) : createCommentVNode("", true), createVNode(unref(ScalarIconButton), {
						"aria-expanded": "false",
						"aria-haspopup": "menu",
						icon: unref(ScalarIconDotsThree),
						label: "More options",
						size: "sm",
						weight: "bold",
						onClick: withModifiers((e) => openMenu(e, item), ["stop"]),
						onKeydown: [
							withKeys(withModifiers((e) => openMenu(e, item), ["stop"]), ["down"]),
							withKeys(withModifiers((e) => openMenu(e, item), ["stop"]), ["enter"]),
							withKeys(withModifiers((e) => openMenu(e, item), ["stop"]), ["space"]),
							withKeys(withModifiers((e) => openMenu(e, item), ["stop"]), ["up"])
						]
					}, null, 8, [
						"icon",
						"onClick",
						"onKeydown"
					])])]),
					icon: withCtx(({ item }) => [item.type === "document" && __props.store.workspace.documents[item.name]?.["x-scalar-is-dirty"] === true ? (openBlock(), createElementBlock("div", _hoisted_3, [createVNode(unref(LibraryIcon), {
						class: "block",
						src: "icon" in item && item.icon || "interface-content-folder"
					}, null, 8, ["src"]), _cache[11] || (_cache[11] = createElementVNode("div", { class: "bg-orange absolute -top-1.25 -right-1.25 flex h-2.5 w-2.5 items-center justify-center rounded-full text-[9px] font-bold text-white" }, " ! ", -1))])) : createCommentVNode("", true)]),
					empty: withCtx(({ item }) => [createVNode(unref(ScalarSidebarItem), {
						is: "button",
						onClick: ($event) => handleAddEmptyFolder(item)
					}, {
						icon: withCtx(() => [createVNode(unref(ScalarIconPlus))]),
						default: withCtx(() => [..._cache[12] || (_cache[12] = [createTextVNode("Add operation", -1)])]),
						_: 1
					}, 8, ["onClick"])]),
					_: 2
				}, [__props.layout !== "modal" ? {
					name: "footer",
					fn: withCtx(() => [createElementVNode("div", { class: normalizeClass({ "empty-sidebar-item border-t": showGettingStarted.value }) }, [showGettingStarted.value ? (openBlock(), createElementBlock("div", _hoisted_4, [createElementVNode("div", _hoisted_5, [createVNode(ScalarAsciiArt_default, {
						art: unref(rabbit_default),
						class: "rabbitsit font-bold"
					}, null, 8, ["art"]), createVNode(ScalarAsciiArt_default, {
						art: unref(rabbitjump_default),
						class: "rabbitjump absolute top-0 left-0 font-bold"
					}, null, 8, ["art"])]), _cache[13] || (_cache[13] = createElementVNode("div", { class: "mt-2 mb-2 text-center text-sm text-balance" }, [createElementVNode("b", { class: "font-medium" }, "Let's Get Started"), createElementVNode("p", { class: "mt-2 leading-5" }, " Create request, folder, collection or import from OpenAPI/Postman ")], -1))])) : createCommentVNode("", true), createElementVNode("div", _hoisted_6, [
						showGettingStarted.value ? (openBlock(), createBlock(unref(ScalarButton), {
							key: 0,
							class: "w-full",
							size: "sm",
							onClick: _cache[1] || (_cache[1] = ($event) => __props.eventBus.emit("ui:open:command-palette", {
								action: "import-from-openapi-swagger-postman-curl",
								payload: void 0
							}))
						}, {
							default: withCtx(() => [..._cache[14] || (_cache[14] = [createTextVNode(" Import Collection ", -1)])]),
							_: 1
						})) : createCommentVNode("", true),
						createVNode(unref(ScalarButton), {
							class: "w-full",
							hotkey: "K",
							size: "sm",
							variant: "outlined",
							onClick: _cache[2] || (_cache[2] = ($event) => __props.eventBus.emit("ui:open:command-palette"))
						}, {
							default: withCtx(() => [_cache[20] || (_cache[20] = createTextVNode(" Add Item \xA0 ", -1)), createElementVNode("span", _hoisted_7, [unref(isMacOS)() ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [_cache[15] || (_cache[15] = createElementVNode("span", { class: "sr-only" }, "Command", -1)), _cache[16] || (_cache[16] = createElementVNode("span", { "aria-hidden": "true" }, "⌘", -1))], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [_cache[17] || (_cache[17] = createElementVNode("span", { class: "sr-only" }, "CTRL", -1)), _cache[18] || (_cache[18] = createElementVNode("span", { "aria-hidden": "true" }, "⌃", -1))], 64)), _cache[19] || (_cache[19] = createTextVNode(" K ", -1))])]),
							_: 1
						}),
						__props.layout === "web" ? (openBlock(), createBlock(DownloadAppButton_default, { key: 1 })) : createCommentVNode("", true)
					])], 2)]),
					key: "0"
				} : void 0]), 1032, [
					"sidebarWidth",
					"activeWorkspace",
					"class",
					"documents",
					"layout",
					"sidebarState",
					"workspaces"
				]),
				menuTarget.value?.showMenu ? (openBlock(), createBlock(SidebarItemMenu_default, {
					key: 0,
					eventBus: __props.eventBus,
					item: menuTarget.value.item,
					sidebarState: __props.sidebarState,
					target: menuTarget.value.el,
					onCloseMenu: closeMenu,
					onShowDeleteModal: _cache[9] || (_cache[9] = ($event) => unref(deleteModalState).show())
				}, null, 8, [
					"eventBus",
					"item",
					"sidebarState",
					"target"
				])) : createCommentVNode("", true),
				menuTarget.value ? (openBlock(), createBlock(unref(ScalarModal), {
					key: 1,
					size: "xxs",
					state: unref(deleteModalState),
					title: `Delete ${menuTarget.value.item.title}`
				}, {
					default: withCtx(() => [createVNode(DeleteSidebarListElement_default, {
						variableName: menuTarget.value.item.title,
						warningMessage: deleteMessage.value,
						onClose: _cache[10] || (_cache[10] = ($event) => unref(deleteModalState).hide()),
						onDelete: handleDelete
					}, null, 8, ["variableName", "warningMessage"])]),
					_: 1
				}, 8, ["state", "title"])) : createCommentVNode("", true)
			]);
		};
	}
});
//#endregion
export { AppSidebar_vue_vue_type_script_setup_true_lang_default as default };

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