"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Checkbox = void 0;

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

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

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

var _CheckBoldIcon = _interopRequireDefault(require("mdi-react/CheckBoldIcon"));

var _MinusIcon = _interopRequireDefault(require("mdi-react/MinusIcon"));

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

var _Box = require("../Box");

var _ControlBox = require("../ControlBox");

var _Form = require("../Form");

var _useForkRef = require("../hooks/useForkRef");

var _useVariantColorWarning = require("../hooks/useVariantColorWarning");

var _Text = require("../Text");

var _VisuallyHidden = require("../VisuallyHidden");

var _styles = _interopRequireDefault(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var Checkbox = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var id = _ref2.id,
      name = _ref2.name,
      value = _ref2.value,
      ariaLabel = _ref2['aria-label'],
      ariaLabelledBy = _ref2['aria-labelledby'],
      _ref2$variantColor = _ref2.variantColor,
      variantColor = _ref2$variantColor === void 0 ? 'primary' : _ref2$variantColor,
      defaultIsChecked = _ref2.defaultIsChecked,
      isFullWidth = _ref2.isFullWidth,
      isChild = _ref2.isChild,
      _ref2$size = _ref2.size,
      size = _ref2$size === void 0 ? 'md' : _ref2$size,
      isDisabled = _ref2.isDisabled,
      isInvalid = _ref2.isInvalid,
      isReadOnly = _ref2.isReadOnly,
      onChange = _ref2.onChange,
      onBlur = _ref2.onBlur,
      onFocus = _ref2.onFocus,
      isIndeterminate = _ref2.isIndeterminate,
      renderCustomControl = _ref2.renderCustomControl,
      skipFormChange = _ref2.skipFormChange,
      children = _ref2.children,
      iconColor = _ref2.iconColor,
      _ref2$iconSize = _ref2.iconSize,
      iconSize = _ref2$iconSize === void 0 ? '12px' : _ref2$iconSize,
      rest = (0, _objectWithoutProperties2["default"])(_ref2, ["id", "name", "value", "aria-label", "aria-labelledby", "variantColor", "defaultIsChecked", "isFullWidth", "isChild", "size", "isDisabled", "isInvalid", "isReadOnly", "onChange", "onBlur", "onFocus", "isIndeterminate", "renderCustomControl", "skipFormChange", "children", "iconColor", "iconSize"]);
  // Wrong usage of `variantColor` prop is quite common
  // Let's add a warning hook that validates the passed variantColor
  (0, _useVariantColorWarning.useVariantColorWarning)('Checkbox', variantColor);

  var _useFormField = (0, _Form.useFormField)({
    name: name,
    onChange: onChange
  }),
      formOnChange = _useFormField.onChange,
      initialCheckboxValue = _useFormField.value;

  var _rest$isChecked = rest.isChecked,
      isChecked = _rest$isChecked === void 0 ? initialCheckboxValue : _rest$isChecked;

  var _useCheckboxStyle = (0, _styles["default"])({
    color: variantColor,
    size: size,
    isDisabled: isDisabled,
    isFullWidth: isFullWidth,
    isChild: isChild
  }),
      rootStyleProps = _useCheckboxStyle.root,
      labelStyleProps = _useCheckboxStyle.label,
      containerStyleProps = _useCheckboxStyle.container;

  var ownRef = (0, _react.useRef)();

  var _ref = (0, _useForkRef.useForkRef)(ownRef, ref);

  (0, _react.useEffect)(function () {
    if (_ref.current) {
      _ref.current.indeterminate = Boolean(isIndeterminate);
    }
  }, [isIndeterminate, _ref]);

  var onSwitchChange = function onSwitchChange(v) {
    if (!skipFormChange && formOnChange && typeof formOnChange === 'function') {
      formOnChange(v, v.target.checked);
    }

    if (onChange) {
      onChange(v);
    }
  };

  var IconTag = isIndeterminate ? _MinusIcon["default"] : _CheckBoldIcon["default"];
  return __jsx(_Box.Box, (0, _extends2["default"])({
    as: "label"
  }, containerStyleProps, rest), __jsx(_VisuallyHidden.VisuallyHidden, {
    as: "input",
    type: "checkbox",
    "aria-label": ariaLabel,
    id: name,
    ref: _ref,
    name: name,
    value: value,
    onChange: isReadOnly ? undefined : onSwitchChange,
    onBlur: onBlur,
    onFocus: onFocus,
    defaultChecked: isReadOnly ? undefined : defaultIsChecked,
    checked: isChecked,
    disabled: isDisabled,
    readOnly: isReadOnly,
    "aria-readonly": isReadOnly,
    "aria-invalid": isInvalid,
    "aria-checked": isIndeterminate ? 'mixed' : isChecked
  }), renderCustomControl ? renderCustomControl(_objectSpread({
    isChecked: isChecked
  }, rootStyleProps)) : __jsx(_ControlBox.ControlBox, (0, _extends2["default"])({
    opacity: isReadOnly ? 0.8 : 1
  }, rootStyleProps), __jsx(IconTag, {
    size: iconSize,
    color: iconColor
  })), children && __jsx(_Text.Text, labelStyleProps, children));
});
exports.Checkbox = Checkbox;
//# sourceMappingURL=Checkbox.js.map