/* Copyright (C) 2024-2025 LibreWeddingPlanner contributors*/

'use client'

import { AbstractApi } from '@/app/api/abstract-api';
import { Website, WebsiteSerializer } from '@/app/lib/website';
import { useEffect, useState } from 'react';
import Tiptap from '../../../components/Tiptap';

export default function Page() {

  const [website, setWebsite] = useState<Website>()
  const api = new AbstractApi<Website>();
  const serializer = new WebsiteSerializer();

  useEffect(() => {
    api.get(serializer, undefined, (loadedWebsite) => {
      setWebsite(loadedWebsite);
      console.log('Website loaded:', loadedWebsite);
    });
  }, []);

  const updateWebsite = (newContent: string) => {
    api.update(serializer, new Website('', newContent), () => { });
  }

  return (
    <div className="border rounded-lg p-4">
      <Tiptap
        key={website?.content ?? 'empty'}
        content={website?.content || ''}
        onUpdate={updateWebsite}
      />
    </div>
  );
}