"use strict";

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

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

var _reactNative = require("react-native");

var _primitives = require("../../primitives");

var _composites = require("../../composites");

var _theme = require("../../../theme");

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

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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); }

const Checkbox = ({
  children,
  ...props
}, ref) => {
  const formControlContext = _react.default.useContext(_FormControl.FormControlContext);

  const {
    groupValueChangeHandler,
    value: cValue = [],
    ...context
  } = _react.default.useContext(_CheckboxGroup.CheckboxContext);

  const {
    activeColor,
    borderColor,
    iconColor,
    onChange,
    isIndeterminate,
    isChecked: pIsChecked,
    defaultIsChecked,
    isDisabled,
    value: pValue,
    ariaLabel,
    icon,
    size,
    ...newProps
  } = (0, _theme.usePropsConfig)('Checkbox', { ...context,
    ...formControlContext,
    ...props
  });
  let isChecked = pIsChecked || cValue.includes(pValue);

  const [checkboxState, setCheckboxState] = _react.default.useState(isChecked || defaultIsChecked);

  const pressHandler = () => {
    isChecked = !checkboxState;
    setCheckboxState(isChecked);

    if (!isIndeterminate) {
      groupValueChangeHandler && groupValueChangeHandler(pValue, isChecked);
      onChange && onChange(pValue, isChecked);
    }
  };

  const sizedIcon = () => /*#__PURE__*/_react.default.cloneElement(icon, {
    size,
    color: icon.props.color ? icon.props.color : iconColor
  }, icon.props.children);

  return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
    activeOpacity: 1,
    disabled: isDisabled,
    onPress: () => pressHandler(),
    accessible: true,
    accessibilityLabel: ariaLabel,
    accessibilityRole: "checkbox",
    ref: ref
  }, /*#__PURE__*/_react.default.createElement(_primitives.Box, _extends({
    flexDirection: "row",
    alignItems: "center"
  }, newProps), /*#__PURE__*/_react.default.createElement(_composites.Center, {
    backgroundColor: checkboxState ? activeColor : 'transparent',
    borderColor: checkboxState ? activeColor : borderColor,
    borderWidth: 2,
    borderRadius: 5
  }, icon && checkboxState ? sizedIcon() : /*#__PURE__*/_react.default.createElement(_primitives.Icon, {
    name: "check-bold",
    type: "MaterialCommunityIcons",
    size: size,
    color: iconColor,
    opacity: checkboxState ? 1 : 0
  })), children));
};

var _default = /*#__PURE__*/_react.default.forwardRef(Checkbox);

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