/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { escapeRegExp, forIn } from "es-toolkit/compat";
const filter = (data, value, filterProps) => {
  const escapedValue = escapeRegExp(value);
  const regex = new RegExp(escapedValue, "i");
  const matchAll = value === "";
  if (matchAll || data.length === 0) {
    return data;
  }
  const find = (input, RE, fields) => {
    if (input?.filterDisabled) {
      return true;
    }
    let found = false;
    forIn(input, (val, key) => {
      if (typeof val === "function" || val == null) {
        return;
      }
      if (fields && !fields.includes(key)) {
        return;
      }
      if (Array.isArray(val) || typeof val === "object" && val !== null) {
        if (find(val, RE)) {
          found = true;
        }
      } else if (RE.test(val)) {
        found = true;
      }
    });
    return found;
  };
  return data.filter((item) => find(item, regex, filterProps));
};
export {
  filter as f
};
