"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const React = require("react");
const get = require("../../../../../../lodash@4.17.21/node_modules/lodash/get.js");
const option = require("./option.js");
const is = require("../_util/is.js");
const getHighlightText = require("../_util/getHighlightText.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);
};
function preventDefaultEvent(e) {
  e && e.preventDefault();
}
function isEmptyValue(value, isMultiple) {
  return isMultiple ? !is.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 is.isObject(item) && "label" in item ? item.value : item;
      }) : value;
    } else {
      value = is.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(child, "props.isSelectOption");
}
function isSelectOptGroup(child) {
  return get(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(child, "props.value");
    var propChildren = get(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.default.cloneElement(child, {
          _key: getChildKey(child.props, child.key)
        });
      }
      var index = optionInfoMap.size;
      var option2 = __assign(__assign({
        child
      }, child.props), {
        value: optionValue,
        _index: index,
        _origin: origin,
        _valid: isValidOption
      });
      optionInfoMap.set(optionValue, option2);
      optionValueList.push(optionValue);
      if (isValidOption) {
        childrenList.push(child);
        if (!option2.disabled) {
          optionIndexListForArrowKey.push(index);
        }
      }
    }
    if (typeof child.props.children !== "string") {
      hasComplexLabelInOptions = true;
    }
  };
  var extendChildren = function(arr, origin) {
    if (origin && is.isArray(arr) && arr.length) {
      arr.forEach(function(option$1) {
        option$1 = is.isString(option$1) || is.isNumber(option$1) ? {
          label: option$1,
          value: option$1
        } : option$1;
        var child = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(option, {
          _key: getChildKey(option$1),
          value: option$1.value,
          disabled: option$1.disabled === true,
          extra: option$1.extra,
          children: option$1.label
        });
        handleOption(child, origin);
      });
    }
  };
  if (userCreatingOption) {
    extendChildren([userCreatingOption], "userCreatingOption");
  }
  if (children) {
    React__default.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.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 (is.isObject(child) && child.props) {
        childrenList.push(React__default.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
  };
}
exports.flatChildren = flatChildren;
exports.getValidValue = getValidValue;
exports.isEmptyValue = isEmptyValue;
exports.isSelectOptGroup = isSelectOptGroup;
exports.isSelectOption = isSelectOption;
exports.preventDefaultEvent = preventDefaultEvent;
//# sourceMappingURL=utils.js.map
