"use client";

import React from "react";
import useSWR from "swr";
import axios from "@/lib/axios";
import PageLoader from "@/components/ui/PageLoader";
import { useTranslations } from "next-intl";

import AdminLegalEditorClient from "../AdminLegalEditorClient";

export default function EditLegalPageClient({ id }: { id: string }) {
  const t = useTranslations("LegalAdmin");
  const { data, error, isLoading } = useSWR(
    id ? `/api/admin/legal-pages/${id}` : null,
    (url) => axios.get(url).then((res) => res.data)
  );

  if (!id) {
    return <div className="p-10 text-center text-red-500">{t("invalid_id")}</div>;
  }

  if (isLoading) {
      return <PageLoader text={t("loading_editor")} />;
  }

  if (error || !data) {
      return <div className="p-10 text-center text-red-500">{t("view_error")}</div>;
  }

  return <AdminLegalEditorClient mode="edit" initialData={data.data} />;
}
