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

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 / CSwitch

var CSwitch = function CSwitch(props) {
  var className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      type = props.type,
      size = props.size,
      disabled = props.disabled,
      color = props.color,
      labelOn = props.labelOn,
      labelOff = props.labelOff,
      variant = props.variant,
      shape = props.shape,
      name = props.name,
      required = props.required,
      value = props.value,
      attributes = _objectWithoutProperties(props, ['className', 'cssModule', 'innerRef', 'type', 'size', 'disabled', 'color', 'labelOn', 'labelOff', 'variant', 'shape', 'name', 'required', 'value']);

  var fields = (0, _react.useRef)({ firstRender: true }).current;

  var _useState = (0, _react.useState)(props.defaultChecked || props.checked),
      checked = _useState[0],
      setChecked = _useState[1];

  var _useState2 = (0, _react.useState)([]),
      selected = _useState2[0],
      setSelected = _useState2[1];

  //effect - update


  (0, _react.useEffect)(function () {
    if (fields.firstRender) return;
    setChecked(props.checked);
  }, [props.checked]);

  (0, _react.useEffect)(function () {
    fields.firstRender = false;
  }, []);

  //events
  var onChange = function onChange(e) {
    var target = e.target;
    setChecked(target.checked);
    if (props.onChange) {
      props.onChange(e);
    }
  };

  //render

  delete attributes.checked;
  delete attributes.defaultChecked;
  delete attributes.onChange;

  /*
  const outlineString = this.outline ? '-outline' : ''
      const outlinedAltString = this.outline === 'alt' ? '-alt' : ''
      return [
        'c-switch form-check-label',
        `c-switch${outlineString}-${this.color}${outlinedAltString}`,
        {
          [`c-switch-${this.size}`]: this.size,
          [`c-switch-${this.shape}`]: this.shape,
          'c-switch-label': this.labelOn || this.labelOff
        }
      ]
  */
  /*
  let outline='';
  switch (variant) {
    case '3d':
        break;
    case 'opposite':
      variant="alt";
      break;
    case '3d-opposite':
        break;
  }
  */

  //console.log(variant);

  var variant2 = ['opposite', 'outline'].indexOf(variant) > -1 ? '-' + variant : '';
  var classes = (0, _classnames2.default)(className, 'c-switch form-check-label', labelOn || labelOff ? 'c-switch-label' : false, size ? 'c-switch-' + size : '', shape ? 'c-switch-' + shape : '', color ? 'c-switch' + variant2 + '-' + color : '', variant == '3d' ? 'c-switch-3d' : '');

  var inputClasses = (0, _classnames2.default)('c-switch-input', 'c-form-check-input');

  var sliderClasses = (0, _classnames2.default)('c-switch-slider');

  //classes = "c-switch form-check-label c-switch-label c-switch-opposite c-switch-primary c-switch-lg"

  return _react2.default.createElement(
    'label',
    _extends({}, attributes, { className: classes, ref: innerRef }),
    _react2.default.createElement('input', { className: inputClasses, type: type, onChange: onChange, checked: checked, name: name, required: required, disabled: disabled, value: value }),
    _react2.default.createElement('span', { className: sliderClasses, 'data-checked': labelOn, 'data-unchecked': labelOff })
  );
};

CSwitch.propTypes = process.env.NODE_ENV !== "production" ? {
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  size: _propTypes2.default.oneOf(['', 'lg', 'sm']),
  shape: _propTypes2.default.oneOf(['', 'pill', 'square']),
  variant: _propTypes2.default.oneOf(['', '3d', '3d-opposite', 'opposite', 'outline']),
  color: _propTypes2.default.string,
  checked: _propTypes2.default.bool,
  labelOn: _propTypes2.default.string,
  labelOff: _propTypes2.default.string,
  type: _propTypes2.default.oneOf(['checkbox', 'radio']),

  value: _propTypes2.default.string,
  defaultChecked: _propTypes2.default.bool,
  name: _propTypes2.default.string,
  disabled: _propTypes2.default.bool,
  form: _propTypes2.default.any, //?
  required: _propTypes2.default.bool,
  onChange: _propTypes2.default.func

  //label: PropTypes.bool,
  /*
  outline: PropTypes.oneOfType([
    PropTypes.bool,
    PropTypes.string,
    PropTypes.oneOf(['', 'alt'])
  ]),
  */

} : {};

CSwitch.defaultProps = {
  size: '',
  checked: false,
  type: 'checkbox',
  variant: '',

  defaultChecked: false,
  disabled: false,
  required: false
  //labelOn: 'On',
  //labelOff: 'Off',
  //label: false,
  //outline: false,
};

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