import { computed as v } from "vue";
import { NavigationFailureType as k } from "vue-router";
import { initializeWorkspaceEventHandlers as E } from "../../workspace-events.js";
function R({
  eventBus: u,
  store: f,
  router: c,
  rebuildSidebar: i,
  navigateToCurrentTab: o,
  onSelectSidebarItem: h,
  onAfterExampleCreation: r,
  onCopyTabUrl: x,
  onToggleSidebar: d,
  closeSidebar: l,
  renameWorkspace: w
}) {
  const s = v(() => c.currentRoute?.value), p = ({
    documentName: e,
    path: m,
    method: t,
    exampleName: a
  }) => !(e !== void 0 && e !== s.value?.params.documentSlug || m !== void 0 && encodeURIComponent(m) !== s.value?.params.pathEncoded || t !== void 0 && t !== s.value?.params.method || a !== void 0 && a !== s.value?.params.exampleName);
  E({
    eventBus: u,
    store: f,
    hooks: {
      //------------------------------------------------------------------------------------
      // Document Related Hooks
      //------------------------------------------------------------------------------------
      "document:delete:document": {
        onAfterExecute: async (e) => {
          s?.value?.params.documentSlug === e.name && await c.push({
            name: "workspace.environment"
          });
        }
      },
      //------------------------------------------------------------------------------------
      // Operation Related Hooks
      //------------------------------------------------------------------------------------
      "operation:update:pathMethod": {
        onBeforeExecute: (e) => ({
          ...e,
          callback: async (m) => {
            m === "success" && (await c.replace({
              name: "example",
              params: {
                method: e.payload.method,
                pathEncoded: encodeURIComponent(e.payload.path),
                exampleName: s.value?.params.exampleName
              }
            }), i(f.value?.workspace.activeDocument?.["x-scalar-navigation"]?.name)), e.callback(m);
          }
        })
      },
      "operation:upsert:parameter": {
        onAfterExecute: (e) => r(e.meta)
      },
      "operation:update:extra-parameters": {
        onAfterExecute: (e) => r(e.meta)
      },
      "operation:reload:history": {
        onAfterExecute: (e) => r({ ...e.meta, exampleKey: "draft" })
      },
      "operation:delete:operation": {
        onAfterExecute: async (e) => {
          i(e.documentName);
          const {
            documentName: m,
            meta: { path: t, method: a }
          } = e;
          p({
            documentName: m,
            path: t,
            method: a
          }) && await c.replace({
            name: "document.overview",
            params: {
              documentSlug: m
            }
          });
        }
      },
      "operation:create:draft-example": {
        onAfterExecute: async (e) => {
          r({ ...e.meta, exampleKey: e.exampleName }), await c.push({
            name: "example",
            params: {
              documentSlug: e.documentName,
              pathEncoded: encodeURIComponent(e.meta.path),
              method: e.meta.method,
              exampleName: e.exampleName
            }
          });
        }
      },
      "operation:delete:example": {
        onAfterExecute: async (e) => {
          i(e.documentName);
          const {
            documentName: m,
            meta: { path: t, method: a, exampleKey: n }
          } = e;
          p({
            documentName: m,
            path: t,
            method: a,
            exampleName: n
          }) && await c.replace({
            name: "example",
            params: {
              pathEncoded: encodeURIComponent(t),
              method: a,
              documentSlug: m,
              exampleName: "default"
            }
          });
        }
      },
      "operation:rename:example": {
        onAfterExecute: async ({ meta: e, payload: m, documentName: t }) => {
          r({ ...e, exampleKey: m.name, documentName: t }), p({ documentName: t, path: e.path, method: e.method, exampleName: e.exampleKey }) && await c.replace({
            name: "example",
            params: {
              documentSlug: t,
              pathEncoded: encodeURIComponent(e.path),
              method: e.method,
              exampleName: m.name
            }
          });
        }
      },
      //------------------------------------------------------------------------------------
      // Operation Request Body Related Hooks
      //------------------------------------------------------------------------------------
      "operation:update:requestBody:value": {
        onAfterExecute: (e) => r(e.meta)
      },
      "operation:update:requestBody:formValue": {
        onAfterExecute: (e) => r(e.meta)
      },
      //------------------------------------------------------------------------------------
      // Tag Related Event Hooks
      //------------------------------------------------------------------------------------
      "tag:create:tag": {
        onAfterExecute: (e) => i(e.documentName)
      },
      "tag:edit:tag": {
        onAfterExecute: async (e) => {
          i(e.documentName), e.tag.children?.some(
            (t) => t.type === "operation" && p({
              documentName: e.documentName,
              path: t.path,
              method: t.method
            })
          ) && await c.replace({ ...s.value });
        }
      },
      "tag:delete:tag": {
        onAfterExecute: (e) => i(e.documentName)
      },
      //------------------------------------------------------------------------------------
      // Tabs Related Event Hooks
      //------------------------------------------------------------------------------------
      "tabs:add:tab": {
        onAfterExecute: o
      },
      "tabs:close:tab": {
        onAfterExecute: o
      },
      "tabs:focus:tab": {
        onAfterExecute: o
      },
      "tabs:focus:tab-last": {
        onAfterExecute: o
      },
      "tabs:navigate:previous": {
        onAfterExecute: o
      },
      "tabs:navigate:next": {
        onAfterExecute: o
      },
      "tabs:update:tabs": {
        onAfterExecute: o
      }
    }
  }), u.on("workspace:update:name", (e) => w(e)), u.on("scroll-to:nav-item", ({ id: e }) => h(e)), u.on("ui:toggle:sidebar", d), u.on("ui:navigate", async (e) => {
    const { replace: m = !1 } = e, t = m ? c.replace : c.push, a = (n) => {
      if (!n)
        return l(), e.callback?.("success");
      const g = k.duplicated;
      return n.type !== g ? e.callback?.("error") : e.callback?.("success");
    };
    if (e.page === "document") {
      const n = {
        documentSlug: e.documentSlug,
        workspaceSlug: e.workspaceSlug,
        namespace: e.namespace
      };
      if (e.path === "overview")
        return a(await t({ name: "document.overview", params: n }));
      if (e.path === "servers")
        return a(await t({ name: "document.servers", params: n }));
      if (e.path === "environment")
        return a(await t({ name: "document.environment", params: n }));
      if (e.path === "authentication")
        return a(await t({ name: "document.authentication", params: n }));
      if (e.path === "cookies")
        return a(await t({ name: "document.cookies", params: n }));
      if (e.path === "settings")
        return a(await t({ name: "document.settings", params: n }));
    }
    if (e.page === "workspace") {
      const n = { workspaceSlug: e.workspaceSlug, namespace: e.namespace };
      if (e.path === "environment")
        return a(await t({ name: "workspace.environment", params: n }));
      if (e.path === "cookies")
        return a(await t({ name: "workspace.cookies", params: n }));
      if (e.path === "settings")
        return a(await t({ name: "workspace.settings", params: n }));
    }
    if (e.page === "example") {
      const n = {
        namespace: e.namespace,
        workspaceSlug: e.workspaceSlug,
        documentSlug: e.documentSlug,
        pathEncoded: encodeURIComponent(e.path),
        method: e.method,
        exampleName: e.exampleName
      };
      return a(await t({ name: "example", params: n }));
    }
    if (e.page === "operation") {
      const n = {
        namespace: e.namespace,
        workspaceSlug: e.workspaceSlug,
        documentSlug: e.documentSlug,
        pathEncoded: encodeURIComponent(e.operationPath),
        method: e.method
      };
      if (e.path === "overview")
        return a(await t({ name: "operation.overview", params: n }));
      if (e.path === "servers")
        return a(await t({ name: "operation.servers", params: n }));
      if (e.path === "authentication")
        return a(await t({ name: "operation.authentication", params: n }));
    }
  }), u.on("tabs:copy:url", (e) => x(e.index));
}
export {
  R as initializeAppEventHandlers
};
