"use strict";

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

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

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

var _useThemeProps = require("../../../hooks/useThemeProps");

var _Center = require("../../composites/Center");

var _FormControl = require("../../composites/FormControl");

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

var _toggle = require("@react-stately/toggle");

var _visuallyHidden = require("@react-aria/visually-hidden");

var _CheckboxGroup = require("./CheckboxGroup");

var _interactions = require("@react-native-aria/interactions");

var _checkbox = require("@react-native-aria/checkbox");

var _focus = require("@react-native-aria/focus");

var _useHasResponsiveProps = require("../../../hooks/useHasResponsiveProps");

var _utils2 = require("../../../theme/tools/utils");

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

var _SizedIcon = _interopRequireDefault(require("./SizedIcon"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

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

const Checkbox = ({
  wrapperRef,
  ...props
}, ref) => {
  const formControlContext = (0, _FormControl.useFormControlContext)();
  const {
    isInvalid,
    isReadOnly,
    isIndeterminate,
    ...combinedProps
  } = (0, _utils3.combineContextAndProps)(formControlContext, props);

  const checkboxGroupContext = _react.default.useContext(_CheckboxGroup.CheckboxGroupContext);

  const _ref = _react.default.useRef();

  const mergedRef = (0, _utils.mergeRefs)([ref, _ref]);
  const state = (0, _toggle.useToggleState)({ ...props,
    defaultSelected: props.defaultIsChecked,
    isSelected: props.isChecked
  });
  const groupState = (0, _react.useContext)(_CheckboxGroup.CheckboxGroupContext);
  const {
    isHovered
  } = (0, _interactions.useHover)({}, _ref); // Swap hooks depending on whether this checkbox is inside a CheckboxGroup.
  // This is a bit unorthodox. Typically, hooks cannot be called in a conditional,
  // but since the checkbox won't move in and out of a group, it should be safe.

  const {
    inputProps
  } = groupState ? // eslint-disable-next-line react-hooks/rules-of-hooks
  (0, _checkbox.useCheckboxGroupItem)({ ...combinedProps,
    'aria-label': combinedProps.accessibilityLabel,
    'value': combinedProps.value
  }, groupState.state, //@ts-ignore
  mergedRef) : // eslint-disable-next-line react-hooks/rules-of-hooks
  (0, _checkbox.useCheckbox)({ ...combinedProps,
    'aria-label': combinedProps.accessibilityLabel
  }, state, //@ts-ignore
  mergedRef);
  const {
    checked: isChecked,
    disabled: isDisabled
  } = inputProps;
  const {
    focusProps,
    isFocusVisible
  } = (0, _focus.useFocusRing)();
  const {
    icon,
    _interactionBox,
    _icon,
    ...resolvedProps
  } = (0, _useThemeProps.usePropsResolution)('Checkbox', { ...checkboxGroupContext,
    ...combinedProps
  }, {
    isInvalid,
    isReadOnly,
    isFocusVisible,
    isDisabled,
    isIndeterminate,
    isChecked,
    isHovered
  });
  const [layoutProps, nonLayoutProps] = (0, _utils2.extractInObject)(resolvedProps, [..._utils2.stylingProps.margin, ..._utils2.stylingProps.layout, ..._utils2.stylingProps.flexbox, ..._utils2.stylingProps.position, '_text']);

  const component = /*#__PURE__*/_react.default.createElement(_Box.default, _extends({}, layoutProps, {
    opacity: isDisabled ? 0.4 : 1,
    cursor: isDisabled ? 'not-allowed' : 'pointer'
  }), /*#__PURE__*/_react.default.createElement(_Center.Center, null, /*#__PURE__*/_react.default.createElement(_Box.default, _extends({}, _interactionBox, {
    style: {
      // @ts-ignore - only for web"
      transition: 'height 200ms, width 200ms'
    },
    h: isFocusVisible || isHovered || isChecked || isInvalid ? '200%' : '100%',
    w: isFocusVisible || isHovered || isChecked || isInvalid ? '200%' : '100%',
    zIndex: -1
  })), /*#__PURE__*/_react.default.createElement(_Center.Center, nonLayoutProps, /*#__PURE__*/_react.default.createElement(_SizedIcon.default, {
    icon: icon,
    _icon: _icon,
    isChecked: isChecked
  }))), resolvedProps === null || resolvedProps === void 0 ? void 0 : resolvedProps.children); //TODO: refactor for responsive prop


  if ((0, _useHasResponsiveProps.useHasResponsiveProps)(resolvedProps)) {
    return null;
  }

  return /*#__PURE__*/_react.default.createElement(_Box.default // @ts-ignore - RN web supports accessibilityRole="label"
  , {
    accessibilityRole: "label",
    ref: (0, _utils.mergeRefs)([wrapperRef, _ref])
  }, /*#__PURE__*/_react.default.createElement(_visuallyHidden.VisuallyHidden, null, /*#__PURE__*/_react.default.createElement("input", _extends({}, inputProps, focusProps, {
    ref: mergedRef
  }))), component);
};

var _default = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(Checkbox));

exports.default = _default;
//# sourceMappingURL=Checkbox.web.js.map