import { queryAnnouncement } from '@/services/user/announcement';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@workspace/ui/components/dialog';
import { Markdown } from '@workspace/ui/custom-components/markdown';
import { getTranslations } from 'next-intl/server';

export default async function Announcement({
  type,
  Authorization,
}: {
  type: 'popup' | 'pinned';
  Authorization?: string;
}) {
  let data;
  try {
    data = await queryAnnouncement(
      {
        page: 1,
        size: 10,
        pinned: type === 'pinned',
        popup: type === 'popup',
      },
      {
        skipErrorHandler: true,
        Authorization,
      },
    ).then((res) => {
      return res.data.data?.announcements.find((item) => item[type]);
    });
  } catch (error) {
    /* empty */
  }
  if (!data) return null;

  const t = await getTranslations('dashboard');

  if (type === 'popup') {
    return (
      <Dialog defaultOpen={!!data}>
        <DialogContent className='sm:max-w-[425px]'>
          <DialogHeader>
            <DialogTitle>{data?.title}</DialogTitle>
          </DialogHeader>
          <Markdown>{data?.content}</Markdown>
        </DialogContent>
      </Dialog>
    );
  }
}
