import { useActiveEntities } from "../../../store/active-entities.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 { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, openBlock, ref, renderList, toDisplayString, unref, withCtx, withModifiers } from "vue";
import { ScalarButton, ScalarDropdown, ScalarDropdownDivider, ScalarDropdownItem, ScalarIcon, ScalarListboxCheckbox, ScalarModal, useModal } from "@scalar/components";
import { useRouter } from "vue-router";
//#region src/views/Request/components/WorkspaceDropdown.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex w-[inherit] items-center text-base" };
var _hoisted_2 = { class: "m-0 flex items-center gap-1.5 font-bold" };
var _hoisted_3 = { class: "line-clamp-1 text-left" };
var _hoisted_4 = { class: "overflow-hidden text-ellipsis" };
var _hoisted_5 = { class: "flex h-4 w-4 items-center justify-center" };
var WorkspaceDropdown_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "WorkspaceDropdown",
	setup(__props) {
		const { activeWorkspace } = useActiveEntities();
		const { workspaces, workspaceMutators, events } = useWorkspace();
		const { push } = useRouter();
		const updateSelected = (uid) => {
			if (uid === activeWorkspace.value?.uid) return;
			push({
				name: "workspace",
				params: { workspace: uid }
			});
		};
		const isLastWorkspace = computed(() => Object.keys(workspaces).length === 1);
		const createNewWorkspace = () => events.commandPalette.emit({ commandName: "Create Workspace" });
		const tempName = ref("");
		const tempUid = ref("");
		const editModal = useModal();
		const deleteModal = useModal();
		const openRenameModal = (uid) => {
			const workspace = workspaces[uid];
			if (!workspace) return;
			tempName.value = workspace.name;
			tempUid.value = uid;
			editModal.show();
		};
		const handleWorkspaceEdit = (name) => {
			if (!name.trim()) return;
			workspaceMutators.edit(tempUid.value, "name", name.trim());
			editModal.hide();
		};
		const openDeleteModal = (uid) => {
			const workspace = workspaces[uid];
			if (!workspace) return;
			tempName.value = workspace.name;
			tempUid.value = uid;
			deleteModal.show();
		};
		const deleteWorkspace = async () => {
			if (!isLastWorkspace.value) {
				const deletedActiveWorkspace = activeWorkspace.value?.uid === tempUid.value;
				const currentWorkspaces = { ...workspaces };
				delete currentWorkspaces[tempUid.value];
				workspaceMutators.delete(tempUid.value);
				if (deletedActiveWorkspace) {
					const newWorkspaceUid = Object.keys(currentWorkspaces)[0];
					await push({
						name: "workspace",
						params: { workspace: newWorkspaceUid }
					});
				}
			}
			deleteModal.hide();
		};
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock("div", null, [
				createElementVNode("div", _hoisted_1, [createVNode(unref(ScalarDropdown), null, {
					items: withCtx(() => [
						(openBlock(true), createElementBlock(Fragment, null, renderList(unref(workspaces), (workspace, uid) => {
							return openBlock(), createBlock(unref(ScalarDropdownItem), {
								key: uid,
								class: "group/item flex w-full items-center gap-1.5 overflow-hidden text-ellipsis whitespace-nowrap",
								onClick: withModifiers(($event) => updateSelected(workspace.uid), ["stop"])
							}, {
								default: withCtx(() => [
									createVNode(unref(ScalarListboxCheckbox), { selected: unref(activeWorkspace)?.uid === uid }, null, 8, ["selected"]),
									createElementVNode("span", _hoisted_4, toDisplayString(workspace.name), 1),
									createVNode(unref(ScalarDropdown), {
										placement: "right-start",
										teleport: ""
									}, {
										items: withCtx(() => [createVNode(unref(ScalarDropdownItem), {
											class: "flex gap-2",
											onMousedown: ($event) => openRenameModal(workspace.uid),
											onTouchend: withModifiers(($event) => openRenameModal(workspace.uid), ["prevent"])
										}, {
											default: withCtx(() => [createVNode(unref(ScalarIcon), {
												class: "inline-flex",
												icon: "Edit",
												size: "md",
												thickness: "1.5"
											}), _cache[2] || (_cache[2] = createElementVNode("span", null, "Rename", -1))]),
											_: 1
										}, 8, ["onMousedown", "onTouchend"]), !isLastWorkspace.value ? (openBlock(), createBlock(unref(ScalarDropdownItem), {
											key: 0,
											class: "flex gap-2",
											onMousedown: withModifiers(($event) => openDeleteModal(workspace.uid), ["prevent"]),
											onTouchend: withModifiers(($event) => openDeleteModal(workspace.uid), ["prevent"])
										}, {
											default: withCtx(() => [createVNode(unref(ScalarIcon), {
												class: "inline-flex",
												icon: "Delete",
												size: "md",
												thickness: "1.5"
											}), _cache[3] || (_cache[3] = createElementVNode("span", null, "Delete", -1))]),
											_: 1
										}, 8, ["onMousedown", "onTouchend"])) : createCommentVNode("", true)]),
										default: withCtx(() => [createVNode(unref(ScalarButton), {
											class: "hover:bg-b-3 -mr-1 ml-auto aspect-square h-fit px-0.5 py-0 group-hover/item:flex",
											size: "sm",
											type: "button",
											variant: "ghost"
										}, {
											default: withCtx(() => [createVNode(unref(ScalarIcon), {
												icon: "Ellipses",
												size: "sm"
											})]),
											_: 1
										})]),
										_: 2
									}, 1024)
								]),
								_: 2
							}, 1032, ["onClick"]);
						}), 128)),
						createVNode(unref(ScalarDropdownDivider)),
						createVNode(unref(ScalarDropdownItem), {
							class: "flex items-center gap-1.5",
							onClick: createNewWorkspace
						}, {
							default: withCtx(() => [createElementVNode("div", _hoisted_5, [createVNode(unref(ScalarIcon), {
								icon: "Add",
								size: "sm"
							})]), _cache[4] || (_cache[4] = createElementVNode("span", null, "Create Workspace", -1))]),
							_: 1
						})
					]),
					default: withCtx(() => [createVNode(unref(ScalarButton), {
						class: "text-c-1 hover:bg-b-2 line-clamp-1 h-full w-fit justify-start px-1.5 py-1.5 font-normal",
						variant: "ghost"
					}, {
						default: withCtx(() => [createElementVNode("div", _hoisted_2, [createElementVNode("h2", _hoisted_3, toDisplayString(unref(activeWorkspace)?.name), 1)])]),
						_: 1
					})]),
					_: 1
				})]),
				createVNode(unref(ScalarModal), {
					size: "xxs",
					state: unref(deleteModal),
					title: "Delete workspace"
				}, {
					default: withCtx(() => [createVNode(DeleteSidebarListElement_default, {
						variableName: tempName.value,
						warningMessage: "This cannot be undone. You're about to delete the workspace and everything inside it.",
						onClose: _cache[0] || (_cache[0] = ($event) => unref(deleteModal).hide()),
						onDelete: deleteWorkspace
					}, null, 8, ["variableName"])]),
					_: 1
				}, 8, ["state"]),
				createVNode(unref(ScalarModal), {
					size: "xxs",
					state: unref(editModal),
					title: "Rename Workspace"
				}, {
					default: withCtx(() => [createVNode(EditSidebarListElement_default, {
						name: tempName.value,
						onClose: _cache[1] || (_cache[1] = ($event) => unref(editModal).hide()),
						onEdit: handleWorkspaceEdit
					}, null, 8, ["name"])]),
					_: 1
				}, 8, ["state"])
			]);
		};
	}
});
//#endregion
export { WorkspaceDropdown_vue_vue_type_script_setup_true_lang_default as default };

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