import { cf as isArrayLike, cg as isArray, ch as isBuffer, ci as isTypedArray, cj as isArguments, ck as getTag, cl as isPrototype, cm as baseKeys, bU as _slicedToArray, b$ as _objectWithoutProperties, bW as _defineProperty, c0 as _extends, bV as _objectSpread2, c3 as ConfigContext, cn as _default, aA as jsxRuntimeExports, co as CompoundedSpace, c2 as _toConsumableArray, cp as DisabledContext, cq as FormItemInputContext } from './MapEditView-67426f04.js';
import classNames from 'classnames';
import * as React from 'react';
import { forwardRef, useRef, useImperativeHandle } from 'react';
import { S as Spin } from './Spin-91879212.js';

/** `Object#toString` result references. */
var mapTag = '[object Map]',
    setTag = '[object Set]';

/** Used for built-in method references. */
var objectProto = Object.prototype;

/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;

/**
 * Checks if `value` is an empty object, collection, map, or set.
 *
 * Objects are considered empty if they have no own enumerable string keyed
 * properties.
 *
 * Array-like values such as `arguments` objects, arrays, buffers, strings, or
 * jQuery-like collections are considered empty if they have a `length` of `0`.
 * Similarly, maps and sets are considered empty if they have a `size` of `0`.
 *
 * @static
 * @memberOf _
 * @since 0.1.0
 * @category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is empty, else `false`.
 * @example
 *
 * _.isEmpty(null);
 * // => true
 *
 * _.isEmpty(true);
 * // => true
 *
 * _.isEmpty(1);
 * // => true
 *
 * _.isEmpty([1, 2, 3]);
 * // => false
 *
 * _.isEmpty({ 'a': 1 });
 * // => false
 */
function isEmpty(value) {
  if (value == null) {
    return true;
  }
  if (isArrayLike(value) &&
      (isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
        isBuffer(value) || isTypedArray(value) || isArguments(value))) {
    return !value.length;
  }
  var tag = getTag(value);
  if (tag == mapTag || tag == setTag) {
    return !value.size;
  }
  if (isPrototype(value)) {
    return !baseKeys(value).length;
  }
  for (var key in value) {
    if (hasOwnProperty.call(value, key)) {
      return false;
    }
  }
  return true;
}

function useEvent(callback) {
  var fnRef = React.useRef();
  fnRef.current = callback;
  var memoFn = React.useCallback(function () {
    var _fnRef$current;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    return (_fnRef$current = fnRef.current) === null || _fnRef$current === void 0 ? void 0 : _fnRef$current.call.apply(_fnRef$current, [fnRef].concat(args));
  }, []);
  return memoFn;
}

function canUseDom() {
  return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
}

/**
 * Wrap `React.useLayoutEffect` which will not throw warning message in test env
 */
var useInternalLayoutEffect = process.env.NODE_ENV !== 'test' && canUseDom() ? React.useLayoutEffect : React.useEffect;
var useLayoutEffect = function useLayoutEffect(callback, deps) {
  var firstMountRef = React.useRef(true);
  useInternalLayoutEffect(function () {
    return callback(firstMountRef.current);
  }, deps);

  // We tell react that first mount has passed
  useInternalLayoutEffect(function () {
    firstMountRef.current = false;
    return function () {
      firstMountRef.current = true;
    };
  }, []);
};
var useLayoutUpdateEffect = function useLayoutUpdateEffect(callback, deps) {
  useLayoutEffect(function (firstMount) {
    if (!firstMount) {
      return callback();
    }
  }, deps);
};

/**
 * Same as React.useState but `setState` accept `ignoreDestroy` param to not to setState after destroyed.
 * We do not make this auto is to avoid real memory leak.
 * Developer should confirm it's safe to ignore themselves.
 */
function useSafeState(defaultValue) {
  var destroyRef = React.useRef(false);
  var _React$useState = React.useState(defaultValue),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    value = _React$useState2[0],
    setValue = _React$useState2[1];
  React.useEffect(function () {
    destroyRef.current = false;
    return function () {
      destroyRef.current = true;
    };
  }, []);
  function safeSetState(updater, ignoreDestroy) {
    if (ignoreDestroy && destroyRef.current) {
      return;
    }
    setValue(updater);
  }
  return [value, safeSetState];
}

