'use strict';

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

var vue = require('vue');
var commonUtil = require('../utils/commonUtil.cjs');
var valueUtil = require('../utils/valueUtil.cjs');

function includes(test, search) {
  return commonUtil.toArray(test).join("").toUpperCase().includes(search);
}
var useFilterOptions = (options, fieldNames, searchValue, filterOption, optionFilterProp) => vue.computed(() => {
  const searchValueVal = searchValue.value;
  const optionFilterPropValue = optionFilterProp == null ? void 0 : optionFilterProp.value;
  const filterOptionValue = filterOption == null ? void 0 : filterOption.value;
  if (!searchValueVal || filterOptionValue === false)
    return options.value;
  const { options: fieldOptions, label: fieldLabel, value: fieldValue } = fieldNames.value;
  const filteredOptions = [];
  const customizeFilter = typeof filterOptionValue === "function";
  const upperSearch = searchValueVal.toUpperCase();
  const filterFunc = customizeFilter ? filterOptionValue : (_, option) => {
    if (optionFilterPropValue)
      return includes(option[optionFilterPropValue], upperSearch);
    if (option[fieldOptions]) {
      return includes(option[fieldLabel !== "children" ? fieldLabel : "label"], upperSearch);
    }
    return includes(option[fieldValue], upperSearch);
  };
  const wrapOption = customizeFilter ? (opt) => valueUtil.injectPropsWithOption(opt) : (opt) => opt;
  options.value.forEach((item) => {
    if (item[fieldOptions]) {
      const matchGroup = filterFunc(searchValueVal, wrapOption(item));
      if (matchGroup) {
        filteredOptions.push(item);
      } else {
        const subOptions = item[fieldOptions].filter(
          (subItem) => filterFunc(searchValueVal, wrapOption(subItem))
        );
        if (subOptions.length) {
          filteredOptions.push({
            ...item,
            [fieldOptions]: subOptions
          });
        }
      }
      return;
    }
    if (filterFunc(searchValueVal, wrapOption(item)))
      filteredOptions.push(item);
  });
  return filteredOptions;
});

exports.default = useFilterOptions;
//# sourceMappingURL=useFilterOptions.cjs.map
