"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

var _icons = require("@react-vant/icons");

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

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

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 Checker = function Checker(props) {
  var iconRef = (0, _react.useRef)(null);

  var getParentProp = function getParentProp(name) {
    if (props.parent && props.bindGroup) {
      return props.parent.props[name];
    }

    return null;
  };

  var disabled = (0, _react.useMemo)(function () {
    return getParentProp('disabled') || props.disabled;
  }, [props.parent, props.disabled]);
  var direction = (0, _react.useMemo)(function () {
    return getParentProp('direction') || null;
  }, [props.parent]);
  var iconStyle = (0, _react.useMemo)(function () {
    var checkedColor = props.checkedColor || getParentProp('checkedColor');

    if (checkedColor && props.checked && !disabled) {
      return {
        borderColor: checkedColor,
        backgroundColor: checkedColor
      };
    }

    return {};
  }, [props.checkedColor, props.checked, disabled]);

  var onClick = function onClick(event) {
    var target = event.target;
    var icon = iconRef.current;
    var iconClicked = icon === target || icon.contains(target);

    if (!disabled && (iconClicked || !props.labelDisabled)) {
      if (props.onToggle) {
        props.onToggle();
      }
    }

    if (props.onClick) {
      props.onClick(event);
    }
  };

  var renderIcon = function renderIcon() {
    var bem = props.bem,
        shape = props.shape,
        checked = props.checked;
    var iconSize = props.iconSize || getParentProp('iconSize');
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: iconRef,
      className: (0, _clsx.default)(bem('icon', [shape, {
        disabled: disabled,
        checked: checked
      }])),
      style: {
        fontSize: (0, _utils.addUnit)(iconSize)
      }
    }, props.iconRender ? props.iconRender({
      checked: checked,
      disabled: disabled
    }) : /*#__PURE__*/_react.default.createElement(_icons.Success, {
      style: iconStyle
    }));
  };

  var renderLabel = function renderLabel() {
    if (props.children) {
      return /*#__PURE__*/_react.default.createElement("span", {
        className: props.bem('label', [props.labelPosition, {
          disabled: disabled
        }])
      }, props.children);
    }

    return null;
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    role: props.role,
    className: (0, _clsx.default)(props.bem([{
      disabled: disabled,
      'label-disabled': props.labelDisabled
    }, direction]), props.className),
    tabIndex: disabled ? -1 : 0,
    "aria-checked": props.checked,
    onClick: onClick
  }, props.labelPosition === 'left' && renderLabel(), renderIcon(), props.labelPosition !== 'left' && renderLabel());
};

Checker.defaultProps = {
  shape: 'round',
  bindGroup: true
};
var _default = Checker;
exports.default = _default;