"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _utils = require("../utils");

var _Plaintext = _interopRequireDefault(require("../Plaintext"));

var defaultProps = {
  as: 'span',
  classPrefix: 'btn-toggle'
};

var Toggle = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      disabled = props.disabled,
      readOnly = props.readOnly,
      plaintext = props.plaintext,
      className = props.className,
      checkedChildren = props.checkedChildren,
      unCheckedChildren = props.unCheckedChildren,
      classPrefix = props.classPrefix,
      checkedProp = props.checked,
      defaultChecked = props.defaultChecked,
      size = props.size,
      localeProp = props.locale,
      onChange = props.onChange,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "disabled", "readOnly", "plaintext", "className", "checkedChildren", "unCheckedChildren", "classPrefix", "checked", "defaultChecked", "size", "locale", "onChange"]);

  var _useControlled = (0, _utils.useControlled)(checkedProp, defaultChecked),
      checked = _useControlled[0],
      setChecked = _useControlled[1];

  var _useCustom = (0, _utils.useCustom)('Toggle', localeProp),
      locale = _useCustom.locale;

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      merge = _useClassNames.merge,
      withClassPrefix = _useClassNames.withClassPrefix,
      prefix = _useClassNames.prefix;

  var classes = merge(className, withClassPrefix(size, {
    checked: checked,
    disabled: disabled
  }));
  var inner = checked ? checkedChildren : unCheckedChildren;
  var label = checked ? locale.on : locale.off;
  var handleChange = (0, _react.useCallback)(function (event) {
    if (disabled || readOnly) {
      return;
    }

    setChecked(!checked);
    onChange === null || onChange === void 0 ? void 0 : onChange(!checked, event);
  }, [checked, disabled, onChange, readOnly, setChecked]);

  if (plaintext) {
    return /*#__PURE__*/_react.default.createElement(_Plaintext.default, null, inner || label);
  }

  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
    role: "switch",
    "aria-checked": checked,
    "aria-disabled": disabled,
    "aria-label": typeof inner === 'string' ? inner : label,
    tabIndex: -1
  }, rest, {
    ref: ref,
    className: classes,
    onClick: handleChange
  }), /*#__PURE__*/_react.default.createElement("span", {
    className: prefix('inner')
  }, inner));
});

Toggle.displayName = 'Toggle';
Toggle.defaultProps = defaultProps;
Toggle.propTypes = {
  disabled: _propTypes.default.bool,
  checked: _propTypes.default.bool,
  defaultChecked: _propTypes.default.bool,
  checkedChildren: _propTypes.default.node,
  unCheckedChildren: _propTypes.default.node,
  classPrefix: _propTypes.default.string,
  className: _propTypes.default.string,
  onChange: _propTypes.default.func
};
var _default = Toggle;
exports.default = _default;