import '../../../../utils/index.mjs';
import { warning } from '../../../../utils/log.mjs';

function getKey(data, index) {
  const { key } = data;
  let value;
  if ("value" in data)
    ({ value } = data);
  if (key !== null && key !== void 0)
    return key;
  if (value !== void 0)
    return value;
  return `rc-index-key-${index}`;
}
function fillFieldNames(fieldNames, childrenAsData) {
  const { label, value, options } = fieldNames || {};
  return {
    label: label || (childrenAsData ? "children" : "label"),
    value: value || "value",
    options: options || "options"
  };
}
function flattenOptions(options, { fieldNames, childrenAsData } = {}) {
  const flattenList = [];
  const {
    label: fieldLabel,
    value: fieldValue,
    options: fieldOptions
  } = fillFieldNames(fieldNames, false);
  function dig(list, isGroupOption) {
    list.forEach((data) => {
      const label = data[fieldLabel];
      if (isGroupOption || !(fieldOptions in data)) {
        const value = data[fieldValue];
        flattenList.push({
          key: getKey(data, flattenList.length),
          groupOption: isGroupOption,
          data,
          label,
          value
        });
      } else {
        let grpLabel = label;
        if (grpLabel === void 0 && childrenAsData)
          grpLabel = data.label;
        flattenList.push({
          key: getKey(data, flattenList.length),
          group: true,
          data,
          label: grpLabel
        });
        dig(data[fieldOptions], true);
      }
    });
  }
  dig(options, false);
  return flattenList;
}
function injectPropsWithOption(option) {
  const newOption = { ...option };
  if (!("props" in newOption)) {
    Object.defineProperty(newOption, "props", {
      get() {
        warning(
          false,
          "Return type is option instead of Option instance. Please read value directly instead of reading from `props`."
        );
        return newOption;
      }
    });
  }
  return newOption;
}
function getSeparatedContent(text, tokens) {
  if (!tokens || !tokens.length)
    return null;
  let match = false;
  function separate(str, [token, ...restTokens]) {
    if (!token)
      return [str];
    const list2 = str.split(token);
    match = match || list2.length > 1;
    return list2.reduce((prevList, unitStr) => [...prevList, ...separate(unitStr, restTokens)], []).filter((unit) => unit);
  }
  const list = separate(text, tokens);
  return match ? list : null;
}

export { fillFieldNames, flattenOptions, getSeparatedContent, injectPropsWithOption };
//# sourceMappingURL=valueUtil.mjs.map
