import { SearchParams } from "@/app/utils/fetchUtil";
import { I18nProvider, getServerI18n } from "@/i18n";
import { Typography } from "@mui/material";
import isString from "lodash/isString";
import { notFound } from "next/navigation";
import { Suspense } from "react";
import EditPrinter from "@/components/EditPrinter";
import { fetchPrinterDetails } from "@/app/api/settings/printer/actions";

type Props = {} & SearchParams;

const EditPrinterPage: React.FC<Props> = async ({ searchParams }) => {
  const { t } = await getServerI18n("common");
  const id = isString(searchParams["id"])
    ? parseInt(searchParams["id"])
    : undefined;
  if (!id) {
    notFound();
  }

  const printer = await fetchPrinterDetails(id);
  if (!printer) {
    notFound();
  }

  return (
    <>
      <Typography variant="h4">{t("Edit")} {t("Printer")}</Typography>
      <I18nProvider namespaces={["common"]}>
        <Suspense fallback={<div>Loading...</div>}>
          <EditPrinter printer={printer} />
        </Suspense>
      </I18nProvider>
    </>
  );
};

export default EditPrinterPage;
