"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Checkbox = void 0;
var _react = _interopRequireWildcard(require("react"));
var _nativeProps = require("../../utils/native-props");
var _classnames = _interopRequireDefault(require("classnames"));
var _groupContext = require("./group-context");
var _usePropsValue = require("../../utils/use-props-value");
var _withDefaultProps = require("../../utils/with-default-props");
var _devLog = require("../../utils/dev-log");
var _checkIcon = require("./check-icon");
var _indeterminateIcon = require("./indeterminate-icon");
var _isDev = require("../../utils/is-dev");
var _nativeInput = require("./native-input");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-checkbox`;
const defaultProps = {
  defaultChecked: false,
  indeterminate: false
};
const Checkbox = exports.Checkbox = (0, _react.forwardRef)((p, ref) => {
  const groupContext = (0, _react.useContext)(_groupContext.CheckboxGroupContext);
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  let [checked, setChecked] = (0, _usePropsValue.usePropsValue)({
    value: props.checked,
    defaultValue: props.defaultChecked,
    onChange: props.onChange
  });
  let disabled = props.disabled;
  const {
    value
  } = props;
  if (groupContext && value !== undefined) {
    if (_isDev.isDev) {
      if (p.checked !== undefined) {
        (0, _devLog.devWarning)('Checkbox', 'When used within `Checkbox.Group`, the `checked` prop of `Checkbox` will not work.');
      }
      if (p.defaultChecked !== undefined) {
        (0, _devLog.devWarning)('Checkbox', 'When used within `Checkbox.Group`, the `defaultChecked` prop of `Checkbox` will not work.');
      }
    }
    checked = groupContext.value.includes(value);
    setChecked = checked => {
      var _a;
      if (checked) {
        groupContext.check(value);
      } else {
        groupContext.uncheck(value);
      }
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, checked);
    };
    disabled = disabled || groupContext.disabled;
  }
  (0, _react.useImperativeHandle)(ref, () => ({
    check: () => {
      setChecked(true);
    },
    uncheck: () => {
      setChecked(false);
    },
    toggle: () => {
      setChecked(!checked);
    }
  }));
  const renderIcon = () => {
    if (props.icon) {
      return _react.default.createElement("div", {
        className: `${classPrefix}-custom-icon`
      }, props.icon(checked, props.indeterminate));
    }
    return _react.default.createElement("div", {
      className: `${classPrefix}-icon`
    }, props.indeterminate ? _react.default.createElement(_indeterminateIcon.IndeterminateIcon, null) : checked && _react.default.createElement(_checkIcon.CheckIcon, null));
  };
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("label", {
    onClick: props.onClick,
    className: (0, _classnames.default)(classPrefix, {
      [`${classPrefix}-checked`]: checked && !props.indeterminate,
      [`${classPrefix}-indeterminate`]: props.indeterminate,
      [`${classPrefix}-disabled`]: disabled,
      [`${classPrefix}-block`]: props.block
    })
  }, _react.default.createElement(_nativeInput.NativeInput, {
    type: 'checkbox',
    checked: checked,
    onChange: setChecked,
    disabled: disabled,
    id: props.id
  }), renderIcon(), props.children && _react.default.createElement("div", {
    className: `${classPrefix}-content`
  }, props.children)));
});