/** We only think `undefined` is empty */
function hasValue(value) {
  return value !== undefined;
}

/**
 * Similar to `useState` but will use props value if provided.
 * Note that internal use rc-util `useState` hook.
 */
function useMergedState(defaultStateValue, option) {
  var _ref = option || {},
    defaultValue = _ref.defaultValue,
    value = _ref.value,
    onChange = _ref.onChange,
    postState = _ref.postState;

  // ======================= Init =======================
  var _useState = useSafeState(function () {
      if (hasValue(value)) {
        return value;
      } else if (hasValue(defaultValue)) {
        return typeof defaultValue === 'function' ? defaultValue() : defaultValue;
      } else {
        return typeof defaultStateValue === 'function' ? defaultStateValue() : defaultStateValue;
      }
    }),
    _useState2 = _slicedToArray(_useState, 2),
    innerValue = _useState2[0],
    setInnerValue = _useState2[1];
  var mergedValue = value !== undefined ? value : innerValue;
  var postMergedValue = postState ? postState(mergedValue) : mergedValue;

  // ====================== Change ======================
  var onChangeFn = useEvent(onChange);
  var _useState3 = useSafeState([mergedValue]),
    _useState4 = _slicedToArray(_useState3, 2),
    prevValue = _useState4[0],
    setPrevValue = _useState4[1];
  useLayoutUpdateEffect(function () {
    var prev = prevValue[0];
    if (innerValue !== prev) {
      onChangeFn(innerValue, prev);
    }
  }, [prevValue]);

  // Sync value back to `undefined` when it from control to un-control
  useLayoutUpdateEffect(function () {
    if (!hasValue(value)) {
      setInnerValue(value);
    }
  }, [value]);

  // ====================== Update ======================
  var triggerChange = useEvent(function (updater, ignoreDestroy) {
    setInnerValue(updater, ignoreDestroy);
    setPrevValue([mergedValue], ignoreDestroy);
  });
  return [postMergedValue, triggerChange];
}

var _excluded$2 = ["prefixCls", "className", "style", "checked", "disabled", "defaultChecked", "type", "title", "onChange"];
var Checkbox$2 = /*#__PURE__*/forwardRef(function (props, ref) {
  var _classNames;
  var _props$prefixCls = props.prefixCls,
    prefixCls = _props$prefixCls === void 0 ? 'rc-checkbox' : _props$prefixCls,
    className = props.className,
    style = props.style,
    checked = props.checked,
    disabled = props.disabled,
    _props$defaultChecked = props.defaultChecked,
    defaultChecked = _props$defaultChecked === void 0 ? false : _props$defaultChecked,
    _props$type = props.type,
    type = _props$type === void 0 ? 'checkbox' : _props$type,
    title = props.title,
    onChange = props.onChange,
    inputProps = _objectWithoutProperties(props, _excluded$2);
  var inputRef = useRef(null);
  var _useMergedState = useMergedState(defaultChecked, {
      value: checked
    }),
    _useMergedState2 = _slicedToArray(_useMergedState, 2),
    rawValue = _useMergedState2[0],
    setRawValue = _useMergedState2[1];
  useImperativeHandle(ref, function () {
    return {
      focus: function focus() {
        var _inputRef$current;
        (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
      },
      blur: function blur() {
        var _inputRef$current2;
        (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.blur();
      },
      input: inputRef.current
    };
  });
  var classString = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-checked"), rawValue), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
  var handleChange = function handleChange(e) {
    if (disabled) {
      return;
    }
    if (!('checked' in props)) {
      setRawValue(e.target.checked);
    }
    onChange === null || onChange === void 0 ? void 0 : onChange({
      target: _objectSpread2(_objectSpread2({}, props), {}, {
        type: type,
        checked: e.target.checked
      }),
      stopPropagation: function stopPropagation() {
        e.stopPropagation();
      },
      preventDefault: function preventDefault() {
        e.preventDefault();
      },
      nativeEvent: e.nativeEvent
    });
  };
  return /*#__PURE__*/React.createElement("span", {
    className: classString,
    title: title,
    style: style
  }, /*#__PURE__*/React.createElement("input", _extends({}, inputProps, {
    className: "".concat(prefixCls, "-input"),
    ref: inputRef,
    onChange: handleChange,
    disabled: disabled,
    checked: !!rawValue,
    type: type
  })), /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-inner")
  }));
});

