import { useState } from 'react';

import { Stack } from '@mui/material';
import { IconImageInPicture } from '@tabler/icons-react';

import ActionButton from '../../components/ActionButton';
import { useTextArea } from '../../context';

import EmbedHTMLModal from './modal';
import { type EmbedHTMLProps } from './types';

const EmbedHTML = ({ title, transformer }: EmbedHTMLProps) => {
  const { editor } = useTextArea();
  const [open, setOpen] = useState(false);

  if (!editor) return null;

  return (
    <Stack>
      <EmbedHTMLModal
        open={open}
        onClose={() => setOpen(false)}
        transformer={transformer}
      />
      <ActionButton
        title={title}
        icon={<IconImageInPicture />}
        onClick={() => setOpen(true)}
        isActive={editor.isActive('raw-html')}
        disabled={editor.isActive('code')}
      />
    </Stack>
  );
};

export default EmbedHTML;
