function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import { getOverride, getOverrideProps } from '../helpers/overrides.js';
import { Checkmark as StyledCheckmark, Input as StyledInput, Label as StyledLabel, Root as StyledRoot, Toggle as StyledToggle, ToggleInner as StyledToggleInner, ToggleTrack as StyledToggleTrack } from './styled-components.js';
import { STYLE_TYPE } from './constants.js';

var StatelessCheckbox =
/*#__PURE__*/
function (_React$Component) {
  _inherits(StatelessCheckbox, _React$Component);

  function StatelessCheckbox() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, StatelessCheckbox);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(StatelessCheckbox)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      isFocused: _this.props.autoFocus || false,
      isHovered: false,
      isActive: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onMouseEnter", function (e) {
      _this.setState({
        isHovered: true
      });

      _this.props.onMouseEnter(e);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onMouseLeave", function (e) {
      _this.setState({
        isHovered: false,
        isActive: false
      });

      _this.props.onMouseLeave(e);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onMouseDown", function (e) {
      _this.setState({
        isActive: true
      });

      _this.props.onMouseDown(e);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onMouseUp", function (e) {
      _this.setState({
        isActive: false
      });

      _this.props.onMouseUp(e);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onFocus", function (e) {
      _this.setState({
        isFocused: true
      });

      _this.props.onFocus(e);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onBlur", function (e) {
      _this.setState({
        isFocused: false
      });

      _this.props.onBlur(e);
    });

    return _this;
  }

  _createClass(StatelessCheckbox, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          autoFocus = _this$props.autoFocus,
          inputRef = _this$props.inputRef;

      if (autoFocus && inputRef.current) {
        inputRef.current.focus();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var checkmarkType = this.props.checkmarkType;
      var _this$props2 = this.props,
          _this$props2$override = _this$props2.overrides,
          overrides = _this$props2$override === void 0 ? {} : _this$props2$override,
          onChange = _this$props2.onChange,
          _this$props2$labelPla = _this$props2.labelPlacement,
          labelPlacement = _this$props2$labelPla === void 0 ? checkmarkType === STYLE_TYPE.toggle ? 'left' : 'right' : _this$props2$labelPla,
          inputRef = _this$props2.inputRef,
          isIndeterminate = _this$props2.isIndeterminate,
          isError = _this$props2.isError,
          disabled = _this$props2.disabled,
          value = _this$props2.value,
          name = _this$props2.name,
          type = _this$props2.type,
          checked = _this$props2.checked,
          children = _this$props2.children,
          required = _this$props2.required;
      var RootOverride = overrides.Root,
          CheckmarkOverride = overrides.Checkmark,
          LabelOverride = overrides.Label,
          InputOverride = overrides.Input,
          ToggleOverride = overrides.Toggle,
          ToggleInnerOverride = overrides.ToggleInner,
          ToggleTrackOverride = overrides.ToggleTrack;
      var Root = getOverride(RootOverride) || StyledRoot;
      var Checkmark = getOverride(CheckmarkOverride) || StyledCheckmark;
      var Label = getOverride(LabelOverride) || StyledLabel;
      var Input = getOverride(InputOverride) || StyledInput;
      var Toggle = getOverride(ToggleOverride) || StyledToggle;
      var ToggleInner = getOverride(ToggleInnerOverride) || StyledToggleInner;
      var ToggleTrack = getOverride(ToggleTrackOverride) || StyledToggleTrack;
      var inputEvents = {
        onChange: onChange,
        onFocus: this.onFocus,
        onBlur: this.onBlur
      };
      var mouseEvents = {
        onMouseEnter: this.onMouseEnter,
        onMouseLeave: this.onMouseLeave,
        onMouseDown: this.onMouseDown,
        onMouseUp: this.onMouseUp
      };
      var sharedProps = {
        $isFocused: this.state.isFocused,
        $isHovered: this.state.isHovered,
        $isActive: this.state.isActive,
        $isError: isError,
        $checked: checked,
        $isIndeterminate: isIndeterminate,
        $required: required,
        $disabled: disabled,
        $value: value,
        $checkmarkType: checkmarkType
      };
      var labelComp = React.createElement(Label, _extends({
        $labelPlacement: labelPlacement
      }, sharedProps, getOverrideProps(LabelOverride)), children);
      return React.createElement(Root, _extends({
        "data-baseweb": "checkbox",
        $labelPlacement: labelPlacement
      }, sharedProps, mouseEvents, getOverrideProps(RootOverride)), (labelPlacement === 'top' || labelPlacement === 'left') && labelComp, checkmarkType === STYLE_TYPE.toggle ? React.createElement(ToggleTrack, _extends({}, sharedProps, getOverrideProps(ToggleTrackOverride)), React.createElement(Toggle, _extends({}, sharedProps, getOverrideProps(ToggleOverride)), React.createElement(ToggleInner, _extends({}, sharedProps, getOverrideProps(ToggleInnerOverride))))) : React.createElement(Checkmark, _extends({
        checked: checked
      }, sharedProps, getOverrideProps(CheckmarkOverride))), React.createElement(Input, _extends({
        value: value,
        name: name,
        checked: checked,
        required: required,
        "aria-invalid": isError || null,
        "aria-required": required || null,
        disabled: disabled,
        type: type,
        $ref: inputRef
      }, sharedProps, inputEvents, getOverrideProps(InputOverride))), (labelPlacement === 'bottom' || labelPlacement === 'right') && labelComp);
    }
  }]);

  return StatelessCheckbox;
}(React.Component);

_defineProperty(StatelessCheckbox, "defaultProps", {
  overrides: {},
  checked: false,
  disabled: false,
  autoFocus: false,
  isIndeterminate: false,
  inputRef: React.createRef(),
  isError: false,
  type: 'checkbox',
  checkmarkType: STYLE_TYPE.default,
  onChange: function onChange() {},
  onMouseEnter: function onMouseEnter() {},
  onMouseLeave: function onMouseLeave() {},
  onMouseDown: function onMouseDown() {},
  onMouseUp: function onMouseUp() {},
  onFocus: function onFocus() {},
  onBlur: function onBlur() {}
});

export default StatelessCheckbox;