"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CheckBox = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeSvg = require("react-native-svg");
var _icons = require("../../assets/icons");
var _styles = require("./styles");
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const CheckBoxContext = /*#__PURE__*/(0, _react.createContext)(null);
const useCheckBoxContext = () => {
  const context = (0, _react.useContext)(CheckBoxContext);
  return context;
};
function Group({
  value,
  select,
  onChange,
  children,
  disabled,
  extractKey,
  ...props
}) {
  return /*#__PURE__*/React.createElement(CheckBoxContext.Provider, {
    value: {
      value,
      onChange,
      disabled,
      select,
      extractKey
    }
  }, /*#__PURE__*/React.createElement(_reactNative.View, props, children));
}
const OptionContext = /*#__PURE__*/(0, _react.createContext)(null);
const useOptionContext = () => {
  const context = (0, _react.useContext)(OptionContext);
  return context;
};
function Option({
  style,
  value,
  children,
  disabled,
  accessibilityHint,
  accessibilityLabel,
  ...props
}) {
  const {
    select,
    onChange,
    extractKey,
    value: selectedValue,
    disabled: groupDisabled
  } = useCheckBoxContext();
  const {
    styles
  } = (0, _styles.useStyles)();
  const selected = select ? select(selectedValue, value) : selectedValue.includes(value);
  const isDisabled = disabled || groupDisabled;
  const handlePress = () => {
    if (isDisabled) return;
    const newValue = selected ? selectedValue.filter(v => extractKey ? extractKey(v) !== extractKey(value) : v !== value) : [...selectedValue, value];
    onChange(newValue);
  };
  return /*#__PURE__*/React.createElement(OptionContext.Provider, {
    value: {
      value,
      disabled: isDisabled
    }
  }, /*#__PURE__*/React.createElement(_reactNative.Pressable, _extends({}, props, {
    disabled: isDisabled,
    onPress: handlePress,
    accessibilityRole: "checkbox",
    accessibilityLabel: accessibilityLabel || `Option ${value}`,
    accessibilityState: {
      checked: selected,
      disabled: isDisabled
    },
    accessibilityHint: accessibilityHint || `${selected ? 'Selected' : 'Not selected'}.`,
    style: ({
      pressed
    }) => [styles.option, pressed && styles.optionPressed, style && style({
      pressed,
      selected,
      disabled: isDisabled
    })]
  }), children));
}
const Icon = ({
  width = 24,
  height = 24,
  style,
  ...props
}) => {
  const {
    theme
  } = (0, _styles.useStyles)();
  const option = useOptionContext();
  const {
    value,
    select
  } = useCheckBoxContext();
  const isSelected = select ? select(value, option.value) : value.includes(option === null || option === void 0 ? void 0 : option.value);
  const color = isSelected ? option !== null && option !== void 0 && option.disabled ? theme.colors.primaryShade3 : theme.colors.primary : option !== null && option !== void 0 && option.disabled ? theme.colors.baseShade4 : theme.colors.baseShade3;
  const icon = isSelected ? option !== null && option !== void 0 && option.disabled ? (0, _icons.checkboxCheckedDisabled)() : (0, _icons.checkboxChecked)() : option !== null && option !== void 0 && option.disabled ? (0, _icons.checkboxUncheckedDisabled)() : (0, _icons.checkboxUnchecked)();
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: style
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, _extends({}, props, {
    xml: icon,
    color: color,
    width: width,
    height: height
  })));
};
const Label = ({
  children,
  ...props
}) => {
  return /*#__PURE__*/React.createElement(_reactNative.View, props, children);
};
const CheckBox = exports.CheckBox = {
  Group,
  Option,
  Icon,
  Label
};
//# sourceMappingURL=index.js.map