"use strict";
const React = require("react");
const group = require("./group.js");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const useCheckbox = require("./useCheckbox.js");
const useMergeValue = require("../_util/hooks/useMergeValue.js");
const omit = require("../_util/omit.js");
const iconHover = require("../_class/icon-hover.js");
const iconCheck = require("./icon-check.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const is = require("../_util/is.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
function Checkbox(baseProps, ref) {
  var _a;
  var inputRef = React.useRef(null);
  var _b = React.useContext(index.ConfigContext), getPrefixCls = _b.getPrefixCls, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var props = useMergeProps(baseProps, {}, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Checkbox);
  var context = React.useContext(group.CheckboxGroupContext);
  var prefixCls = getPrefixCls("checkbox");
  var onGroupChange = context.onGroupChange;
  var mergeProps = __assign({}, props);
  if (context.isCheckboxGroup) {
    mergeProps.checked = context.checkboxGroupValue.indexOf(props.value) !== -1;
    mergeProps.disabled = "disabled" in props ? props.disabled : context.disabled;
  }
  var disabled = mergeProps.disabled, children = mergeProps.children, className = mergeProps.className, value = mergeProps.value, style = mergeProps.style, indeterminate = mergeProps.indeterminate, error = mergeProps.error, rest = __rest(mergeProps, ["disabled", "children", "className", "value", "style", "indeterminate", "error"]);
  var _c = __read(useMergeValue(false, {
    value: mergeProps.checked,
    defaultValue: mergeProps.defaultChecked
  }), 2), checked = _c[0], setChecked = _c[1];
  var classNames$1 = classNames(prefixCls, (_a = {}, _a[prefixCls + "-disabled"] = !!disabled, _a[prefixCls + "-indeterminate"] = !!indeterminate, _a[prefixCls + "-checked"] = checked, _a[prefixCls + "-rtl"] = rtl, _a.error = error, _a), className);
  React.useEffect(function() {
    context.registerValue(value);
    return function() {
      context.unRegisterValue(value);
    };
  }, [value]);
  var onChange = React.useCallback(function(e) {
    e.persist();
    e.stopPropagation();
    setChecked(e.target.checked);
    if (context.isCheckboxGroup) {
      onGroupChange && onGroupChange(props.value, e.target.checked, e);
    }
    props.onChange && props.onChange(e.target.checked, e);
  }, [onGroupChange, context.isCheckboxGroup, props.onChange, props.value]);
  var onLabelClick = React__default.default.useCallback(function(e) {
    if (is.isFunction(props.children)) {
      e.preventDefault();
      inputRef.current && inputRef.current.click();
    }
    rest.onClick && rest.onClick(e);
  }, [props.children, rest.onClick]);
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("label", {
    ...__assign({
      ref,
      "aria-disabled": disabled
    }, omit(rest, ["onChange"]), {
      onClick: onLabelClick,
      className: classNames$1,
      style
    }),
    children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("input", {
      value,
      disabled: !!disabled,
      ref: inputRef,
      checked: !!checked,
      onChange,
      onClick: function(e) {
        return e.stopPropagation();
      },
      type: "checkbox"
    }), is.isFunction(children) ? children({
      checked,
      indeterminate
    }) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(iconHover, {
        prefix: prefixCls,
        className: prefixCls + "-mask-wrapper",
        disabled: checked || disabled || indeterminate,
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
          className: prefixCls + "-mask",
          children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(iconCheck, {
            className: prefixCls + "-mask-icon"
          })
        })
      }), children && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
        className: prefixCls + "-text",
        children
      })]
    })]
  });
}
var CheckboxComponent = React__default.default.forwardRef(Checkbox);
CheckboxComponent.displayName = "Checkbox";
CheckboxComponent.Group = group.default;
CheckboxComponent.useCheckbox = useCheckbox;
const Checkbox$1 = CheckboxComponent;
module.exports = Checkbox$1;
//# sourceMappingURL=checkbox.js.map
