import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output, ViewChild } from '@angular/core';
import * as i1 from '@ironsource/fusion-ui/services/log';
import { fromEvent } from 'rxjs';
import { take } from 'rxjs/operators';

const _c0 = ["toast"];
class ToastBaseComponent {
    set configuration(value) {
        this.text = value.text || '';
        this.type = value.type || null;
        this.iconName = !this.type && value.icon ? value.icon : this.setIconByType(this.type);
        this.image = !this.type && value.image ? `url(${value.image})` : null;
        this.hasIconHolder = !this.type && !this.image && !this.iconName;
        this.hasIcon = !!this.type || !!this.iconName;
        this.duration = !!value.duration ? value.duration * 1000 : null;
        this.location = value.location;
        this.locationClass = !this.shownByService ? this.location : '';
        this.custom = this.getCustomConfiguration(value);
    }
    constructor(logService, renderer) {
        this.logService = logService;
        this.renderer = renderer;
        this.toastClosed = new EventEmitter();
        /** @internal */
        this.shownByService = false; // util set from service
        /** @internal */
        this.hasIconHolder = true;
        /** @internal */
        this.hasIcon = true;
    }
    /** @internal */
    onCloseClicked() {
        this.closeToast();
    }
    ngOnInit() {
        if (this.duration) {
            setTimeout(this.closeToast.bind(this), this.duration);
        }
    }
    ngAfterViewInit() {
        this.renderer.addClass(this.toastView.nativeElement, this.getShownTransition());
    }
    closeToast() {
        this.setCloseTransition();
        fromEvent(this.toastView.nativeElement, 'animationend')
            .pipe(take(1))
            .subscribe(data => {
            this.toastClosed.emit();
        });
    }
    setIconByType(type) {
        let iconName;
        switch (type) {
            case 'success':
                iconName = { iconName: 'check', iconVersion: 'v2' };
                break;
            case 'alert':
                iconName = { iconName: 'bullhorn', iconVersion: 'v2' };
                break;
            case 'error':
            case 'warning':
                iconName = { iconName: 'warning-note', iconVersion: 'v2' };
                break;
        }
        return iconName || '';
    }
    getCustomConfiguration(toastConfig) {
        const isCustom = toastConfig?.custom?.component || toastConfig?.custom?.element || toastConfig?.custom?.htmlSnippet;
        if (isCustom) {
            if (!!toastConfig.text || !!toastConfig.type || !!toastConfig.icon || !!toastConfig.image) {
                this.logService.error(new Error(`Can't use predefined and custom toast content same time. Custom content will be ignored.`));
            }
            return toastConfig.custom;
        }
        return null;
    }
    setCloseTransition() {
        let onCloseClass = 'fu-fadeout'; // default and center;
        if (this.location) {
            if (this.location.endsWith('-right')) {
                onCloseClass = 'fu-slide-out-right';
            }
            else if (this.location.endsWith('-left')) {
                onCloseClass = 'fu-slide-out-left';
            }
        }
        if (onCloseClass !== 'fu-fadeout' && this.shownByService) {
            // in case toast opened from service and has slide animation
            this.renderer.setStyle(this.toastView.nativeElement, 'position', 'relative');
        }
        // set on close class with animation
        this.renderer.addClass(this.toastView.nativeElement, onCloseClass);
    }
    getShownTransition() {
        return this.getShownTransitionByLocation(this.location);
    }
    getShownTransitionByLocation(location) {
        if (!!location && location.startsWith('top-')) {
            return 'fu-top-fadein';
        }
        else if (!!location && location.startsWith('bottom-')) {
            return 'fu-bottom-fadein';
        }
        else {
            return 'fu-fadein';
        }
    }
    static { this.ɵfac = function ToastBaseComponent_Factory(t) { return new (t || ToastBaseComponent)(i0.ɵɵdirectiveInject(i1.LogService), i0.ɵɵdirectiveInject(i0.Renderer2)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: ToastBaseComponent, viewQuery: function ToastBaseComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 5);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.toastView = _t.first);
        } }, inputs: { configuration: "configuration" }, outputs: { toastClosed: "toastClosed" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ToastBaseComponent, [{
        type: Directive
    }], () => [{ type: i1.LogService }, { type: i0.Renderer2 }], { configuration: [{
            type: Input
        }], toastClosed: [{
            type: Output
        }], toastView: [{
            type: ViewChild,
            args: ['toast']
        }] }); })();

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

export { ToastBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-toast-common-base.mjs.map
