"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const CheckCircleFilled_1 = tslib_1.__importDefault(require("@ant-design/icons/CheckCircleFilled"));
const CloseCircleFilled_1 = tslib_1.__importDefault(require("@ant-design/icons/CloseCircleFilled"));
const ExclamationCircleFilled_1 = tslib_1.__importDefault(require("@ant-design/icons/ExclamationCircleFilled"));
const InfoCircleFilled_1 = tslib_1.__importDefault(require("@ant-design/icons/InfoCircleFilled"));
const LoadingOutlined_1 = tslib_1.__importDefault(require("@ant-design/icons/LoadingOutlined"));
const rc_notification_1 = tslib_1.__importDefault(require("rc-notification"));
const react_1 = tslib_1.__importDefault(require("react"));
let defaultDuration = 3;
let defaultTop;
let messageInstance;
let key = 1;
let prefixCls = 'kt-message';
let transitionName = 'move-up';
let getContainer;
let maxCount;
function getMessageInstance(callback) {
    if (messageInstance) {
        callback(messageInstance);
        return;
    }
    rc_notification_1.default.newInstance({
        prefixCls,
        transitionName,
        style: { top: defaultTop },
        getContainer,
        maxCount,
    }, (instance) => {
        if (messageInstance) {
            callback(messageInstance);
            return;
        }
        messageInstance = instance;
        callback(instance);
    });
}
const typeToIcon = {
    info: InfoCircleFilled_1.default,
    success: CheckCircleFilled_1.default,
    error: CloseCircleFilled_1.default,
    warning: ExclamationCircleFilled_1.default,
    loading: LoadingOutlined_1.default,
};
function notice(args) {
    const duration = args.duration !== undefined ? args.duration : defaultDuration;
    const target = args.key || key++;
    const closePromise = new Promise((resolve) => {
        const callback = () => {
            if (typeof args.onClose === 'function') {
                args.onClose();
            }
            return resolve(true);
        };
        getMessageInstance((instance) => {
            const IconComponent = typeToIcon[args.type];
            instance.notice({
                key: target,
                duration,
                style: {},
                content: (react_1.default.createElement("div", { className: `${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}` },
                    args.icon || (IconComponent && react_1.default.createElement(IconComponent, null)),
                    react_1.default.createElement("span", null, args.content))),
                onClose: callback,
            });
        });
    });
    const result = () => {
        if (messageInstance) {
            messageInstance.removeNotice(target);
        }
    };
    result.then = (filled, rejected) => closePromise.then(filled, rejected);
    result.promise = closePromise;
    return result;
}
function isArgsProps(content) {
    return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
}
const api = {
    open: notice,
    config(options) {
        if (options.top !== undefined) {
            defaultTop = options.top;
            messageInstance = null; // delete messageInstance for new defaultTop
        }
        if (options.duration !== undefined) {
            defaultDuration = options.duration;
        }
        if (options.prefixCls !== undefined) {
            prefixCls = options.prefixCls;
        }
        if (options.getContainer !== undefined) {
            getContainer = options.getContainer;
        }
        if (options.transitionName !== undefined) {
            transitionName = options.transitionName;
            messageInstance = null; // delete messageInstance for new transitionName
        }
        if (options.maxCount !== undefined) {
            maxCount = options.maxCount;
            messageInstance = null;
        }
    },
    destroy() {
        if (messageInstance) {
            messageInstance.destroy();
            messageInstance = null;
        }
    },
};
['success', 'info', 'warning', 'error', 'loading'].forEach((type) => {
    api[type] = (content, duration, onClose) => {
        if (isArgsProps(content)) {
            return api.open(Object.assign(Object.assign({}, content), { type }));
        }
        if (typeof duration === 'function') {
            onClose = duration;
            duration = undefined;
        }
        return api.open({ content, duration, type, onClose });
    };
});
api.warn = api.warning;
exports.default = api;
//# sourceMappingURL=message.js.map