import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import { isUndefined, isNullOrUndefined, isString } from '@ironsource/fusion-ui/utils';
import { Subject } from 'rxjs';

class DropdownService {
    constructor() {
        this.closeAllSubject$ = new Subject();
        this.closeAll$ = this.closeAllSubject$.asObservable();
    }
    setOptionsState(options, selectedOptions) {
        let parentOption;
        const checkOption = (option) => {
            if (Array.isArray(option.childOptions)) {
                const wasChildSelected = option.childOptions.find(item => item.isSelected);
                option.childOptions = option.childOptions.map(checkOption);
                const curChildSelected = option.childOptions.find(item => item.isSelected);
                option.isOpen = !isUndefined(curChildSelected);
                if (option.isOpen) {
                    parentOption = option;
                }
                if (wasChildSelected !== curChildSelected) {
                    option = { ...option };
                }
            }
            else {
                const wasSelected = option.isSelected;
                Object.assign(option, {
                    isSelected: this.isOptionInSelected(option, selectedOptions)
                });
                if (option.isSelected && !wasSelected) {
                    option = { ...option };
                }
            }
            return option;
        };
        return { options: options.map(checkOption), parentOption };
    }
    isOptionInSelected(option, selected) {
        return selected.length > 0 && selected.some(item => (option.id && item.id && item.id === option.id) || item === option);
    }
    mapGroupedOptions(options) {
        return options.reduce((acc, item) => {
            if (item.childOptions) {
                acc.push({
                    id: item.id,
                    isGroup: true,
                    title: item.title,
                    displayText: item.displayText
                }, ...item.childOptions);
            }
            else {
                acc.push(item);
            }
            return acc;
        }, []);
    }
    optionToString(option, mappingOptions, { dropdownType, lastSearchValue } = {}, withoutHTMLInString = false) {
        if (isNullOrUndefined(option)) {
            return '';
        }
        if (dropdownType === 'tags') {
            return option.displayText ? option.displayText : option.title;
        }
        else {
            let optionToString;
            if (isString(option)) {
                optionToString = option;
            }
            else if (option?.displayText) {
                optionToString = option.displayText;
            }
            else {
                mappingOptions = mappingOptions ? mappingOptions : { id: 'id', title: 'title' };
                if (withoutHTMLInString) {
                    return `${option[mappingOptions.id]} ${option[mappingOptions?.title]}`;
                }
                else {
                    // eslint-disable-next-line
                    return `<span class="is-id">${option[mappingOptions?.id]}</span> <span class="is-title">${this.highlightSearchTerm(option[mappingOptions?.title], lastSearchValue)}</span>`;
                }
            }
            return this.highlightSearchTerm(optionToString, lastSearchValue);
        }
    }
    highlightSearchTerm(stringToShow, lastSearchValue) {
        if (lastSearchValue) {
            try {
                // regex escape (https://makandracards.com/makandra/15879-javascript-how-to-generate-a-regular-expression-from-a-string)
                const searchRegExp = new RegExp(`${lastSearchValue.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}(?![^<>]*(([\\/\\"']|]]|\\b)>))`, 'gm');
                stringToShow = stringToShow.replace(searchRegExp, `<span class='is-keyword'>${lastSearchValue}</span>`);
            }
            catch (e) {
                // nothing to do;
            }
        }
        return stringToShow;
    }
    emitCloseAllEvent(event) {
        this.closeAllSubject$.next(event);
    }
    filterEmptyGroupTitle(options) {
        return options.filter((item, idx, initial) => {
            return !(item.isGroup &&
                (isNullOrUndefined(initial[idx + 1]) || (!isNullOrUndefined(initial[idx + 1]) && initial[idx + 1].isGroup)));
        });
    }
    static { this.ɵfac = function DropdownService_Factory(t) { return new (t || DropdownService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: DropdownService, factory: DropdownService.ɵfac, providedIn: null }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(DropdownService, [{
        type: Injectable,
        args: [{
                providedIn: null
            }]
    }], () => [], null); })();

/**
 * Generated bundle index. Do not edit.
 */

export { DropdownService };
//# sourceMappingURL=ironsource-fusion-ui-components-dropdown-service.mjs.map