var _excluded$1 = ["defaultValue", "children", "options", "prefixCls", "className", "rootClassName", "style", "onChange", "direction", "spaceProps"];
var GroupContext = /*#__PURE__*/React.createContext(null);
var InternalCheckboxGroup = function InternalCheckboxGroup(_ref, ref) {
  var defaultValue = _ref.defaultValue,
    children = _ref.children,
    _ref$options = _ref.options,
    options = _ref$options === void 0 ? [] : _ref$options,
    customizePrefixCls = _ref.prefixCls,
    className = _ref.className,
    rootClassName = _ref.rootClassName,
    style = _ref.style,
    onChange = _ref.onChange,
    _ref$direction = _ref.direction,
    direction = _ref$direction === void 0 ? 'horizontal' : _ref$direction,
    _ref$spaceProps = _ref.spaceProps,
    spaceProps = _ref$spaceProps === void 0 ? {} : _ref$spaceProps,
    restProps = _objectWithoutProperties(_ref, _excluded$1);
  var _React$useState = React.useState(restProps.value || defaultValue || []),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    value = _React$useState2[0],
    setValue = _React$useState2[1];
  var _React$useState3 = React.useState([]),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    registeredValues = _React$useState4[0],
    setRegisteredValues = _React$useState4[1];
  React.useEffect(function () {
    if ('value' in restProps) {
      setValue(restProps.value || []);
    }
  }, [restProps.value]);
  var getOptions = function getOptions() {
    return options.map(function (option) {
      if (typeof option === 'string' || typeof option === 'number') {
        return {
          label: option,
          value: option
        };
      }
      return option;
    });
  };
  var cancelValue = function cancelValue(val) {
    setRegisteredValues(function (prevValues) {
      return prevValues.filter(function (v) {
        return v !== val;
      });
    });
  };
  var registerValue = function registerValue(val) {
    setRegisteredValues(function (prevValues) {
      return [].concat(_toConsumableArray(prevValues), [val]);
    });
  };
  var toggleOption = function toggleOption(option) {
    var optionIndex = value.indexOf(option.value);
    var newValue = _toConsumableArray(value);
    if (optionIndex === -1) {
      newValue.push(option.value);
    } else {
      newValue.splice(optionIndex, 1);
    }
    if (!('value' in restProps)) {
      setValue(newValue);
    }
    var opts = getOptions();
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue.filter(function (val) {
      return registeredValues.includes(val);
    }).sort(function (a, b) {
      var indexA = opts.findIndex(function (opt) {
        return opt.value === a;
      });
      var indexB = opts.findIndex(function (opt) {
        return opt.value === b;
      });
      return indexA - indexB;
    }));
  };
  var _React$useContext = React.useContext(ConfigContext),
    getPrefixCls = _React$useContext.getPrefixCls;
  var prefixCls = getPrefixCls('checkbox', customizePrefixCls);
  var groupPrefixCls = "".concat(prefixCls, "-group");
  var domProps = _default(restProps, ['value', 'disabled']);
  if (options && options.length > 0) {
    children = /*#__PURE__*/jsxRuntimeExports.jsx(CompoundedSpace, _objectSpread2(_objectSpread2({
      direction: direction
    }, spaceProps), {}, {
      children: getOptions().map(function (option) {
        return /*#__PURE__*/jsxRuntimeExports.jsx(Checkbox$1, {
          disabled: 'disabled' in option ? option.disabled : restProps.disabled,
          value: option.value,
          checked: value.includes(option.value),
          onChange: option.onChange,
          className: "".concat(groupPrefixCls, "-item"),
          style: option.style,
          children: option.label
        }, option.value.toString());
      })
    }));
  }
  var context = {
    toggleOption: toggleOption,
    value: value,
    disabled: restProps.disabled,
    name: restProps.name,
    registerValue: registerValue,
    cancelValue: cancelValue
  };
  var classString = classNames(groupPrefixCls, className, rootClassName);
  return /*#__PURE__*/jsxRuntimeExports.jsx("div", _objectSpread2(_objectSpread2({
    className: classString,
    style: style
  }, domProps), {}, {
    ref: ref,
    children: /*#__PURE__*/jsxRuntimeExports.jsx(GroupContext.Provider, {
      value: context,
      children: children
    })
  }));
};
var CheckboxGroup = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(InternalCheckboxGroup));

