import { Overlay, OverlayModule } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { ɵɵelement, ɵɵelementContainerStart, ɵɵelementContainerEnd, ɵɵnextContext, ɵɵadvance, ɵɵproperty, ɵɵsanitizeHtml, EventEmitter, ɵɵdirectiveInject, ChangeDetectorRef, ɵɵdefineComponent, ɵɵelementStart, ɵɵlistener, ɵɵtemplate, ɵɵelementEnd, ɵsetClassMetadata, Component, ChangeDetectionStrategy, ViewEncapsulation, Input, Output, ɵɵgetCurrentView, ɵɵrestoreView, ɵɵstyleProp, ɵɵdefineNgModule, ɵɵdefineInjector, NgModule, ɵɵinject, Injector, ɵɵdefineInjectable, Injectable, ɵɵsetNgModuleScope } from '@angular/core';
import { NzStringTemplateOutletDirective, moveUpMotion, toCssPixel, NzConfigService, NzSingletonService, NzOutletModule } from 'ng-zorro-antd/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NgClass, NgSwitch, NgSwitchCase, NgForOf, CommonModule } from '@angular/common';
import { NzIconDirective, NzIconModule } from 'ng-zorro-antd/icon';

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
function NzMessageComponent_i_4_Template(rf, ctx) { if (rf & 1) {
    ɵɵelement(0, "i", 10);
} }
function NzMessageComponent_i_5_Template(rf, ctx) { if (rf & 1) {
    ɵɵelement(0, "i", 11);
} }
function NzMessageComponent_i_6_Template(rf, ctx) { if (rf & 1) {
    ɵɵelement(0, "i", 12);
} }
function NzMessageComponent_i_7_Template(rf, ctx) { if (rf & 1) {
    ɵɵelement(0, "i", 13);
} }
function NzMessageComponent_i_8_Template(rf, ctx) { if (rf & 1) {
    ɵɵelement(0, "i", 14);
} }
function NzMessageComponent_ng_container_9_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementContainerStart(0);
    ɵɵelement(1, "span", 15);
    ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r655 = ɵɵnextContext();
    ɵɵadvance(1);
    ɵɵproperty("innerHTML", ctx_r655.nzMessage.content, ɵɵsanitizeHtml);
} }
class NzMessageComponent {
    constructor(cdr) {
        this.cdr = cdr;
        this.messageDestroy = new EventEmitter();
        this.eraseTimer = null;
    }
    ngOnInit() {
        // `NzMessageContainer` does its job so all properties cannot be undefined.
        this.options = this.nzMessage.options;
        if (this.options.nzAnimate) {
            this.nzMessage.state = 'enter';
        }
        this.autoClose = this.options.nzDuration > 0;
        if (this.autoClose) {
            this.initErase();
            this.startEraseTimeout();
        }
    }
    ngOnDestroy() {
        if (this.autoClose) {
            this.clearEraseTimeout();
        }
    }
    onEnter() {
        if (this.autoClose && this.options.nzPauseOnHover) {
            this.clearEraseTimeout();
            this.updateTTL();
        }
    }
    onLeave() {
        if (this.autoClose && this.options.nzPauseOnHover) {
            this.startEraseTimeout();
        }
    }
    // Remove self
    destroy(userAction = false) {
        if (this.options.nzAnimate) {
            this.nzMessage.state = 'leave';
            this.cdr.detectChanges();
            setTimeout(() => {
                this.messageDestroy.next({ id: this.nzMessage.messageId, userAction: userAction });
            }, 200);
        }
        else {
            this.messageDestroy.next({ id: this.nzMessage.messageId, userAction: userAction });
        }
    }
    initErase() {
        this.eraseTTL = this.options.nzDuration;
        this.eraseTimingStart = Date.now();
    }
    updateTTL() {
        if (this.autoClose) {
            this.eraseTTL -= Date.now() - this.eraseTimingStart;
        }
    }
    startEraseTimeout() {
        if (this.eraseTTL > 0) {
            this.clearEraseTimeout();
            this.eraseTimer = setTimeout(() => this.destroy(), this.eraseTTL);
            this.eraseTimingStart = Date.now();
        }
        else {
            this.destroy();
        }
    }
    clearEraseTimeout() {
        if (this.eraseTimer !== null) {
            clearTimeout(this.eraseTimer);
            this.eraseTimer = null;
        }
    }
}
/** @nocollapse */ NzMessageComponent.ɵfac = function NzMessageComponent_Factory(t) { return new (t || NzMessageComponent)(ɵɵdirectiveInject(ChangeDetectorRef)); };
/** @nocollapse */ NzMessageComponent.ɵcmp = ɵɵdefineComponent({ type: NzMessageComponent, selectors: [["nz-message"]], inputs: { nzMessage: "nzMessage", nzIndex: "nzIndex" }, outputs: { messageDestroy: "messageDestroy" }, exportAs: ["nzMessage"], decls: 10, vars: 9, consts: [[1, "ant-message-notice", 3, "mouseenter", "mouseleave"], [1, "ant-message-notice-content"], [1, "ant-message-custom-content", 3, "ngClass"], [3, "ngSwitch"], ["nz-icon", "", "nzType", "check-circle", 4, "ngSwitchCase"], ["nz-icon", "", "nzType", "info-circle", 4, "ngSwitchCase"], ["nz-icon", "", "nzType", "exclamation-circle", 4, "ngSwitchCase"], ["nz-icon", "", "nzType", "close-circle", 4, "ngSwitchCase"], ["nz-icon", "", "nzType", "loading", 4, "ngSwitchCase"], [4, "nzStringTemplateOutlet"], ["nz-icon", "", "nzType", "check-circle"], ["nz-icon", "", "nzType", "info-circle"], ["nz-icon", "", "nzType", "exclamation-circle"], ["nz-icon", "", "nzType", "close-circle"], ["nz-icon", "", "nzType", "loading"], [3, "innerHTML"]], template: function NzMessageComponent_Template(rf, ctx) { if (rf & 1) {
        ɵɵelementStart(0, "div", 0);
        ɵɵlistener("mouseenter", function NzMessageComponent_Template_div_mouseenter_0_listener() { return ctx.onEnter(); })("mouseleave", function NzMessageComponent_Template_div_mouseleave_0_listener() { return ctx.onLeave(); });
        ɵɵelementStart(1, "div", 1);
        ɵɵelementStart(2, "div", 2);
        ɵɵelementContainerStart(3, 3);
        ɵɵtemplate(4, NzMessageComponent_i_4_Template, 1, 0, "i", 4);
        ɵɵtemplate(5, NzMessageComponent_i_5_Template, 1, 0, "i", 5);
        ɵɵtemplate(6, NzMessageComponent_i_6_Template, 1, 0, "i", 6);
        ɵɵtemplate(7, NzMessageComponent_i_7_Template, 1, 0, "i", 7);
        ɵɵtemplate(8, NzMessageComponent_i_8_Template, 1, 0, "i", 8);
        ɵɵelementContainerEnd();
        ɵɵtemplate(9, NzMessageComponent_ng_container_9_Template, 2, 1, "ng-container", 9);
        ɵɵelementEnd();
        ɵɵelementEnd();
        ɵɵelementEnd();
    } if (rf & 2) {
        ɵɵproperty("@moveUpMotion", ctx.nzMessage.state);
        ɵɵadvance(2);
        ɵɵproperty("ngClass", "ant-message-" + ctx.nzMessage.type);
        ɵɵadvance(1);
        ɵɵproperty("ngSwitch", ctx.nzMessage.type);
        ɵɵadvance(1);
        ɵɵproperty("ngSwitchCase", "success");
        ɵɵadvance(1);
        ɵɵproperty("ngSwitchCase", "info");
        ɵɵadvance(1);
        ɵɵproperty("ngSwitchCase", "warning");
        ɵɵadvance(1);
        ɵɵproperty("ngSwitchCase", "error");
        ɵɵadvance(1);
        ɵɵproperty("ngSwitchCase", "loading");
        ɵɵadvance(1);
        ɵɵproperty("nzStringTemplateOutlet", ctx.nzMessage.content);
    } }, directives: [NgClass, NgSwitch, NgSwitchCase, NzStringTemplateOutletDirective, NzIconDirective], encapsulation: 2, data: { animation: [moveUpMotion] }, changeDetection: 0 });
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzMessageComponent, [{
        type: Component,
        args: [{
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                selector: 'nz-message',
                exportAs: 'nzMessage',
                preserveWhitespaces: false,
                animations: [moveUpMotion],
                template: `
    <div class="ant-message-notice" [@moveUpMotion]="nzMessage.state" (mouseenter)="onEnter()" (mouseleave)="onLeave()">
      <div class="ant-message-notice-content">
        <div class="ant-message-custom-content" [ngClass]="'ant-message-' + nzMessage.type">
          <ng-container [ngSwitch]="nzMessage.type">
            <i *ngSwitchCase="'success'" nz-icon nzType="check-circle"></i>
            <i *ngSwitchCase="'info'" nz-icon nzType="info-circle"></i>
            <i *ngSwitchCase="'warning'" nz-icon nzType="exclamation-circle"></i>
            <i *ngSwitchCase="'error'" nz-icon nzType="close-circle"></i>
            <i *ngSwitchCase="'loading'" nz-icon nzType="loading"></i>
          </ng-container>
          <ng-container *nzStringTemplateOutlet="nzMessage.content">
            <span [innerHTML]="nzMessage.content"></span>
          </ng-container>
        </div>
      </div>
    </div>
  `
            }]
    }], function () { return [{ type: ChangeDetectorRef }]; }, { nzMessage: [{
            type: Input
        }], nzIndex: [{
            type: Input
        }], messageDestroy: [{
            type: Output
        }] }); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
