import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, ContentChild, ViewChild, HostBinding, Output } from '@angular/core';
import { BehaviorSubject, Subject, merge, fromEvent } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { ApiBase } from '@ironsource/fusion-ui/components/api-base';
import { ChipFilterTestIdModifiers } from '@ironsource/fusion-ui/entities';
import { TestIdsService } from '@ironsource/fusion-ui/services/test-ids';

var ChipTypeToClass;
(function (ChipTypeToClass) {
    ChipTypeToClass["UnRemoveAbleSelect"] = "fu-un-removable-filter";
    ChipTypeToClass["AddFilter"] = "fu-add-filter";
    ChipTypeToClass["RemoveAbleSelect"] = "fu-removable-filter";
    ChipTypeToClass["ChipFilter"] = "fu-chip-filter";
    ChipTypeToClass["ChipSelect"] = "fu-select-filter";
})(ChipTypeToClass || (ChipTypeToClass = {}));

const _c0 = ["ref"];
class ChipFilterBaseComponent {
    set configuration(value) {
        if (!!value) {
            this.id = value.id;
            this.disabled = value.disabled || false;
            this.mode = value.mode || 'static';
            this.close = value.close || false;
            this.maxWidth = value.maxWidth || 200;
            this.rightIcon = value.rightIcon;
            this.leftIcon = value.leftIcon;
        }
    }
    /** @internal */
    set isSelected(value) {
        this.selected = value ?? false;
        if (value) {
            this.chipSelectValue = {
                id: this.id,
                isSelected: this.selected
            };
        }
    }
    /** @internal */
    set isDynamicContent(value) {
        this.isDefaultContent = !value;
    }
    get testAttribute() {
        return this.testIdsService.getTestAttribute(this.testId, this.testIdChipFilterModifiers.CHIP_FILTER);
    }
    set maxWidth(width) {
        this._maxWidth = width;
    }
    get maxWidth() {
        return this._maxWidth;
    }
    set close(close) {
        this.isCloseIcon$.next(close);
        this.changeHostClass('closed-icon', close);
    }
    get close() {
        return this.isCloseIcon$.getValue();
    }
    set disabled(disabled) {
        this._disabled$.next(disabled);
        this.changeHostClass('fu-disabled', disabled);
    }
    get disabled() {
        return this._disabled$.getValue();
    }
    set mode(chipMode) {
        this.chipMode = chipMode;
        this.setChipType(this.selected);
    }
    get mode() {
        return this.chipMode;
    }
    set selected(selected) {
        this.isChipSelected = selected;
        this.changeHostClass('fu-selected', selected);
        this.setChipType(selected);
    }
    get selected() {
        return this.isChipSelected;
    }
    constructor(
    /** @internal */
    element, renderer, cdr, injector) {
        this.element = element;
        this.renderer = renderer;
        this.cdr = cdr;
        this.injector = injector;
        /** @internal */
        this.chipType$ = new BehaviorSubject(null);
        /** @internal */
        this.isCloseIcon$ = new BehaviorSubject(false);
        /** @internal */
        this.isDefaultContent = true;
        /** @internal */
        this.isRoleButton = false;
        /** @internal */
        this.placeholderChipV4Mode = false;
        /** @internal */
        this.testIdChipFilterModifiers = ChipFilterTestIdModifiers;
        /** @internal */
        this.testIdsService = this.injector.get(TestIdsService);
        this.onDestroy$ = new Subject();
        this.restListeners$ = new Subject();
        this.clearClickSubscription$ = merge(this.onDestroy$, this.restListeners$);
        this.isChipSelected = false;
        this.chipMode = 'static';
        this._disabled$ = new BehaviorSubject(false);
        /**
         * On dynamic filter close button clicked (filter removed)
         * @ignore
         * */
        this.chipRemove = new EventEmitter();
        this.selectedChange = new EventEmitter();
        /**
         * used for chip filter removed (not for consumer)
         * @internal
         */
        this.onRemove = new EventEmitter();
    }
    ngOnInit() {
        if (!this.apiBase && !this.disabled) {
            this.setClickListener();
        }
        if (this.apiBase) {
            this.apiBase.changeConfig(`${this.maxWidth}px`);
            this.apiBase.templateRef = this.ref;
            this.apiBase.isComponentDisabled$.next(this.disabled);
            this.apiBase.placeholderChipV4Mode = this.placeholderChipV4Mode;
        }
    }
    ngAfterViewInit() {
        this.setValueSelectedListener();
    }
    ngAfterContentInit() {
        this.defaultContent = this.apiBase?.contentTemplate;
    }
    ngOnDestroy() {
        this.onDestroy$.next();
        this.onDestroy$.complete();
        this.restListeners$.complete();
    }
    /** @internal */
    closeClicked($event) {
        $event.stopPropagation();
        this.selected = false;
        this.setChipType(this.selected);
        // internal using
        this.onRemove.emit({
            id: this.id,
            isSelected: this.selected
        });
        // external, for consumer
        this.chipRemove.emit(this.id);
        this.apiBase?.resetState$.next();
    }
    setValueSelectedListener() {
        this.apiBase
            ?.valueSelected()
            .pipe(takeUntil(this.onDestroy$))
            .subscribe((selected) => {
            if (selected?.isSelected) {
                this.selected = selected.isSelected;
                this.setChipType(this.selected);
                this.chipSelectValue = {
                    id: this.id,
                    ...selected
                };
                this.selectedChange.emit(this.chipSelectValue);
            }
            else {
                this.selected = selected?.isSelected;
                this.setChipType(this.selected);
                this.onRemove.emit({
                    id: this.id,
                    isSelected: this.selected
                });
            }
            this.cdr.detectChanges();
        });
    }
    setClickListener() {
        this.restListeners$.next();
        fromEvent(this.element.nativeElement, 'click')
            .pipe(takeUntil(this.clearClickSubscription$))
            .subscribe(_ => {
            this.selected = true;
            this.setChipType(this.selected);
            this.chipSelectValue = {
                id: this.id,
                isSelected: this.selected
            };
            this.selectedChange.emit(this.chipSelectValue);
        });
    }
    setChipType(hasValue) {
        if (this.chipType$.getValue()) {
            this.changeHostClass(ChipTypeToClass[this.chipType$.getValue()], false);
        }
        switch (this.mode) {
            case 'dynamic':
                this.setChipSelectType(hasValue);
                break;
            case 'static':
                this.setChipSelectType(hasValue);
                break;
            case 'add':
                this.chipType$.next('AddFilter');
                break;
        }
        this.changeHostClass(ChipTypeToClass[this.chipType$.getValue()], !!this.chipType$.getValue());
    }
    setChipSelectType(hasValue) {
        if (this.close === true) {
            this.chipType$.next(hasValue ? 'RemoveAbleSelect' : 'ChipFilter');
        }
        else {
            this.chipType$.next(hasValue ? 'UnRemoveAbleSelect' : 'ChipFilter');
        }
    }
    changeHostClass(className, add) {
        const classAction = add ? 'addClass' : 'removeClass';
        this.renderer[classAction](this.element.nativeElement, className);
    }
    static { this.ɵfac = function ChipFilterBaseComponent_Factory(t) { return new (t || ChipFilterBaseComponent)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.Renderer2), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef), i0.ɵɵdirectiveInject(i0.Injector)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: ChipFilterBaseComponent, contentQueries: function ChipFilterBaseComponent_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
            i0.ɵɵcontentQuery(dirIndex, ApiBase, 7);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.apiBase = _t.first);
        } }, viewQuery: function ChipFilterBaseComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 7);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.ref = _t.first);
        } }, hostVars: 1, hostBindings: function ChipFilterBaseComponent_HostBindings(rf, ctx) { if (rf & 2) {
            i0.ɵɵattribute("data-testid", ctx.testAttribute);
        } }, inputs: { testId: "testId", configuration: "configuration", isSelected: "isSelected", isDynamicContent: "isDynamicContent" }, outputs: { chipRemove: "chipRemove", selectedChange: "selectedChange" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ChipFilterBaseComponent, [{
        type: Directive
    }], () => [{ type: i0.ElementRef }, { type: i0.Renderer2 }, { type: i0.ChangeDetectorRef }, { type: i0.Injector }], { testId: [{
            type: Input
        }], apiBase: [{
            type: ContentChild,
            args: [ApiBase, { static: true }]
        }], ref: [{
            type: ViewChild,
            args: ['ref', { static: true }]
        }], configuration: [{
            type: Input
        }], isSelected: [{
            type: Input
        }], isDynamicContent: [{
            type: Input
        }], testAttribute: [{
            type: HostBinding,
            args: ['attr.data-testid']
        }], chipRemove: [{
            type: Output
        }], selectedChange: [{
            type: Output
        }] }); })();

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

export { ChipFilterBaseComponent, ChipTypeToClass };
//# sourceMappingURL=ironsource-fusion-ui-components-chip-filter-common-base.mjs.map
