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

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

import React, { useEffect, useState, useRef } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

//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 = useRef({ firstRender: true }).current;

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

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

  //effect - update


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

  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 = classNames(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 = classNames('c-switch-input', 'c-form-check-input');

  var sliderClasses = classNames('c-switch-slider');

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

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

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

  value: PropTypes.string,
  defaultChecked: PropTypes.bool,
  name: PropTypes.string,
  disabled: PropTypes.bool,
  form: PropTypes.any, //?
  required: PropTypes.bool,
  onChange: PropTypes.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,
};

export default CSwitch;