"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const CheckCircleOutlined_1 = tslib_1.__importDefault(require("@ant-design/icons/CheckCircleOutlined"));
const CloseCircleOutlined_1 = tslib_1.__importDefault(require("@ant-design/icons/CloseCircleOutlined"));
const CloseOutlined_1 = tslib_1.__importDefault(require("@ant-design/icons/CloseOutlined"));
const ExclamationCircleOutlined_1 = tslib_1.__importDefault(require("@ant-design/icons/ExclamationCircleOutlined"));
const InfoCircleOutlined_1 = tslib_1.__importDefault(require("@ant-design/icons/InfoCircleOutlined"));
const rc_notification_1 = tslib_1.__importDefault(require("rc-notification"));
const react_1 = tslib_1.__importDefault(require("react"));
const ide_utils_1 = require("@opensumi/ide-utils");
const notificationInstance = {};
let defaultDuration = 4.5;
let defaultTop = 24;
let defaultBottom = 24;
let defaultPlacement = 'topRight';
let defaultGetContainer;
let defaultCloseIcon;
function setNotificationConfig(options) {
    const { duration, placement, bottom, top, getContainer, closeIcon } = options;
    if (duration !== undefined) {
        defaultDuration = duration;
    }
    if (placement !== undefined) {
        defaultPlacement = placement;
    }
    if (bottom !== undefined) {
        defaultBottom = bottom;
    }
    if (top !== undefined) {
        defaultTop = top;
    }
    if (getContainer !== undefined) {
        defaultGetContainer = getContainer;
    }
    if (closeIcon !== undefined) {
        defaultCloseIcon = closeIcon;
    }
}
function getPlacementStyle(placement, top = defaultTop, bottom = defaultBottom) {
    let style;
    switch (placement) {
        case 'topLeft':
            style = {
                left: 0,
                top,
                bottom: 'auto',
            };
            break;
        case 'topRight':
            style = {
                right: 0,
                top,
                bottom: 'auto',
            };
            break;
        case 'bottomLeft':
            style = {
                left: 0,
                top: 'auto',
                bottom,
            };
            break;
        default:
            style = {
                right: 0,
                top: 'auto',
                bottom,
            };
            break;
    }
    return style;
}
function getNotificationInstance({ prefixCls, placement = defaultPlacement, getContainer = defaultGetContainer, top, bottom, closeIcon = defaultCloseIcon, }, callback) {
    const cacheKey = `${prefixCls}-${placement}`;
    if (notificationInstance[cacheKey]) {
        callback(notificationInstance[cacheKey]);
        return;
    }
    const closeIconToRender = (react_1.default.createElement("span", { className: `${prefixCls}-close-x` }, closeIcon || react_1.default.createElement(CloseOutlined_1.default, { className: `${prefixCls}-close-icon` })));
    rc_notification_1.default.newInstance({
        prefixCls,
        className: `${prefixCls}-${placement}`,
        style: getPlacementStyle(placement, top, bottom),
        getContainer,
        closeIcon: closeIconToRender,
    }, (notification) => {
        notificationInstance[cacheKey] = notification;
        callback(notification);
    });
}
const typeToIcon = {
    success: CheckCircleOutlined_1.default,
    info: InfoCircleOutlined_1.default,
    error: CloseCircleOutlined_1.default,
    warning: ExclamationCircleOutlined_1.default,
};
function notice(args) {
    const outerPrefixCls = args.prefixCls || 'kt-notification';
    const prefixCls = `${outerPrefixCls}-notice`;
    const duration = (0, ide_utils_1.isUndefined)(args.duration) ? defaultDuration : args.duration;
    let iconNode = null;
    if (args.icon) {
        iconNode = react_1.default.createElement("span", { className: `${prefixCls}-icon` }, args.icon);
    }
    else if (args.type) {
        iconNode = react_1.default.createElement(typeToIcon[args.type] || null, {
            className: `${prefixCls}-icon ${prefixCls}-icon-${args.type}`,
        });
    }
    const { placement, top, bottom, getContainer, closeIcon } = args;
    getNotificationInstance({
        prefixCls: outerPrefixCls,
        placement,
        top,
        bottom,
        getContainer,
        closeIcon,
    }, (notification) => {
        notification.notice({
            content: (react_1.default.createElement("div", { className: iconNode ? `${prefixCls}-with-icon` : '' },
                iconNode,
                react_1.default.createElement("div", { className: `${prefixCls}-message` }, args.message),
                react_1.default.createElement("div", { className: `${prefixCls}-description` }, args.description),
                args.btn ? react_1.default.createElement("span", { className: `${prefixCls}-btn` }, args.btn) : null)),
            duration,
            closable: true,
            onClose: args.onClose,
            onClick: args.onClick,
            key: args.key,
            style: args.style || {},
            className: args.className,
        });
    });
}
const api = {
    open: notice,
    close(key) {
        Object.keys(notificationInstance).forEach((cacheKey) => notificationInstance[cacheKey].removeNotice(key));
    },
    config: setNotificationConfig,
    destroy() {
        Object.keys(notificationInstance).forEach((cacheKey) => {
            notificationInstance[cacheKey].destroy();
            delete notificationInstance[cacheKey];
        });
    },
};
['success', 'info', 'warning', 'error'].forEach((type) => {
    api[type] = (args) => api.open({
        ...args,
        type,
    });
});
api.warn = api.warning;
exports.default = api;
//# sourceMappingURL=notification.js.map