function NzMessageContainerComponent_nz_message_1_Template(rf, ctx) { if (rf & 1) {
    const _r649 = ɵɵgetCurrentView();
    ɵɵelementStart(0, "nz-message", 2);
    ɵɵlistener("messageDestroy", function NzMessageContainerComponent_nz_message_1_Template_nz_message_messageDestroy_0_listener($event) { ɵɵrestoreView(_r649); const ctx_r648 = ɵɵnextContext(); return ctx_r648.removeMessage($event.id, $event.userAction); });
    ɵɵelementEnd();
} if (rf & 2) {
    const message_r647 = ctx.$implicit;
    ɵɵproperty("nzMessage", message_r647);
} }
const NZ_CONFIG_COMPONENT_NAME = 'message';
const NZ_MESSAGE_DEFAULT_CONFIG = {
    nzAnimate: true,
    nzDuration: 3000,
    nzMaxStack: 7,
    nzPauseOnHover: true,
    nzTop: 24
};
class NzMessageContainerComponent {
    constructor(cdr, nzConfigService) {
        this.cdr = cdr;
        this.nzConfigService = nzConfigService;
        this.destroy$ = new Subject();
        this.messages = [];
        this.updateConfig();
    }
    ngOnInit() {
        this.subscribeConfigChange();
    }
    ngOnDestroy() {
        this.destroy$.next();
        this.destroy$.complete();
    }
    /**
     * Create a new message.
     * @param message Parsed message configuration.
     */
    createMessage(message) {
        if (this.messages.length >= this.config.nzMaxStack) {
            this.messages = this.messages.slice(1);
        }
        message.options = this.mergeMessageOptions(message.options);
        message.onClose = new Subject();
        this.messages = [...this.messages, message];
        this.cdr.detectChanges();
    }
    /**
     * Remove a message by `messageId`.
     * @param messageId Id of the message to be removed.
     * @param userAction Whether this is closed by user interaction.
     */
    removeMessage(messageId, userAction = false) {
        this.messages.some((message, index) => {
            if (message.messageId === messageId) {
                this.messages.splice(index, 1);
                this.messages = [...this.messages];
                this.cdr.detectChanges();
                message.onClose.next(userAction);
                message.onClose.complete();
                return true;
            }
            return false;
        });
    }
    /**
     * Remove all messages.
     */
    removeMessageAll() {
        this.messages = [];
        this.cdr.detectChanges();
    }
    updateConfig() {
        this.config = this.updateConfigFromConfigService();
        this.top = toCssPixel(this.config.nzTop);
        this.cdr.markForCheck();
    }
    subscribeConfigChange() {
        this.nzConfigService
            .getConfigChangeEventForComponent(NZ_CONFIG_COMPONENT_NAME)
            .pipe(takeUntil(this.destroy$))
            .subscribe(() => this.updateConfig());
    }
    updateConfigFromConfigService() {
        return Object.assign(Object.assign(Object.assign({}, NZ_MESSAGE_DEFAULT_CONFIG), this.config), this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME));
    }
    /**
     * Merge default options and custom message options
     * @param options
     */
    mergeMessageOptions(options) {
        const defaultOptions = {
            nzDuration: this.config.nzDuration,
            nzAnimate: this.config.nzAnimate,
            nzPauseOnHover: this.config.nzPauseOnHover
        };
        return Object.assign(Object.assign({}, defaultOptions), options);
    }
}
/** @nocollapse */ NzMessageContainerComponent.ɵfac = function NzMessageContainerComponent_Factory(t) { return new (t || NzMessageContainerComponent)(ɵɵdirectiveInject(ChangeDetectorRef), ɵɵdirectiveInject(NzConfigService)); };
/** @nocollapse */ NzMessageContainerComponent.ɵcmp = ɵɵdefineComponent({ type: NzMessageContainerComponent, selectors: [["nz-message-container"]], exportAs: ["nzMessageContainer"], decls: 2, vars: 3, consts: [[1, "ant-message"], [3, "nzMessage", "messageDestroy", 4, "ngFor", "ngForOf"], [3, "nzMessage", "messageDestroy"]], template: function NzMessageContainerComponent_Template(rf, ctx) { if (rf & 1) {
        ɵɵelementStart(0, "div", 0);
        ɵɵtemplate(1, NzMessageContainerComponent_nz_message_1_Template, 1, 1, "nz-message", 1);
        ɵɵelementEnd();
    } if (rf & 2) {
        ɵɵstyleProp("top", ctx.top);
        ɵɵadvance(1);
        ɵɵproperty("ngForOf", ctx.messages);
    } }, directives: [NgForOf, NzMessageComponent], encapsulation: 2, changeDetection: 0 });
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzMessageContainerComponent, [{
        type: Component,
        args: [{
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                selector: 'nz-message-container',
                exportAs: 'nzMessageContainer',
                preserveWhitespaces: false,
                template: `
    <div class="ant-message" [style.top]="top">
      <nz-message
        *ngFor="let message of messages"
        [nzMessage]="message"
        (messageDestroy)="removeMessage($event.id, $event.userAction)"
      ></nz-message>
    </div>
  `
            }]
    }], function () { return [{ type: ChangeDetectorRef }, { type: NzConfigService }]; }, null); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
