import React__default from "react";
import get_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/get.js";
import Option from "./option.js";
import { isArray, isObject, isString, isNumber } from "../_util/is.js";
import getHighlightText from "../_util/getHighlightText.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
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);
};
function preventDefaultEvent(e) {
  e && e.preventDefault();
}
function isEmptyValue(value, isMultiple) {
  return isMultiple ? !isArray(value) || !value.length : value === void 0;
}
function getValidValue(value, isMultiple, labelInValue) {
  if (labelInValue) {
    if (isMultiple) {
      value = Array.isArray(value) ? value.map(function(item) {
        return isObject(item) && "label" in item ? item.value : item;
      }) : value;
    } else {
      value = isObject(value) && "label" in value ? value.value : value;
    }
  }
  return isEmptyValue(value, isMultiple) ? isMultiple ? Array.isArray(value) ? value : [] : void 0 : value;
}
function isSelectOption(child) {
  return get_1(child, "props.isSelectOption");
}
function isSelectOptGroup(child) {
  return get_1(child, "props.isSelectOptGroup");
}
function flatChildren(_a, _b, _c) {
  var children = _a.children, options = _a.options, filterOption = _a.filterOption;
  var _d = _b.inputValue, inputValue = _d === void 0 ? "" : _d, userCreatedOptions = _b.userCreatedOptions, userCreatingOption = _b.userCreatingOption, prefixCls = _b.prefixCls;
  var _e = _c === void 0 ? {} : _c, _f = _e.optionInfoMap, optionInfoMap = _f === void 0 ? /* @__PURE__ */ new Map() : _f, _g = _e.optionValueList, optionValueList = _g === void 0 ? [] : _g, _h = _e.customNodeCount, customNodeCount = _h === void 0 ? 0 : _h;
  var hasOptGroup = false;
  var hasComplexLabelInOptions = false;
  var childrenList = [];
  var optionIndexListForArrowKey = [];
  var getChildValue = function(child) {
    var propValue = get_1(child, "props.value");
    var propChildren = get_1(child, "props.children");
    return propValue === void 0 && propChildren !== null && propChildren !== void 0 ? propChildren.toString() : propValue;
  };
  var getChildKey = function(_a2, key, isGroupTitle) {
    var label = _a2.label, value = _a2.value;
    if (!label && !value && !key) {
      customNodeCount++;
      return "custom_node_" + customNodeCount;
    }
    return isGroupTitle ? key || "group_" + label : key || typeof value + "_" + value || label + "_" + optionInfoMap.size;
  };
  var handleOption = function(child, origin) {
    var optionValue = getChildValue(child);
    var isValidOption = true;
    if (filterOption === true) {
      isValidOption = optionValue !== void 0 && String(optionValue).toLowerCase().indexOf(inputValue.toLowerCase()) !== -1;
    } else if (typeof filterOption === "function") {
      isValidOption = !inputValue || filterOption(inputValue, child);
    }
    if (!optionInfoMap.get(optionValue)) {
      if (!("_key" in child.props)) {
        child = React__default.cloneElement(child, {
          _key: getChildKey(child.props, child.key)
        });
      }
      var index = optionInfoMap.size;
      var option = __assign(__assign({
        child
      }, child.props), {
        value: optionValue,
        _index: index,
        _origin: origin,
        _valid: isValidOption
      });
      optionInfoMap.set(optionValue, option);
      optionValueList.push(optionValue);
      if (isValidOption) {
        childrenList.push(child);
        if (!option.disabled) {
          optionIndexListForArrowKey.push(index);
        }
      }
    }
    if (typeof child.props.children !== "string") {
      hasComplexLabelInOptions = true;
    }
  };
  var extendChildren = function(arr, origin) {
    if (origin && isArray(arr) && arr.length) {
      arr.forEach(function(option) {
        option = isString(option) || isNumber(option) ? {
          label: option,
          value: option
        } : option;
        var child = /* @__PURE__ */ jsx(Option, {
          _key: getChildKey(option),
          value: option.value,
          disabled: option.disabled === true,
          extra: option.extra,
          children: option.label
        });
        handleOption(child, origin);
      });
    }
  };
  if (userCreatingOption) {
    extendChildren([userCreatingOption], "userCreatingOption");
  }
  if (children) {
    React__default.Children.map(children, function(child) {
      if (isSelectOptGroup(child)) {
        var _a2 = child.props, children_1 = _a2.children, options_1 = _a2.options;
        var _b2 = flatChildren({
          children: children_1,
          options: options_1,
          filterOption
        }, {
          inputValue,
          prefixCls
        }, {
          optionInfoMap,
          optionValueList,
          customNodeCount
        }), _childrenList = _b2.childrenList, _optionIndexListForArrowKey = _b2.optionIndexListForArrowKey, _hasComplexLabelInOptions = _b2.hasComplexLabelInOptions;
        if (_childrenList.length) {
          childrenList.push(React__default.cloneElement(child, {
            children: null,
            _key: getChildKey(child.props, child.key, true)
          }));
          childrenList = childrenList.concat(_childrenList);
          optionIndexListForArrowKey = optionIndexListForArrowKey.concat(_optionIndexListForArrowKey);
          hasOptGroup = true;
          hasComplexLabelInOptions = hasComplexLabelInOptions || _hasComplexLabelInOptions;
        }
      } else if (isSelectOption(child)) {
        handleOption(child, "children");
      } else if (isObject(child) && child.props) {
        childrenList.push(React__default.cloneElement(child, {
          _key: getChildKey(child.props, child.key)
        }));
      }
    });
  }
  extendChildren(options, "options");
  extendChildren(userCreatedOptions, "userCreatedOptions");
  return {
    childrenList: getHighlightText({
      nodeList: childrenList,
      pattern: inputValue,
      highlightClassName: prefixCls + "-highlight"
    }),
    optionInfoMap,
    optionValueList,
    optionIndexListForArrowKey,
    hasOptGroup,
    hasComplexLabelInOptions
  };
}
export {
  flatChildren,
  getValidValue,
  isEmptyValue,
  isSelectOptGroup,
  isSelectOption,
  preventDefaultEvent
};
//# sourceMappingURL=utils.js.map
