'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _helper = require('./Shared/helper.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

//component - CoreUI / CCustomInput

var CCustomInput = function CCustomInput(props) {
  var children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      label = props.label,
      inline = props.inline,
      valid = props.valid,
      invalid = props.invalid,
      bsSize = props.bsSize,
      wrapperProps = props.wrapperProps,
      attributes = _objectWithoutProperties(props, ['children', 'className', 'cssModule', 'innerRef', 'label', 'inline', 'valid', 'invalid', 'bsSize', 'wrapperProps']);

  //render

  var type = attributes.type;

  var customClass = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, 'custom-' + type, bsSize ? 'custom-' + type + '-' + bsSize : false), cssModule);

  var validationClassNames = (0, _helper.mapToCssModules)((0, _classnames2.default)(invalid && 'is-invalid', valid && 'is-valid'), cssModule);

  if (type === 'select') {
    return _react2.default.createElement(
      'select',
      _extends({}, attributes, { className: (0, _classnames2.default)(validationClassNames, customClass), ref: innerRef }),
      children
    );
  }

  if (type === 'file') {
    return _react2.default.createElement(
      'div',
      _extends({ className: customClass }, wrapperProps, { ref: innerRef }),
      _react2.default.createElement('input', _extends({}, attributes, { className: (0, _classnames2.default)(validationClassNames, (0, _helper.mapToCssModules)('custom-file-input', cssModule)) })),
      _react2.default.createElement(
        'label',
        { className: (0, _helper.mapToCssModules)('custom-file-label', cssModule), htmlFor: attributes.id },
        label || 'Choose file'
      )
    );
  }

  if (type !== 'checkbox' && type !== 'radio' && type !== 'switch') {
    return _react2.default.createElement('input', _extends({}, attributes, { className: (0, _classnames2.default)(validationClassNames, customClass), ref: innerRef }));
  }

  var wrapperClasses = (0, _classnames2.default)(customClass, (0, _helper.mapToCssModules)((0, _classnames2.default)('custom-control', { 'custom-control-inline': inline }), cssModule));

  return _react2.default.createElement(
    'div',
    _extends({}, wrapperProps, { className: wrapperClasses }),
    _react2.default.createElement('input', _extends({}, attributes, {
      type: type === 'switch' ? 'checkbox' : type,
      className: (0, _classnames2.default)(validationClassNames, (0, _helper.mapToCssModules)('custom-control-input', cssModule)),
      ref: innerRef
    })),
    _react2.default.createElement(
      'label',
      { className: (0, _helper.mapToCssModules)('custom-control-label', cssModule), htmlFor: attributes.id },
      label
    ),
    children
  );
};

CCustomInput.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.array, _propTypes2.default.func]),
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  id: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]).isRequired,
  type: _propTypes2.default.string.isRequired,
  label: _propTypes2.default.node,
  inline: _propTypes2.default.bool,
  valid: _propTypes2.default.bool,
  invalid: _propTypes2.default.bool,
  bsSize: _propTypes2.default.string,
  wrapperProps: _propTypes2.default.object
} : {};

exports.default = CCustomInput;
module.exports = exports['default'];