import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { createInitialRequest } from "../../store/requests.js";
import { useWorkspace } from "../../store/store.js";
import DeleteSidebarListElement_default from "../../components/Sidebar/Actions/DeleteSidebarListElement.vue.js";
import EditSidebarListElement_default from "../../components/Sidebar/Actions/EditSidebarListElement.vue.js";
import EditSidebarListCollection_default from "../../components/Sidebar/Actions/EditSidebarListCollection.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, onBeforeUnmount, onMounted, openBlock, ref, toDisplayString, unref, watch, withCtx, withKeys } from "vue";
import { ScalarDropdownButton, ScalarDropdownMenu, ScalarFloating, ScalarIcon, ScalarModal, useModal } from "@scalar/components";
import { useRouter } from "vue-router";
//#region src/views/Request/RequestSidebarItemMenu.vue?vue&type=script&setup=true&lang.ts
var RequestSidebarItemMenu_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "RequestSidebarItemMenu",
	props: { menuItem: {} },
	emits: [
		"closeMenu",
		"toggleWatchMode",
		"clearDrafts"
	],
	setup(__props, { emit: __emit }) {
		const props = __props;
		const emit = __emit;
		const { replace } = useRouter();
		const { activeRouterParams, activeWorkspaceCollections, activeWorkspaceRequests } = useActiveEntities();
		const { events, requestMutators } = useWorkspace();
		const editModal = useModal();
		const deleteModal = useModal();
		const clearDraftsModal = useModal();
		/** Add example */
		const handleAddExample = () => events.commandPalette.emit({
			commandName: "Add Example",
			metaData: { itemUid: props.menuItem.item?.entity.uid }
		});
		const handleEdit = (newName, newIcon) => {
			props.menuItem.item?.edit(newName, newIcon);
			editModal.hide();
		};
		/** Delete with redirect for both requests and requestExamples */
		const handleItemDelete = () => {
			props.menuItem.item?.delete();
			if (!activeWorkspaceRequests.value.length) {
				const { request } = createInitialRequest();
				const draftCollection = activeWorkspaceCollections.value.find((collection) => collection.info?.title === "Drafts");
				if (draftCollection) {
					requestMutators.add(request, draftCollection.uid);
					replace({
						name: "request",
						params: { [PathId.Request]: request.uid }
					});
				}
			}
			if (activeRouterParams.value[PathId.Request] === props.menuItem.item?.entity.uid) replace({
				name: "request",
				params: { [PathId.Request]: "default" }
			});
			if (activeRouterParams.value[PathId.Examples] === props.menuItem.item?.entity.uid) replace({
				name: "request",
				params: { [PathId.Request]: "default" }
			});
			if (activeWorkspaceCollections.value[0]) {
				const firstRequest = activeWorkspaceCollections.value[0].requests[0];
				replace({
					name: "request",
					params: { [PathId.Request]: firstRequest }
				});
			}
			deleteModal.hide();
		};
		const menuRef = ref(null);
		watch([() => props.menuItem.open, menuRef], ([open]) => {
			if (open && menuRef.value?.$parent?.$el) menuRef.value.$parent.$el.focus();
		});
		const globalClickListener = () => props.menuItem.open && emit("closeMenu");
		onMounted(() => window.addEventListener("click", globalClickListener));
		onBeforeUnmount(() => window.removeEventListener("click", globalClickListener));
		const toggleWatchMode = () => {
			emit("toggleWatchMode", props.menuItem.item);
		};
		const handleClearDrafts = () => {
			emit("clearDrafts");
			clearDraftsModal.hide();
		};
		const isDraftsMenuItem = computed(() => {
			return props.menuItem.item?.title === "Drafts";
		});
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock(Fragment, null, [
				__props.menuItem.targetRef && __props.menuItem.open ? (openBlock(), createBlock(unref(ScalarFloating), {
					key: 0,
					placement: "right-start",
					target: __props.menuItem.targetRef,
					teleport: ""
				}, {
					floating: withCtx(() => [createVNode(unref(ScalarDropdownMenu), { onKeydown: _cache[3] || (_cache[3] = withKeys(($event) => _ctx.$emit("closeMenu"), ["escape"])) }, {
						default: withCtx(() => [
							__props.menuItem.item?.entity.type === "request" ? (openBlock(), createBlock(unref(ScalarDropdownButton), {
								key: 0,
								class: "flex items-center gap-2",
								onClick: handleAddExample
							}, {
								default: withCtx(() => [createVNode(unref(ScalarIcon), {
									class: "inline-flex",
									icon: "Example",
									size: "md",
									thickness: "1.5"
								}), _cache[8] || (_cache[8] = createElementVNode("span", null, "Add Example", -1))]),
								_: 1
							})) : createCommentVNode("", true),
							!isDraftsMenuItem.value ? (openBlock(), createBlock(unref(ScalarDropdownButton), {
								key: 1,
								ref_key: "menuRef",
								ref: menuRef,
								class: "flex items-center gap-2",
								onClick: _cache[0] || (_cache[0] = ($event) => unref(editModal).show())
							}, {
								default: withCtx(() => [createVNode(unref(ScalarIcon), {
									class: "inline-flex",
									icon: "Edit",
									size: "md",
									thickness: "1.5"
								}), createElementVNode("span", null, [__props.menuItem.item?.entity.type === "collection" ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createTextVNode(" Edit ")], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [createTextVNode(" Rename ")], 64))])]),
								_: 1
							}, 512)) : createCommentVNode("", true),
							__props.menuItem.item?.documentUrl ? (openBlock(), createBlock(unref(ScalarDropdownButton), {
								key: 2,
								ref_key: "menuRef",
								ref: menuRef,
								class: "flex items-center gap-2",
								onClick: toggleWatchMode
							}, {
								default: withCtx(() => [createVNode(unref(ScalarIcon), {
									class: "inline-flex",
									icon: __props.menuItem.item?.watchMode ? "Unwatch" : "Watch",
									size: "md",
									thickness: "1.5"
								}, null, 8, ["icon"]), createElementVNode("span", null, toDisplayString(__props.menuItem.item?.watchMode ? "Disable Watch Mode" : "Enable Watch Mode"), 1)]),
								_: 1
							}, 512)) : createCommentVNode("", true),
							!isDraftsMenuItem.value ? (openBlock(), createBlock(unref(ScalarDropdownButton), {
								key: 3,
								class: "flex items-center gap-2",
								onClick: _cache[1] || (_cache[1] = ($event) => unref(deleteModal).show())
							}, {
								default: withCtx(() => [createVNode(unref(ScalarIcon), {
									class: "inline-flex",
									icon: "Delete",
									size: "md",
									thickness: "1.5"
								}), _cache[9] || (_cache[9] = createElementVNode("span", null, "Delete", -1))]),
								_: 1
							})) : createCommentVNode("", true),
							isDraftsMenuItem.value ? (openBlock(), createBlock(unref(ScalarDropdownButton), {
								key: 4,
								class: "flex items-center gap-2",
								onClick: _cache[2] || (_cache[2] = ($event) => unref(clearDraftsModal).show())
							}, {
								default: withCtx(() => [createVNode(unref(ScalarIcon), {
									class: "inline-flex",
									icon: "Delete",
									size: "md",
									thickness: "1.5"
								}), _cache[10] || (_cache[10] = createElementVNode("span", null, "Clear Drafts", -1))]),
								_: 1
							})) : createCommentVNode("", true)
						]),
						_: 1
					})]),
					_: 1
				}, 8, ["target"])) : createCommentVNode("", true),
				createVNode(unref(ScalarModal), {
					size: "xxs",
					state: unref(deleteModal),
					title: `Delete ${__props.menuItem.item?.resourceTitle}`
				}, {
					default: withCtx(() => [createVNode(DeleteSidebarListElement_default, {
						variableName: __props.menuItem.item?.title ?? "",
						warningMessage: __props.menuItem.item?.warning,
						onClose: _cache[4] || (_cache[4] = ($event) => unref(deleteModal).hide()),
						onDelete: handleItemDelete
					}, null, 8, ["variableName", "warningMessage"])]),
					_: 1
				}, 8, ["state", "title"]),
				createVNode(unref(ScalarModal), {
					size: "xxs",
					state: unref(editModal),
					title: `Edit ${__props.menuItem.item?.resourceTitle}`
				}, {
					default: withCtx(() => [__props.menuItem.item?.resourceTitle === "Collection" ? (openBlock(), createBlock(EditSidebarListCollection_default, {
						key: 0,
						icon: __props.menuItem.item?.icon || "interface-content-folder",
						name: __props.menuItem.item?.title,
						onClose: _cache[5] || (_cache[5] = ($event) => unref(editModal).hide()),
						onEdit: handleEdit
					}, null, 8, ["icon", "name"])) : (openBlock(), createBlock(EditSidebarListElement_default, {
						key: 1,
						name: __props.menuItem.item?.title ?? "",
						onClose: _cache[6] || (_cache[6] = ($event) => unref(editModal).hide()),
						onEdit: handleEdit
					}, null, 8, ["name"]))]),
					_: 1
				}, 8, ["state", "title"]),
				createVNode(unref(ScalarModal), {
					size: "xxs",
					state: unref(clearDraftsModal),
					title: "Clear Drafts"
				}, {
					default: withCtx(() => [createVNode(DeleteSidebarListElement_default, {
						variableName: "All Drafts",
						warningMessage: "This action will clear all drafts. This cannot be undone.",
						onClose: _cache[7] || (_cache[7] = ($event) => unref(clearDraftsModal).hide()),
						onDelete: handleClearDrafts
					})]),
					_: 1
				}, 8, ["state"])
			], 64);
		};
	}
});
//#endregion
export { RequestSidebarItemMenu_vue_vue_type_script_setup_true_lang_default as default };

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