import { OpenAPI } from "openapi-types";

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

export * from "./types/purchase";
export * from "./types/receipt";

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

export const router: RouterExtension = {
  app: "pos",
  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 = {
  "pos.purchase:list": {
    icon: ReceiptLongIcon,
    permission: "pos.view_purchase",
  },
} as const;

export const contrib: Record<string, ContribComponentMap> = {
  dashboard: {
    "dashboard:stats:purchase:count": {
      component: async () => (await import("./contrib/PurchaseCountWidget")).default,
      variant: "inline",
      permission: "pos.view_purchase",
    },
    "dashboard:stats:purchase:amount": {
      component: async () => (await import("./contrib/PurchaseAmountWidget")).default,
      variant: "inline",
      permission: "pos.view_purchase",
    },
    "dashboard:stats:payment-method:count": {
      component: async () => (await import("./contrib/PaymentMethodWidget")).default,
      variant: "inline",
      permission: "pos.view_purchase",
    },
  },
} as const;
