import * as tslib_1 from "tslib";
import { Component, ViewEncapsulation, Input, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
import { ToastComponent } from './toast';
import { ToastDataFilled } from './toast.config';
var ToastItemComponent = /** @class */ (function () {
    function ToastItemComponent(toastComponent, cd) {
        this.toastComponent = toastComponent;
        this.cd = cd;
        this.timer = null;
    }
    ToastItemComponent.prototype.ngOnInit = function () {
        this.options = this.toast.options;
        if (this.options.animate) {
            this.toast.state = 'enter';
            this.cd.markForCheck();
        }
        this.autoErase = this.options.duration > 0;
        if (this.autoErase) {
            this._startEraseTimeout();
        }
    };
    ToastItemComponent.prototype.ngOnDestroy = function () {
        if (this.autoErase) {
            this._clearEraseTimeout();
        }
    };
    // Remove self
    ToastItemComponent.prototype._destroy = function () {
        var _this = this;
        if (this.options.animate) {
            this.toast.state = 'leave';
            this.cd.markForCheck();
            setTimeout(function () { return _this.toastComponent.removeToast(_this.toast.toastId); }, 200);
        }
        else {
            this.toastComponent.removeToast(this.toast.toastId);
        }
    };
    ToastItemComponent.prototype._startEraseTimeout = function () {
        var _this = this;
        if (this.options.duration > 0) {
            this._clearEraseTimeout();
            this.timer = window.setTimeout(function () { return _this._destroy(); }, this.options.duration);
            this.cd.markForCheck();
        }
        else {
            this._destroy();
        }
    };
    ToastItemComponent.prototype._clearEraseTimeout = function () {
        if (this.timer !== null) {
            window.clearTimeout(this.timer);
            this.timer = null;
            this.cd.markForCheck();
        }
    };
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", ToastDataFilled)
    ], ToastItemComponent.prototype, "toast", void 0);
    ToastItemComponent = tslib_1.__decorate([
        Component({
            selector: 'nb-toast-item',
            template: "<div class=\"nb-toast-container\" [@enterLeave]=\"toast.state\"><div class=\"nb-toast-content\" [ngClass]=\"'nb-toast-' + toast.type\"><ng-container [ngSwitch]=\"toast.type\"><i *ngSwitchCase=\"'success'\" class=\"icon iconfont icon-check_c\"></i> <i *ngSwitchCase=\"'warn'\" class=\"icon iconfont icon-attention\"></i> <i *ngSwitchCase=\"'info'\" class=\"icon iconfont icon-info_c\"></i> <i *ngSwitchCase=\"'error'\" class=\"icon iconfont icon-close_c\"></i></ng-container><div class=\"nb-toast-message\">{{ toast.content }}</div></div></div>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false,
            animations: [
                trigger('enterLeave', [
                    state('enter', style({ opacity: 1, transform: 'translateY(0)' })),
                    transition('* => enter', [
                        style({ opacity: 0, transform: 'translateY(-50%)' }),
                        animate('200ms linear')
                    ]),
                    state('leave', style({ opacity: 0, transform: 'translateY(-50%)' })),
                    transition('* => leave', [
                        style({ opacity: 1, transform: 'translateY(0)' }),
                        animate('100ms linear')
                    ])
                ])
            ]
        }),
        tslib_1.__metadata("design:paramtypes", [ToastComponent,
            ChangeDetectorRef])
    ], ToastItemComponent);
    return ToastItemComponent;
}());
export { ToastItemComponent };
//# sourceMappingURL=toast.item.js.map