import { notFound } from "next/navigation";

import { CurrencyForm } from "../_components/currency-form/currency-form";
import { fetchBackendCurrencyById } from "../_lib/currencies-server-api";

type PageProps = {
  params: Promise<{ id: string }>;
};

export default async function CurrencyUpdatePage({ params }: PageProps) {
  const { id } = await params;
  const currencyId = Number(id);

  if (!Number.isFinite(currencyId)) {
    notFound();
  }

  const { currency } = await fetchBackendCurrencyById(currencyId);

  if (!currency) {
    notFound();
  }

  return <CurrencyForm mode="update" initial={currency} />;
}