var _excluded = ["prefixCls", "className", "rootClassName", "children", "indeterminate", "style", "onMouseEnter", "onMouseLeave", "skipGroup", "disabled", "textHighlight", "loading"];
var InternalCheckbox = function InternalCheckbox(_ref, ref) {
  var _ref2, _checkboxGroup$disabl, _classNames;
  var customizePrefixCls = _ref.prefixCls,
    className = _ref.className,
    rootClassName = _ref.rootClassName,
    children = _ref.children,
    indeterminate = _ref.indeterminate,
    style = _ref.style,
    onMouseEnter = _ref.onMouseEnter,
    onMouseLeave = _ref.onMouseLeave,
    skipGroup = _ref.skipGroup,
    customDisabled = _ref.disabled,
    textHighlight = _ref.textHighlight,
    loading = _ref.loading,
    restProps = _objectWithoutProperties(_ref, _excluded);
  var checkboxGroup = React.useContext(GroupContext);
  var disabled = React.useContext(DisabledContext);
  var _React$useContext = React.useContext(FormItemInputContext),
    isFormItemInput = _React$useContext.isFormItemInput;
  var mergedDisabled = (_ref2 = (_checkboxGroup$disabl = checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.disabled) !== null && _checkboxGroup$disabl !== void 0 ? _checkboxGroup$disabl : customDisabled) !== null && _ref2 !== void 0 ? _ref2 : loading || disabled;
  var prevValue = React.useRef(restProps.value);
  React.useEffect(function () {
    checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.registerValue(restProps.value);
  }, []);
  React.useEffect(function () {
    if (skipGroup) {
      return;
    }
    if (restProps.value !== prevValue.current) {
      checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.cancelValue(prevValue.current);
      checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.registerValue(restProps.value);
      prevValue.current = restProps.value;
    }
    return function () {
      return checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.cancelValue(restProps.value);
    };
  }, [restProps.value]);
  var _React$useContext2 = React.useContext(ConfigContext),
    getPrefixCls = _React$useContext2.getPrefixCls;
  var prefixCls = getPrefixCls('checkbox', customizePrefixCls);
  var checkboxProps = _objectSpread2({}, restProps);
  if (checkboxGroup && !skipGroup) {
    checkboxProps.onChange = function () {
      if (restProps.onChange) {
        restProps.onChange.apply(restProps, arguments);
      }
      if (checkboxGroup.toggleOption) {
        checkboxGroup.toggleOption({
          label: children,
          value: restProps.value
        });
      }
    };
    checkboxProps.name = checkboxGroup.name;
    checkboxProps.checked = checkboxGroup.value.includes(restProps.value);
  }
  var classString = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-wrapper"), true), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-checked"), checkboxProps.checked), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-disabled"), mergedDisabled), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-loading"), loading), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-in-form-item"), isFormItemInput), _defineProperty(_classNames, "".concat(prefixCls, "-highlight"), textHighlight), _classNames), className, rootClassName);
  var checkboxClass = classNames(_defineProperty({}, "".concat(prefixCls, "-indeterminate"), indeterminate));
  var ariaChecked = indeterminate ? 'mixed' : undefined;
  return /*#__PURE__*/jsxRuntimeExports.jsxs("label", {
    className: classString,
    style: style,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    children: [/*#__PURE__*/jsxRuntimeExports.jsx(Checkbox$2, _objectSpread2(_objectSpread2({
      "aria-checked": ariaChecked
    }, checkboxProps), {}, {
      prefixCls: prefixCls,
      className: checkboxClass,
      disabled: mergedDisabled,
      ref: !isEmpty(ref) ? ref : undefined
    })), children !== undefined && /*#__PURE__*/jsxRuntimeExports.jsx("span", {
      children: children
    }), loading ? /*#__PURE__*/jsxRuntimeExports.jsx(Spin, {
      size: "small",
      brand: true,
      className: "".concat(prefixCls, "-loading")
    }) : null]
  });
};
var Checkbox$1 = InternalCheckbox;
Checkbox$1.defaultProps = {
  indeterminate: false,
  skipGroup: false
};
Checkbox$1.displayName = 'Checkbox';

var Checkbox = Checkbox$1;
Checkbox.Group = CheckboxGroup;

export { Checkbox as C };