class NzMessageServiceModule {
}
/** @nocollapse */ NzMessageServiceModule.ɵmod = ɵɵdefineNgModule({ type: NzMessageServiceModule });
/** @nocollapse */ NzMessageServiceModule.ɵinj = ɵɵdefineInjector({ factory: function NzMessageServiceModule_Factory(t) { return new (t || NzMessageServiceModule)(); } });
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzMessageServiceModule, [{
        type: NgModule
    }], null, null); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
let globalCounter = 0;
class NzMessageService {
    constructor(nzSingletonService, overlay, injector) {
        this.nzSingletonService = nzSingletonService;
        this.overlay = overlay;
        this.injector = injector;
        this.name = 'message-';
    }
    // Shortcut methods
    success(content, options) {
        return this.createMessage({ type: 'success', content }, options);
    }
    error(content, options) {
        return this.createMessage({ type: 'error', content }, options);
    }
    info(content, options) {
        return this.createMessage({ type: 'info', content }, options);
    }
    warning(content, options) {
        return this.createMessage({ type: 'warning', content }, options);
    }
    loading(content, options) {
        return this.createMessage({ type: 'loading', content }, options);
    }
    create(type, content, options) {
        return this.createMessage({ type, content }, options);
    }
    remove(messageId) {
        if (messageId) {
            this.container.removeMessage(messageId);
        }
        else {
            this.container.removeMessageAll();
        }
    }
    createMessage(message, options) {
        this.container = this.withContainer();
        this.nzSingletonService.registerSingletonWithKey(this.name, this.container);
        const resultMessage = Object.assign(Object.assign({}, message), {
            createdAt: new Date(),
            messageId: this.generateMessageId(),
            options
        });
        this.container.createMessage(resultMessage);
        return resultMessage;
    }
    generateMessageId() {
        return `${this.name}-${globalCounter++}`;
    }
    // Manually creating container for overlay to avoid multi-checking error, see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/391
    // NOTE: we never clean up the container component and it's overlay resources, if we should, we need to do it by our own codes.
    withContainer() {
        const containerInstance = this.nzSingletonService.getSingletonWithKey(this.name);
        if (containerInstance) {
            return containerInstance;
        }
        const overlayRef = this.overlay.create({
            hasBackdrop: false,
            scrollStrategy: this.overlay.scrollStrategies.noop(),
            positionStrategy: this.overlay.position().global()
        });
        const componentPortal = new ComponentPortal(NzMessageContainerComponent, null, this.injector);
        const componentRef = overlayRef.attach(componentPortal);
        const overlayPane = overlayRef.overlayElement;
        overlayPane.style.zIndex = '1010'; // Patching: assign the same zIndex of ant-message to it's parent overlay panel, to the ant-message's zindex work.
        return componentRef.instance;
    }
}
/** @nocollapse */ NzMessageService.ɵfac = function NzMessageService_Factory(t) { return new (t || NzMessageService)(ɵɵinject(NzSingletonService), ɵɵinject(Overlay), ɵɵinject(Injector)); };
/** @nocollapse */ NzMessageService.ɵprov = ɵɵdefineInjectable({ token: NzMessageService, factory: NzMessageService.ɵfac, providedIn: NzMessageServiceModule });
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzMessageService, [{
        type: Injectable,
        args: [{
                providedIn: NzMessageServiceModule
            }]
    }], function () { return [{ type: NzSingletonService }, { type: Overlay }, { type: Injector }]; }, null); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
class NzMessageModule {
}
/** @nocollapse */ NzMessageModule.ɵmod = ɵɵdefineNgModule({ type: NzMessageModule });
/** @nocollapse */ NzMessageModule.ɵinj = ɵɵdefineInjector({ factory: function NzMessageModule_Factory(t) { return new (t || NzMessageModule)(); }, imports: [[CommonModule, OverlayModule, NzIconModule, NzOutletModule, NzMessageServiceModule]] });
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵɵsetNgModuleScope(NzMessageModule, { declarations: [NzMessageContainerComponent, NzMessageComponent], imports: [CommonModule, OverlayModule, NzIconModule, NzOutletModule, NzMessageServiceModule] }); })();
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzMessageModule, [{
        type: NgModule,
        args: [{
                imports: [CommonModule, OverlayModule, NzIconModule, NzOutletModule, NzMessageServiceModule],
                declarations: [NzMessageContainerComponent, NzMessageComponent]
            }]
    }], null, null); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

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

export { NzMessageComponent, NzMessageContainerComponent, NzMessageModule, NzMessageService, NzMessageServiceModule };
//# sourceMappingURL=ng-zorro-antd-message.js.map
