/* eslint-disable @typescript-eslint/no-base-to-string */
// #region Imports
import { AutoCompleteTextSearchMode } from '../Types/AutoCompleteTextSearchMode';
import { ControllerBase } from './Abstracts/ControllerBase';
/**
 * Represents the `AutoCompleteController` class.
 *
 * @public
 */
export class AutoCompleteController extends ControllerBase {
    // #region Fields
    _filterCallback;
    _selectCallback;
    _filterBehavior;
    _searchText;
    _filteredItems;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `AutoCompleteController` class.
     *
     * @public
     */
    constructor(host, config) {
        super(host);
        this._filterCallback = config?.filterCallback ?? null;
        this._selectCallback = config?.selectCallback ?? null;
        this._filterBehavior = config?.filterBehavior ?? 'full';
        this._filteredItems = [];
        this._searchText = '';
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    hostUpdated() {
        // Logic that runs when the host component updates
    }
    // Public updateItems(items: Array<T>, minimumPrefixLength: number, mode: AutoCompleteTextSearchMode, displayMemberPath: string): void {
    //     This._items = items;
    //     This.filterItems(minimumPrefixLength, mode, displayMemberPath);
    // }
    updateSearchText(items, text, minimumPrefixLength, mode, displayMemberPath) {
        this._searchText = text;
        this.filterItems(items, minimumPrefixLength, mode, displayMemberPath);
    }
    selectItem(item) {
        this._selectCallback?.(item);
    }
    get filteredItems() {
        return [...this._filteredItems];
    }
    getDisplayValue(item, displayMemberPath) {
        const value = displayMemberPath ? getMemberValue(item, displayMemberPath) : item;
        if (value === null || value === undefined) {
            return '';
        }
        return String(value);
    }
    filterItems(items, minimumPrefixLength, mode, displayMemberPath) {
        if (this._searchText.length >= minimumPrefixLength) {
            const predicate = AutoCompleteTextSearchMode.getPredicate(mode);
            this._filteredItems = items.filter((item) => {
                const displayValue = this.getDisplayValue(item, displayMemberPath);
                return predicate(this._searchText, displayValue);
            });
        }
        else if (this._filterBehavior === 'empty') {
            this._filteredItems = [];
        }
        else {
            this._filteredItems = items;
        }
        this._filterCallback?.(this._filteredItems);
    }
}
function getMemberValue(self, memberKey) {
    if (!memberKey) {
        return self;
    }
    return memberKey
        .split('.')
        .reduce((accumulator, key) => {
        if (accumulator === null || accumulator === undefined) {
            return undefined;
        }
        if (typeof accumulator !== 'object') {
            return undefined;
        }
        return accumulator[key];
    }, self);
}
//# sourceMappingURL=AutoCompleteController.js.map