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

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

const defaultProps = {
  children: '',
  className: '',
  defaultClassName: 'sui-o-modal__title',
  tag: 'div',
};

export const ModalTitle = ({
  children, className, defaultClassName, tag: Tag,
}) => {
  const { id } = useModalContext();

  return (
    <Tag id={`dialog:${id}:label`} className={classnames(defaultClassName, className)}>
      {children}
    </Tag>
  );
};

ModalTitle.propTypes = propTypes;
ModalTitle.defaultProps = defaultProps;
