import MailOutlineIcon from "@mui/icons-material/MailOutline";

import { OpenAPI } from "openapi-types";

import RotateLeftIcon from "../../assets/symbols/RotateLeft";
import ShoppingBagIcon from "../../assets/symbols/ShoppingBag";
import StorefrontIcon from "../../assets/symbols/Storefront";
import { RouterExtension } from "../../router/Router";
import { ContribComponentMap, NavigationOverrides, PageComponent } from "../../types";
export * from "./types/order";
export * from "./types/recipient";
export * from "./types/shipment";

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;
    }
  >
> = {
  order: {
    list: {
      page: async () => (await import("./pages/order/list")).default,
      request: {
        query: {
          has_shipment: "false",
          destination_type: "STORE",
        },
      },
      defaultRequest: {
        query: {
          state: "PENDING",
        },
      },
    },
  },
} as const;

export const router: RouterExtension = {
  app: "fulfillment",
  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 = {
  "fulfillment.order:list": {
    icon: StorefrontIcon,
    title: "Pickup",
    permission: "fulfillment.view_order",
  },
} as const;

export const contrib: Record<string, ContribComponentMap> = {
  orders: {
    "pos:purchase:detail:orders": {
      title: "Order",
      icon: ShoppingBagIcon,
      component: async () => (await import("./components/contrib/PurchaseOrderList")).default,
      variant: "tab",
      permission: "fulfillment.view_order",
    },
  },
  returns: {
    "pos:purchase:detail:returns": {
      title: "Return",
      icon: RotateLeftIcon,
      component: async () => (await import("./components/contrib/ReturnProspectList")).default,
      variant: "tab",
      permission: "fulfillment.view_order",
    },
  },
  actions: {
    "pos:purchase:detail:send-confirmation-email": {
      title: "Send Purchase Receipt",
      variant: "action",
      icon: MailOutlineIcon,
    },
  },
} as const;
