import SellOutlinedIcon from "@mui/icons-material/SellOutlined";

import { OpenAPI } from "openapi-types";

import FormatListNumberedRtlIcon from "../../assets/symbols/FormatListNumberedRtl";
import PaymentsIcon from "../../assets/symbols/Payments";
import { RouterExtension } from "../../router/Router";
import { ContribComponentMap, NavigationOverrides, PageComponent } from "../../types";

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

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

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

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

    return undefined;
  },
};

export const navigation: NavigationOverrides = {
  "pricing.price_list:list": {
    icon: SellOutlinedIcon,
    permission: "pricing.view_pricelist",
  },
} as const;

export const contrib: Record<string, ContribComponentMap> = {
  prices: {
    "pricing:price_list:detail:prices": {
      title: "Prices",
      icon: FormatListNumberedRtlIcon,
      component: async () => (await import("./contrib/PriceListPrices")).default,
      variant: "inline",
      permission: "pricing.view_price",
    },
  },
  catalog: {
    "catalog:article:detail:pricing": {
      title: "Prices",
      icon: PaymentsIcon,
      component: async () => (await import("./contrib/ArticlePricing")).default,
      variant: "inline",
      permission: "pricing.view_pricelist",
    },
  },
} as const;
