import { initializeWorkspaceEventHandlers } from "../../workspace-events.js";
import { computed } from "vue";
import { NavigationFailureType } from "vue-router";
//#region src/v2/features/app/app-events.ts
function initializeAppEventHandlers({ eventBus, store, router, rebuildSidebar, navigateToCurrentTab, onSelectSidebarItem, onAfterExampleCreation, onCopyTabUrl, onToggleSidebar, closeSidebar, renameWorkspace }) {
	const currentRoute = computed(() => router.currentRoute?.value);
	/**
	* Checks if the current route params match the specified operation meta.
	* Useful for determining if the sidebar or UI needs to be updated after changes to operations/examples.
	*
	* NOTE: It may be beneficial to compare to the active state instead of the router
	*/
	const isRouteParamsMatch = ({ documentName, path, method, exampleName }) => {
		if (documentName !== void 0 && documentName !== currentRoute.value?.params.documentSlug) return false;
		if (path !== void 0 && encodeURIComponent(path) !== currentRoute.value?.params.pathEncoded) return false;
		if (method !== void 0 && method !== currentRoute.value?.params.method) return false;
		if (exampleName !== void 0 && exampleName !== currentRoute.value?.params.exampleName) return false;
		return true;
	};
	initializeWorkspaceEventHandlers({
		eventBus,
		store,
		hooks: {
			"document:delete:document": { onAfterExecute: async (payload) => {
				if (currentRoute?.value?.params.documentSlug === payload.name) await router.push({ name: "workspace.environment" });
			} },
			"operation:update:pathMethod": { onBeforeExecute: (payload) => ({
				...payload,
				callback: async (status) => {
					if (status === "success") {
						rebuildSidebar(store.value?.workspace.activeDocument?.["x-scalar-navigation"]?.name);
						await router.replace({
							name: "example",
							params: {
								method: payload.payload.method,
								pathEncoded: encodeURIComponent(payload.payload.path),
								exampleName: currentRoute.value?.params.exampleName
							}
						});
					}
					payload.callback(status);
				}
			}) },
			"operation:upsert:parameter": { onAfterExecute: (payload) => onAfterExampleCreation(payload.meta) },
			"operation:update:extra-parameters": { onAfterExecute: (payload) => onAfterExampleCreation(payload.meta) },
			"operation:reload:history": { onAfterExecute: (payload) => onAfterExampleCreation({
				...payload.meta,
				exampleKey: "draft"
			}) },
			"operation:delete:operation": { onAfterExecute: async (payload) => {
				rebuildSidebar(payload.documentName);
				const { documentName, meta: { path, method } } = payload;
				if (isRouteParamsMatch({
					documentName,
					path,
					method
				})) await router.replace({
					name: "document.overview",
					params: { documentSlug: documentName }
				});
			} },
			"operation:create:draft-example": { onAfterExecute: async (payload) => {
				onAfterExampleCreation({
					...payload.meta,
					exampleKey: payload.exampleName
				});
				await router.push({
					name: "example",
					params: {
						documentSlug: payload.documentName,
						pathEncoded: encodeURIComponent(payload.meta.path),
						method: payload.meta.method,
						exampleName: payload.exampleName
					}
				});
			} },
			"operation:delete:example": { onAfterExecute: async (payload) => {
				rebuildSidebar(payload.documentName);
				const { documentName, meta: { path, method, exampleKey } } = payload;
				if (isRouteParamsMatch({
					documentName,
					path,
					method,
					exampleName: exampleKey
				})) await router.replace({
					name: "example",
					params: {
						pathEncoded: encodeURIComponent(path),
						method,
						documentSlug: documentName,
						exampleName: "default"
					}
				});
			} },
			"operation:rename:example": { onAfterExecute: async ({ meta, payload, documentName }) => {
				onAfterExampleCreation({
					...meta,
					exampleKey: payload.name,
					documentName
				});
				if (isRouteParamsMatch({
					documentName,
					path: meta.path,
					method: meta.method,
					exampleName: meta.exampleKey
				})) await router.replace({
					name: "example",
					params: {
						documentSlug: documentName,
						pathEncoded: encodeURIComponent(meta.path),
						method: meta.method,
						exampleName: payload.name
					}
				});
			} },
			"operation:update:requestBody:value": { onAfterExecute: (payload) => onAfterExampleCreation(payload.meta) },
			"operation:update:requestBody:formValue": { onAfterExecute: (payload) => onAfterExampleCreation(payload.meta) },
			"tag:create:tag": { onAfterExecute: (payload) => rebuildSidebar(payload.documentName) },
			"tag:edit:tag": { onAfterExecute: async (payload) => {
				rebuildSidebar(payload.documentName);
				if (payload.tag.children?.some((child) => child.type === "operation" && isRouteParamsMatch({
					documentName: payload.documentName,
					path: child.path,
					method: child.method
				}))) await router.replace({ ...currentRoute.value });
			} },
			"tag:delete:tag": { onAfterExecute: (payload) => rebuildSidebar(payload.documentName) },
			"tabs:add:tab": { onAfterExecute: navigateToCurrentTab },
			"tabs:close:tab": { onAfterExecute: navigateToCurrentTab },
			"tabs:focus:tab": { onAfterExecute: navigateToCurrentTab },
			"tabs:focus:tab-last": { onAfterExecute: navigateToCurrentTab },
			"tabs:navigate:previous": { onAfterExecute: navigateToCurrentTab },
			"tabs:navigate:next": { onAfterExecute: navigateToCurrentTab },
			"tabs:update:tabs": { onAfterExecute: navigateToCurrentTab }
		}
	});
	eventBus.on("workspace:update:name", (payload) => renameWorkspace(payload));
	eventBus.on("scroll-to:nav-item", ({ id }) => onSelectSidebarItem(id));
	eventBus.on("ui:toggle:sidebar", onToggleSidebar);
	/**
	* Bind the inernal navigation to a public api
	*/
	eventBus.on("ui:navigate", async (payload) => {
		const { replace = false } = payload;
		const fn = replace ? router.replace : router.push;
		const execCallback = (result) => {
			if (!result) {
				closeSidebar();
				return payload.callback?.("success");
			}
			const navigationFailure = NavigationFailureType.duplicated;
			if (result.type !== navigationFailure) return payload.callback?.("error");
			return payload.callback?.("success");
		};
		if (payload.page === "document") {
			const params = {
				documentSlug: payload.documentSlug,
				workspaceSlug: payload.workspaceSlug,
				namespace: payload.namespace
			};
			if (payload.path === "overview") return execCallback(await fn({
				name: "document.overview",
				params
			}));
			if (payload.path === "servers") return execCallback(await fn({
				name: "document.servers",
				params
			}));
			if (payload.path === "environment") return execCallback(await fn({
				name: "document.environment",
				params
			}));
			if (payload.path === "authentication") return execCallback(await fn({
				name: "document.authentication",
				params
			}));
			if (payload.path === "cookies") return execCallback(await fn({
				name: "document.cookies",
				params
			}));
			if (payload.path === "settings") return execCallback(await fn({
				name: "document.settings",
				params
			}));
		}
		if (payload.page === "workspace") {
			const params = {
				workspaceSlug: payload.workspaceSlug,
				namespace: payload.namespace
			};
			if (payload.path === "environment") return execCallback(await fn({
				name: "workspace.environment",
				params
			}));
			if (payload.path === "cookies") return execCallback(await fn({
				name: "workspace.cookies",
				params
			}));
			if (payload.path === "settings") return execCallback(await fn({
				name: "workspace.settings",
				params
			}));
		}
		if (payload.page === "example") return execCallback(await fn({
			name: "example",
			params: {
				namespace: payload.namespace,
				workspaceSlug: payload.workspaceSlug,
				documentSlug: payload.documentSlug,
				pathEncoded: encodeURIComponent(payload.path),
				method: payload.method,
				exampleName: payload.exampleName
			}
		}));
		if (payload.page === "operation") {
			const params = {
				namespace: payload.namespace,
				workspaceSlug: payload.workspaceSlug,
				documentSlug: payload.documentSlug,
				pathEncoded: encodeURIComponent(payload.operationPath),
				method: payload.method
			};
			if (payload.path === "overview") return execCallback(await fn({
				name: "operation.overview",
				params
			}));
			if (payload.path === "servers") return execCallback(await fn({
				name: "operation.servers",
				params
			}));
			if (payload.path === "authentication") return execCallback(await fn({
				name: "operation.authentication",
				params
			}));
		}
	});
	eventBus.on("tabs:copy:url", (payload) => onCopyTabUrl(payload.index));
}
//#endregion
export { initializeAppEventHandlers };

//# sourceMappingURL=app-events.js.map