'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');

var _lodash = require('lodash.isobject');

var _lodash2 = _interopRequireDefault(_lodash);

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

var colWidths = ['xs', 'sm', 'md', 'lg', 'xl'];

var stringOrNumberProp = _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]);

var columnProps = _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.shape({
  size: stringOrNumberProp,
  push: (0, _helper.deprecated)(stringOrNumberProp, 'Please use the prop "order"'),
  pull: (0, _helper.deprecated)(stringOrNumberProp, 'Please use the prop "order"'),
  order: stringOrNumberProp,
  offset: stringOrNumberProp
})]);

var getColumnSizeClass = function getColumnSizeClass(isXs, colWidth, colSize) {
  if (colSize === true || colSize === '') {
    return isXs ? 'col' : 'col-' + colWidth;
  } else if (colSize === 'auto') {
    return isXs ? 'col-auto' : 'col-' + colWidth + '-auto';
  }
  return isXs ? 'col-' + colSize : 'col-' + colWidth + '-' + colSize;
};

//component - CoreUI / CLabel

var CLabel = function CLabel(props) {
  var Tag = props.tag,
      className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      hidden = props.hidden,
      widths = props.widths,
      check = props.check,
      size = props.size,
      htmlFor = props.for,
      attributes = _objectWithoutProperties(props, ['tag', 'className', 'cssModule', 'innerRef', 'hidden', 'widths', 'check', 'size', 'for']);

  // render

  var colClasses = [];

  widths.forEach(function (colWidth, i) {

    var columnProp = props[colWidth];
    delete attributes[colWidth];

    if (!columnProp && columnProp !== '') {
      return;
    }

    var isXs = !i;
    var colClass = void 0;

    if ((0, _lodash2.default)(columnProp)) {
      var _classNames;

      var colSizeInterfix = isXs ? '-' : '-' + colWidth + '-';
      colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
      colClasses.push((0, _helper.mapToCssModules)((0, _classnames2.default)((_classNames = {}, _classNames[colClass] = columnProp.size || columnProp.size === '', _classNames['order' + colSizeInterfix + columnProp.order] = columnProp.order || columnProp.order === 0, _classNames['offset' + colSizeInterfix + columnProp.offset] = columnProp.offset || columnProp.offset === 0, _classNames))), cssModule);
    } else {
      colClass = getColumnSizeClass(isXs, colWidth, columnProp);
      colClasses.push(colClass);
    }
  });

  var classes = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, hidden ? 'sr-only' : false, check ? 'form-check-label' : false, size ? 'col-form-label-' + size : false, colClasses, colClasses.length ? 'col-form-label' : false), cssModule);

  return _react2.default.createElement(Tag, _extends({ htmlFor: htmlFor }, attributes, { className: classes, ref: innerRef }));
};

CLabel.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _helper.tagPropType,
  children: _propTypes2.default.node,
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  hidden: _propTypes2.default.bool,
  check: _propTypes2.default.bool,
  size: _propTypes2.default.string,
  for: _propTypes2.default.string,
  xs: columnProps,
  sm: columnProps,
  md: columnProps,
  lg: columnProps,
  xl: columnProps,
  widths: _propTypes2.default.array
} : {};

CLabel.defaultProps = {
  tag: 'label',
  widths: colWidths
};

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