import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import Sidebar_default from "../../components/Sidebar/Sidebar.vue.js";
import ViewLayout_default from "../../components/ViewLayout/ViewLayout.vue.js";
import ViewLayoutContent_default from "../../components/ViewLayout/ViewLayoutContent.vue.js";
import ViewLayoutSection_default from "../../components/ViewLayout/ViewLayoutSection.vue.js";
import EmptyState_default from "../../components/EmptyState.vue.js";
import SidebarButton_default from "../../components/Sidebar/SidebarButton.vue.js";
import SidebarList_default from "../../components/Sidebar/SidebarList.vue.js";
import SidebarListElement_default from "../../components/Sidebar/SidebarListElement.vue.js";
import CookieForm_default from "./CookieForm.vue.js";
import CookieModal_default from "./CookieModal.vue.js";
import { Fragment, computed, createBlock, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, onBeforeUnmount, onMounted, openBlock, renderList, unref, watch, withCtx, withModifiers } from "vue";
import { useModal } from "@scalar/components";
import { useRoute, useRouter } from "vue-router";
import { cookieSchema } from "@scalar/oas-utils/entities/cookie";
//#region src/views/Cookies/Cookies.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex-1" };
var _hoisted_2 = { class: "relative mb-[.5px] last:mb-0" };
var Cookies_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "Cookies",
	setup(__props) {
		const { cookies, cookieMutators, events, workspaceMutators } = useWorkspace();
		const { activeWorkspace, activeCookieId } = useActiveEntities();
		const router = useRouter();
		const route = useRoute();
		const cookieModal = useModal();
		const addCookieHandler = (cookieData) => {
			const cookie = cookieSchema.parse({
				name: cookieData.name,
				value: cookieData.value,
				domain: cookieData.domain,
				path: "/"
			});
			cookieMutators.add(cookie);
			workspaceMutators.edit(activeWorkspace.value?.uid, "cookies", [...activeWorkspace.value?.cookies ?? [], cookie.uid]);
			router.push({
				name: "cookies",
				params: { cookies: cookie.uid }
			});
		};
		const removeCookie = (uid) => {
			cookieMutators.delete(uid);
			workspaceMutators.edit(activeWorkspace.value?.uid, "cookies", [...(activeWorkspace.value?.cookies ?? []).filter((c) => c !== uid)]);
			const remainingCookies = Object.values(cookies).filter((cookie) => cookie.uid !== uid);
			if (remainingCookies.length > 0) {
				const lastCookie = remainingCookies[remainingCookies.length - 1];
				if (lastCookie) router.push(lastCookie.uid);
			} else router.push({
				name: "cookies",
				params: { [PathId.Cookies]: "default" }
			});
		};
		const openCookieModal = () => {
			cookieModal.show();
		};
		const handleHotKey = (event) => {
			if (event?.createNew && route.name === "cookies") openCookieModal();
		};
		/**
		* Navigate to specific cookies
		*/
		const handleNavigation = (event, uid) => {
			const to = {
				name: "cookies",
				params: {
					workspace: activeWorkspace.value?.uid ?? "default",
					cookies: uid
				}
			};
			if (event.metaKey) {
				const path = router.resolve(to).href;
				window.open(path, "_blank");
				return;
			}
			router.push(to);
		};
		/** Bind keyboard shortcuts */
		onMounted(() => events.hotKeys.on(handleHotKey));
		/** Unbind keyboard shortcuts */
		onBeforeUnmount(() => events.hotKeys.off(handleHotKey));
		const activeCookie = computed(() => cookies[activeCookieId.value]);
		const hasCookies = computed(() => Object.keys(cookies).length > 0 && activeCookie.value);
		watch(() => route.query.openCookieModal, (newVal) => {
			if (newVal === "true") openCookieModal();
		}, { immediate: true });
		return (_ctx, _cache) => {
			return openBlock(), createBlock(ViewLayout_default, null, {
				default: withCtx(() => [
					createVNode(unref(Sidebar_default), { title: "Cookies" }, {
						content: withCtx(() => [createElementVNode("div", _hoisted_1, [createVNode(SidebarList_default, null, {
							default: withCtx(() => [(openBlock(true), createElementBlock(Fragment, null, renderList(Object.values(unref(cookies)), (cookie) => {
								return openBlock(), createElementBlock("li", {
									key: cookie.uid,
									class: "gap-1/2 flex flex-col"
								}, [createElementVNode("div", _hoisted_2, [(openBlock(), createBlock(SidebarListElement_default, {
									key: cookie.uid,
									class: "text-xs",
									isDeletable: "",
									to: {
										name: "cookies",
										params: { [unref(PathId).Cookies]: cookie.uid }
									},
									type: "cookies",
									variable: {
										name: cookie.name,
										uid: cookie.uid
									},
									warningMessage: `Are you sure you want to delete this cookie?`,
									onClick: withModifiers(($event) => handleNavigation($event, cookie.uid), ["prevent"]),
									onDelete: ($event) => removeCookie(cookie.uid)
								}, null, 8, [
									"to",
									"variable",
									"onClick",
									"onDelete"
								]))])]);
							}), 128))]),
							_: 1
						})])]),
						button: withCtx(() => [createVNode(SidebarButton_default, {
							click: openCookieModal,
							hotkey: "N"
						}, {
							title: withCtx(() => [..._cache[1] || (_cache[1] = [createTextVNode(" Add Cookie ", -1)])]),
							_: 1
						})]),
						_: 1
					}),
					createVNode(ViewLayoutContent_default, { class: "flex-1" }, {
						default: withCtx(() => [hasCookies.value ? (openBlock(), createBlock(ViewLayoutSection_default, {
							key: 0,
							class: "*:border-b-0"
						}, {
							title: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode("Edit Cookie", -1)])]),
							default: withCtx(() => [createVNode(CookieForm_default)]),
							_: 1
						})) : (openBlock(), createBlock(EmptyState_default, { key: 1 }))]),
						_: 1
					}),
					createVNode(CookieModal_default, {
						state: unref(cookieModal),
						onCancel: _cache[0] || (_cache[0] = ($event) => unref(cookieModal).hide()),
						onSubmit: addCookieHandler
					}, null, 8, ["state"])
				]),
				_: 1
			});
		};
	}
});
//#endregion
export { Cookies_vue_vue_type_script_setup_true_lang_default as default };

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