"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.notification = void 0;
exports.open = open;
exports.close = close;
exports.update = update;
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = tslib_1.__importDefault(require("react"));
const ide_utils_1 = require("@opensumi/ide-utils");
const button_1 = require("../button");
const common_1 = require("../common");
const notification_1 = tslib_1.__importDefault(require("./notification"));
require("./notification.less");
const DURATION = {
    [common_1.MessageType.Info]: 15,
    [common_1.MessageType.Warning]: 18,
    [common_1.MessageType.Error]: 20,
};
notification_1.default.config({
    placement: 'bottomRight',
});
exports.notification = notification_1.default;
const cachedArgs = new Map();
function open(message, type, closable = true, key, buttons, description, duration, onClose) {
    return (0, ide_utils_1.createCancelablePromise)((token) => {
        token.onCancellationRequested(() => {
            close(key);
        });
        return new Promise((resolve) => {
            const args = {
                key,
                className: (0, classnames_1.default)('kt-notification-wrapper', {
                    ['kt-notification-info']: type === common_1.MessageType.Info,
                    ['kt-notification-error']: type === common_1.MessageType.Error,
                    ['kt-notification-warn']: type === common_1.MessageType.Warning,
                }),
                duration: (0, ide_utils_1.isUndefined)(duration) ? DURATION[type] : duration,
                onClose: () => {
                    onClose && onClose();
                    cachedArgs.delete(key);
                    resolve(undefined);
                },
                btn: buttons
                    ? buttons.map((button, index) => {
                        const isStringButton = typeof button === 'string';
                        const buttonProps = {
                            className: `${(0, classnames_1.default)('kt-notification-button')}${isStringButton ? '' : button.class}`,
                            ghost: isStringButton ? index === 0 : !button.primary,
                            key: isStringButton ? button : button.id,
                            onClick: () => {
                                resolve(button);
                                notification_1.default.close(key);
                                if (!isStringButton) {
                                    button.run();
                                }
                            },
                        };
                        const text = isStringButton ? button : button.label;
                        return (react_1.default.createElement(button_1.Button, { size: 'small', ...buttonProps }, text));
                    })
                    : null,
                message,
                description,
            };
            cachedArgs.set(key, [type, args]);
            // closable 为 false 时，不展示 closeIcon
            if (!closable) {
                args.closeIcon = react_1.default.createElement("span", null);
            }
            doOpenNotification(type, args);
        });
    });
}
function close(key) {
    exports.notification.close(key);
}
function update(key, message) {
    const args = cachedArgs.get(key);
    doOpenNotification(args[0], {
        ...args[1],
        key,
        message,
    });
}
function doOpenNotification(type, args) {
    switch (type) {
        case common_1.MessageType.Info:
            exports.notification.info(args);
            break;
        case common_1.MessageType.Warning:
            exports.notification.warning(args);
            break;
        case common_1.MessageType.Error:
            exports.notification.error(args);
            break;
        default:
            exports.notification.open(args);
            break;
    }
}
//# sourceMappingURL=index.js.map