var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { htmlSanitizer, labelSanitizeStrategy } from '../../../../utils/sanitize';
/**
 * The `VegaDropdownSearchController` class is a TypeScript class that provides methods for searching
 * and filtering dropdown items based on a search string.
 */
export class VegaDropdownSearchController extends VegaSlimmer {
  /**
   * The search function returns the source array if there is no search string, otherwise it searches the
   * nested list for matching items.
   *
   * @param {VegaDropdown['source']} source - The `source` parameter is of type `VegaDropdown['source']`, which means it is a
   * property of the `VegaDropdown` object. It is expected to be an array of `VegaDropdownSourceItem`
   * objects.
   * @returns {VegaDropdownSourceItem[]} an array of `VegaDropdownSourceItem` objects.
   */
  search(source) {
    return this.canSearch() ? this.searchSource(source, []) : source;
  }
  /**
   * The function `canSearch` returns true if the conditions for searching are met.
   *
   * @returns {boolean} The method `canSearch()` returns a boolean value based on the conditions specified in the
   * return statement. It returns `true` if `useDefaultFilter` is true, `searchable` is true, and
   * `searchString` is not an empty string; otherwise, it returns `false`.
   */
  canSearch() {
    return this.useDefaultFilter && !!this.searchString;
  }
  /**
   * The function `searchSource` recursively searches through a nested list of `VegaDropdownSourceItem`
   * objects, filtering out items that do not match a search string and modifying the labels of matching
   * items.
   *
   * @param {VegaDropdownSourceItem[]} source - An array of objects of type VegaDropdownSourceItem. Each
   * object represents a dropdown item and may have a "children" property which is an array of nested
   * dropdown items.
   * @param {VegaDropdownSourceItem[]} result - The `result` parameter is an array of
   * `VegaDropdownSourceItem` objects. It is used to store the filtered items that match the search
   * criteria.
   * @returns {VegaDropdownSourceItem[]} an array of VegaDropdownSourceItem objects.
   */
  searchSource(source, result) {
    const searchResult = this.remoteInvocationController.callRemoteSearch(source, this.searchString, this.caseSensitive);
    if (searchResult)
      return searchResult;
    source.forEach((item) => {
      item.label = htmlSanitizer.sanitize(item.label, labelSanitizeStrategy);
      if (item['children']) {
        const filtered = this.searchSource(item['children'], []);
        if (filtered.length > 0) {
          result.push(Object.assign(Object.assign({}, item), { children: filtered }));
        }
      }
      else if (this.isLabelIncludeSearchString(item.label)) {
        result.push(Object.assign(Object.assign({}, item), { label: this.replaceLabelBySearchString(item.label) }));
      }
    });
    return result;
  }
  /**
   * The function checks if a given label includes a search string, taking into account case sensitivity.
   *
   * @param {string} label - A string representing the label to be checked for inclusion of the search
   * string.
   * @returns {boolean} a boolean value.
   */
  isLabelIncludeSearchString(label) {
    return this.caseSensitive
      ? label.includes(this.searchString)
      : label.toLowerCase().includes(this.searchString.toLowerCase());
  }
  /**
   * The function replaces occurrences of a search string in a label with bolded text.
   *
   * @param {string} label - The `label` parameter is a string that represents the label to be modified.
   * @returns {string} a modified version of the input label string. The function uses the `replace` method with a
   * regular expression pattern to find occurrences of the `searchString` within the label. It then
   * replaces each occurrence with the same value wrapped in `<b>` tags. The modified label string is
   * then returned.
   */
  replaceLabelBySearchString(label) {
    const escaped = this.searchString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const flags = 'g' + (this.caseSensitive ? '' : 'i');
    return label.replace(new RegExp(`(${escaped})`, flags), `<b>$1</b>`);
  }
}
__decorate([
  MapToComponentField()
], VegaDropdownSearchController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchController.prototype, "searchString", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchController.prototype, "caseSensitive", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchController.prototype, "useDefaultFilter", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchController.prototype, "searchable", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchController.prototype, "remoteInvocationController", void 0);
