import InventoryOutlinedIcon from "@mui/icons-material/InventoryOutlined";

import { OpenAPI } from "openapi-types";

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

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

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;
    }
  >
> = {
  product: {
    create: { page: async () => (await import("./pages/product/create")).default, offline: true },
    detail: { page: async () => (await import("./pages/product/detail")).default },
    list: { page: async () => (await import("./pages/product/list")).default },
  },
};

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

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

    return undefined;
  },
};

export const navigation: NavigationOverrides = {
  "pim.product:list": {
    icon: WorkspacesIcon,
    permission: "pim.view_product",
  },
} as const;

export const contrib: Record<string, ContribComponentMap> = {
  catalog: {
    "catalog:article:detail:product": {
      title: "Product",
      icon: InventoryOutlinedIcon,
      component: async () => (await import("./contrib/ArticleProduct")).default,
      predicate: (article: ArticleDetail) => !!article.product_number,
      variant: "inline",
      permission: "pim.view_product",
    },
  },
} as const;
