import { notFound } from "next/navigation";
import fs from "node:fs/promises";
import path from "node:path";

import Header from "@/components/Current/Header";

import type { Params, SearchParams } from "@/types/params";

export default async function CurrentContentPage({
  params,
  searchParams,
}: Params<SearchParams>) {
  try {
    const filePath = path.join(
      process.cwd(),
      "mockCms",
      params.lang,
      searchParams.uri,
      "data.json"
    );

    const data = await fs.readFile(filePath, { encoding: "utf-8" });

    if (!data) {
      throw new Error("No data");
    }

    const json = JSON.parse(data);

    return (
      <>
        <Header lang={params.lang} pathname={searchParams.uri} />
        {json.hero ? (
          <div
            dangerouslySetInnerHTML={{ __html: json.hero }}
            className="hero-content-overlay hero-content-widget"
          />
        ) : null}
        {json.content ? (
          <main
            className="main l-sections-wrapper"
            role="main"
            id="maincontent"
            dangerouslySetInnerHTML={{ __html: json.content }}
          />
        ) : null}
      </>
    );
  } catch (err) {
    return notFound();
  }
}
