'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var lodashEs = require('lodash-es');

const SEARCH_MARK = "__rc_cascader_search_mark__";
const defaultFilter = (search, options, { label }) => options.some((opt) => String(opt[label]).toLowerCase().includes(search.toLowerCase()));
const defaultRender = ({ path, fieldNames }) => path.map((opt) => opt[fieldNames.label]).join(" / ");
var useSearchOptions = (search, options, fieldNames, prefixCls, config, changeOnSelect) => {
  return vue.computed(() => {
    const { filter = defaultFilter, render = defaultRender, limit = 50, sort } = config.value;
    const filteredOptions = [];
    if (!search.value)
      return [];
    function dig(list, pathOptions) {
      list.forEach((option) => {
        if (!sort && lodashEs.isNumber(limit) && limit > 0 && filteredOptions.length >= limit)
          return;
        const connectedPathOptions = [...pathOptions, option];
        const children = option[fieldNames.value.children];
        if (
          // If is leaf option
          !children || children.length === 0 || changeOnSelect.value
        ) {
          if (filter(search.value, connectedPathOptions, { label: fieldNames.value.label })) {
            filteredOptions.push({
              ...option,
              [fieldNames.value.label]: render({
                inputValue: search.value,
                path: connectedPathOptions,
                prefixCls: prefixCls.value,
                fieldNames: fieldNames.value
              }),
              [SEARCH_MARK]: connectedPathOptions
            });
          }
        }
        if (children)
          dig(option[fieldNames.value.children], connectedPathOptions);
      });
    }
    dig(options.value, []);
    if (sort) {
      filteredOptions.sort((a, b) => {
        return sort(a[SEARCH_MARK], b[SEARCH_MARK], search.value, fieldNames.value);
      });
    }
    return lodashEs.isNumber(limit) && limit > 0 ? filteredOptions.slice(0, limit) : filteredOptions;
  });
};

exports.SEARCH_MARK = SEARCH_MARK;
exports.default = useSearchOptions;
//# sourceMappingURL=useSearchOptions.cjs.map
