import React, { useMemo, useRef } from "react";
import classnames from "classnames";
import Icon from "../icon";
import { addUnit } from "../utils";
const Checker = (props) => {
  const iconRef = useRef(null);
  const getParentProp = (name) => {
    if (props.parent && props.bindGroup) {
      return props.parent.props[name];
    }
    return null;
  };
  const disabled = useMemo(() => getParentProp("disabled") || props.disabled, [props.parent, props.disabled]);
  const direction = useMemo(() => getParentProp("direction") || null, [props.parent]);
  const iconStyle = useMemo(() => {
    const checkedColor = props.checkedColor || getParentProp("checkedColor");
    if (checkedColor && props.checked && !disabled) {
      return {
        borderColor: checkedColor,
        backgroundColor: checkedColor
      };
    }
    return {};
  }, [props.checkedColor, props.checked, disabled]);
  const onClick = (event) => {
    const {
      target
    } = event;
    const icon = iconRef.current;
    const iconClicked = icon === target || icon.contains(target);
    if (!disabled && (iconClicked || !props.labelDisabled)) {
      if (props.onToggle) {
        props.onToggle();
      }
    }
    if (props.onClick) {
      props.onClick(event);
    }
  };
  const renderIcon = () => {
    const {
      bem,
      shape,
      checked
    } = props;
    const iconSize = props.iconSize || getParentProp("iconSize");
    return /* @__PURE__ */ React.createElement("div", {
      ref: iconRef,
      className: classnames(bem("icon", [shape, {
        disabled,
        checked
      }])),
      style: {
        fontSize: addUnit(iconSize)
      }
    }, props.iconRender ? props.iconRender({
      checked,
      disabled
    }) : /* @__PURE__ */ React.createElement(Icon, {
      name: "success",
      style: iconStyle
    }));
  };
  const renderLabel = () => {
    if (props.children) {
      return /* @__PURE__ */ React.createElement("span", {
        className: props.bem("label", [props.labelPosition, {
          disabled
        }])
      }, props.children);
    }
    return null;
  };
  return /* @__PURE__ */ React.createElement("div", {
    role: props.role,
    className: classnames(props.bem([{
      disabled,
      "label-disabled": props.labelDisabled
    }, direction]), props.className),
    tabIndex: disabled ? -1 : 0,
    "aria-checked": props.checked,
    onClick
  }, props.labelPosition === "left" && renderLabel(), renderIcon(), props.labelPosition !== "left" && renderLabel());
};
Checker.defaultProps = {
  shape: "round",
  bindGroup: true
};
var stdin_default = Checker;
export {
  stdin_default as default
};
