import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Button } from '../../atoms/button/Button';
import { useModalContext } from './Modal';

const propTypes = {
  className: PropTypes.string,
  defaultClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
  tag: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
};

const defaultProps = {
  className: '',
  defaultClassName: 'sui-o-modal__close',
  tag: Button,
};

export const ModalCloseButton = ({
  className, defaultClassName, tag: Tag,
}) => {
  const { handleClose } = useModalContext();

  const classes = classnames(defaultClassName, className);

  return (
    <Tag defaultClassName={defaultClassName} className={classes} onClick={handleClose}>
      &times;
    </Tag>
  );
};

ModalCloseButton.propTypes = propTypes;
ModalCloseButton.defaultProps = defaultProps;
