import { TAG_MAX_LENGTH } from './TagsManagementState';
import { action, makeObservable, observable, computed } from 'mobx';
export class TagsSearchState {
    constructor({ tagsManagementState }) {
        this.searchValue = '';
        this.setSearchValue = (newSearchValue) => {
            this.searchValue = newSearchValue;
            if (this.tagsManagementState.shouldAllowTagsPagination) {
                this.tagsManagementState.collection.search(newSearchValue.substring(0, TAG_MAX_LENGTH));
            }
        };
        this.tagsManagementState = tagsManagementState;
        makeObservable(this, {
            searchValue: observable,
            setSearchValue: action,
            items: computed,
            hasSameTagAsSearch: computed,
        });
    }
    get items() {
        if (this.tagsManagementState.shouldAllowTagsPagination) {
            return this.tagsManagementState.items;
        }
        if (!this.searchValue) {
            return this.tagsManagementState.items;
        }
        const filtered = this.tagsManagementState.items.filter((tag) => tag.name.toLowerCase().includes(this.searchValue.toLowerCase()));
        if (filtered.length === 0 && this.searchValue.length > TAG_MAX_LENGTH) {
            const truncated = this.tagsManagementState.items.find((tag) => tag.name.toLowerCase() ===
                this.searchValue.substring(0, TAG_MAX_LENGTH).toLowerCase());
            if (truncated) {
                return [truncated];
            }
        }
        return filtered;
    }
    get hasSameTagAsSearch() {
        const term = this.searchValue.substring(0, TAG_MAX_LENGTH).toLowerCase();
        return this.tagsManagementState.items.some((tag) => tag.name.toLowerCase() === term);
    }
}
//# sourceMappingURL=TagsSearchState.js.map