'use strict';

Object.defineProperty(exports, "__esModule", {
  value: 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 _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _index = require('./Hint/index');

var _index2 = _interopRequireDefault(_index);

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

var _styleModuleImportMap = {
  './field-control.scss': {
    'form-group': 'flive_field-control_form-group',
    'with-marge': 'flive_field-control_with-marge',
    'form-label': 'flive_field-control_form-label',
    'mandatory': 'flive_field-control_mandatory'
  }
};

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; }

var formatErrorLabel = function formatErrorLabel(_ref) {
  var error = _ref.error,
      label = _ref.label,
      name = _ref.name;
  return error.replace('"' + name + '"', '"' + label + '"');
};

var propTypes = {
  className: _propTypes2.default.string,
  label: _propTypes2.default.string,
  children: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.func]),
  mandatory: _propTypes2.default.bool,
  length: _propTypes2.default.number,
  maxLength: _propTypes2.default.number,
  touched: _propTypes2.default.bool,
  error: _propTypes2.default.node,
  hint: _propTypes2.default.node,
  meta: _propTypes2.default.object,
  input: _propTypes2.default.object,
  withoutMarge: _propTypes2.default.bool,
  formatError: _propTypes2.default.func
};

var defaultProps = {
  className: null,
  label: null,
  hint: null,
  children: null,
  mandatory: false,
  length: 0,
  maxLength: null,
  touched: false,
  error: null,
  meta: null,
  input: null,
  withoutMarge: false,
  formatError: formatErrorLabel
};

var FieldControl = function FieldControl(_ref2) {
  var className = _ref2.className,
      label = _ref2.label,
      hint = _ref2.hint,
      children = _ref2.children,
      mandatory = _ref2.mandatory,
      maxLength = _ref2.maxLength,
      length = _ref2.length,
      meta = _ref2.meta,
      withoutMarge = _ref2.withoutMarge,
      formatError = _ref2.formatError,
      props = _objectWithoutProperties(_ref2, ['className', 'label', 'hint', 'children', 'mandatory', 'maxLength', 'length', 'meta', 'withoutMarge', 'formatError']);

  var _ref3 = meta || props,
      error = _ref3.error,
      touched = _ref3.touched;

  var _ref4 = props.input || props,
      name = _ref4.name;

  return _react2.default.createElement(
    'div',
    {
      className: (className ? className + ' ' : '') + (0, _getClassName3.default)((0, _classnames2.default)('form-group', { 'with-marge': !withoutMarge }), _styleModuleImportMap)
    },
    label && _react2.default.createElement(
      'span',
      { className: 'flive_field-control_form-label' },
      label,
      mandatory && _react2.default.createElement(
        'span',
        { className: 'flive_field-control_mandatory' },
        '*'
      )
    ),
    typeof children === 'function' ? children((0, _omit2.default)(props, ['error', 'touched'])) : children,
    touched && !!error && _react2.default.createElement(
      _index2.default,
      { variant: 'danger' },
      formatError(_extends({}, props, { name: name, label: label, error: error }))
    ),
    !!maxLength && _react2.default.createElement(
      _index2.default,
      null,
      'Content length :',
      ' ',
      _react2.default.createElement(
        _index2.default,
        { component: 'span', variant: length <= maxLength ? 'success' : 'danger' },
        length
      ),
      ' / ',
      maxLength,
      ' (Max. length)'
    ),
    !!hint && _react2.default.createElement(
      _index2.default,
      null,
      hint
    )
  );
};

FieldControl.propTypes = propTypes;
FieldControl.defaultProps = defaultProps;

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