import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { modifierCSS } from '../../../helpers';

const propTypes = {
  className: PropTypes.string,
  color: PropTypes.oneOf(['', 'warning', 'danger', 'success', 'primary']),
  defaultClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
  isDisabled: PropTypes.bool,
  isInline: PropTypes.bool,
  isLoading: PropTypes.bool,
  isReadOnly: PropTypes.bool,
  /**
   * The component used for the root node.
   * Either a string to use a DOM element or a component.
   */
  tag: PropTypes.elementType,
  wrapperClassName: PropTypes.string,
};

const defaultProps = {
  className: '',
  color: '',
  defaultClassName: 'sui-a-form-control',
  isDisabled: false,
  isInline: false,
  isLoading: false,
  isReadOnly: false,
  tag: 'input',
  wrapperClassName: '',
};

export const FormControlInput = ({
  className,
  color,
  defaultClassName,
  isDisabled,
  isInline,
  isLoading,
  isReadOnly,
  tag: Tag,
  wrapperClassName,
  ...attributes
}) => {
  const classes = classnames(
    className,
    modifierCSS(color),
    isInline ? 'as--inline' : ''
  );

  const wrapperClasses = classnames(
    defaultClassName,
    isLoading ? 'as--loading' : '',
    isInline ? 'as--inline' : '',
    wrapperClassName,
  );

  return (
    <div className={wrapperClasses}>
      <Tag disabled={isDisabled} readOnly={isReadOnly} {...attributes} className={classes} />
    </div>
  );
};

FormControlInput.propTypes = propTypes;
FormControlInput.defaultProps = defaultProps;
