import { redirect } from "next/navigation";

import {
  MultiPageNavigationProvider,
  ProgressLayout,
} from "@/components/domains/additional-purchases";
import { loadFunnelPaymentById } from "@/entities/session/funnel/loaders";
import { ROUTES } from "@/shared/constants/client-routes";
import { ELocalesPlacement } from "@/types";

interface LayoutProps {
  children: React.ReactNode;
  params: Promise<{ pageType: string }>;
}

const payload = {
  funnel: ELocalesPlacement.CompatibilityV2,
};

export default async function MultiPageLayout({
  children,
  params,
}: LayoutProps) {
  const { pageType } = await params;
  const pages = await loadFunnelPaymentById(payload, "additionalProducts");

  const allProducts = Array.isArray(pages) ? pages : pages ? [pages] : [];
  const currentProduct = allProducts.find(page => page.type === pageType);

  if (!currentProduct) {
    return redirect(ROUTES.home());
  }

  return (
    <MultiPageNavigationProvider data={allProducts} currentType={pageType}>
      <ProgressLayout>{children}</ProgressLayout>
    </MultiPageNavigationProvider>
  );
}
