import { useState } from 'react';

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

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

import EmbedVideoModal from './modal';

export type EmbedVideoProps = {
  title: string;
  transformer?: (url: string) => string;
};

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

  if (!editor) {
    return null;
  }

  return (
    <Stack>
      <EmbedVideoModal
        open={open}
        onClose={() => setOpen(false)}
        transformer={transformer}
      />
      <ActionButton
        title={title}
        icon={<IconVideo />}
        onClick={() => setOpen(true)}
        isActive={isMediaActive(editor, 'iframe')}
        disabled={editor.isActive('code')}
      />
    </Stack>
  );
};

export default EmbedVideo;
