import StorefrontIcon from "@mui/icons-material/Storefront";

import { OpenAPI } from "openapi-types";

import WorkspacesIcon from "../../assets/symbols/Workspaces";
import { RouterExtension } from "../../router/Router";
import { ContribComponentMap, NavigationOverrides, PageComponent } from "../../types";

export * from "./types/article";
export * from "./types/contrib";

const routes: Record<
  string,
  Record<
    string,
    {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      page: () => PageComponent<any> | Promise<PageComponent<any>>;
      request?: OpenAPI.Request;
      defaultRequest?: OpenAPI.Request;
      offline?: boolean;
    }
  >
> = {
  article: {
    create: { page: async () => (await import("./pages/article/create")).default, offline: true },
    detail: { page: async () => (await import("./pages/article/detail")).default },
    list: { page: async () => (await import("./pages/article/list")).default },
  },
};

export const router: RouterExtension = {
  app: "catalog",
  pages: (route) => {
    const { page, ...hit } = routes[route.view]?.[route.action] ?? {};

    if (page != null) {
      return {
        page: page(),
        ...hit,
      };
    }

    return undefined;
  },
};

export const navigation: NavigationOverrides = {
  "catalog.article:list": {
    icon: WorkspacesIcon,
    permission: "catalog.view_article",
  },
} as const;

export const contrib: Record<string, ContribComponentMap> = {
  pos: {
    "catalog:article:detail:items": {
      title: "Availability",
      icon: StorefrontIcon,
      component: async () => (await import("./contrib/SiteAvailability")).default,
      variant: "tab",
      permission: "catalog.view_article",
    },
  },
  pim: {
    "pim:product:detail:articles": {
      title: "Product",
      icon: StorefrontIcon,
      component: async () => (await import("./contrib/ProductArticles")).default,
      variant: "sidebar",
      permission: "catalog.view_article",
    },
  },
} as const;
