/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var Transition = require('react-transition-group/Transition');
var React = require('react');
var Html = require('./Html.js');
var amisCore = require('amis-core');
var icons = require('./icons.js');
var groupBy = require('lodash/groupBy');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var Transition__default = /*#__PURE__*/_interopDefaultLegacy(Transition);
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var groupBy__default = /*#__PURE__*/_interopDefaultLegacy(groupBy);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var _a;
var fadeStyles = (_a = {},
    _a[Transition.ENTERING] = 'in',
    _a[Transition.ENTERED] = 'in',
    _a[Transition.EXITING] = 'out',
    _a);
var toastRef = null;
var show = function (content, conf, method) {
    if (conf === void 0) { conf = {}; }
    if (!toastRef || !toastRef[method]) {
        return;
    }
    toastRef[method](content, tslib.__assign({}, conf));
};
var ToastComponent = /** @class */ (function (_super) {
    tslib.__extends(ToastComponent, _super);
    function ToastComponent() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // 当前ToastComponent是否真正render了
        _this.hasRendered = false;
        _this.state = {
            items: []
        };
        return _this;
    }
    ToastComponent.prototype.componentDidMount = function () {
        this.hasRendered = true;
        toastRef = this;
    };
    ToastComponent.prototype.componentWillUnmount = function () {
        if (this.hasRendered) {
            toastRef = null;
        }
    };
    ToastComponent.prototype.notifiy = function (level, content, config) {
        var mobileUI = config.mobileUI || this.props.mobileUI;
        this.setState(function (state) {
            var items = state.items.concat();
            if (mobileUI) {
                // 移动端只能存在一个
                items = [];
            }
            items.push(tslib.__assign(tslib.__assign({ body: content, level: level }, config), { id: amisCore.guid(), className: config.className || '', position: config.position || (mobileUI ? 'center' : config.position), timeout: config.timeout || (mobileUI ? 3000 : undefined) }));
            return {
                items: items,
                mobileUI: mobileUI
            };
        });
    };
    ToastComponent.prototype.success = function (content, config) {
        this.notifiy('success', content, config);
    };
    ToastComponent.prototype.error = function (content, config) {
        this.notifiy('error', content, config);
    };
    ToastComponent.prototype.info = function (content, config) {
        this.notifiy('info', content, config);
    };
    ToastComponent.prototype.warning = function (content, config) {
        this.notifiy('warning', content, config);
    };
    ToastComponent.prototype.handleDismissed = function (index) {
        var _a;
        var items = this.state.items.concat();
        var _b = tslib.__read(items.splice(index, 1), 1), item = _b[0];
        (_a = item === null || item === void 0 ? void 0 : item.onDissmiss) === null || _a === void 0 ? void 0 : _a.call(item);
        this.setState({
            items: items
        });
    };
    ToastComponent.prototype.render = function () {
        var _this = this;
        if (toastRef && !this.hasRendered) {
            return null;
        }
        var _a = this.props, cx = _a.classnames, className = _a.className, timeout = _a.timeout, errorTimeout = _a.errorTimeout, position = _a.position, showIcon = _a.showIcon, translate = _a.translate, closeButton = _a.closeButton;
        var items = this.state.items;
        var mobileUI = this.state.mobileUI || this.props.mobileUI;
        var groupedItems = groupBy__default["default"](items, function (item) { return item.position || position; });
        return Object.keys(groupedItems).map(function (position) {
            var toasts = groupedItems[position];
            return (_J$X_("div", { key: position, className: cx("Toast-wrap Toast-wrap--".concat(position.replace(/\-(\w)/g, function (_, l) {
                    return l.toUpperCase();
                })), {
                    'Toast-mobile': mobileUI
                }, className) }, toasts.map(function (item, index) {
                var _a, _b, _c;
                var level = item.level || 'info';
                var toastTimeout = (_a = item.timeout) !== null && _a !== void 0 ? _a : (level === 'error' ? errorTimeout : timeout);
                return (_J$X_(ToastMessage, { classnames: cx, key: item.id || index, title: item.title, body: item.body, level: level, className: item.className, timeout: toastTimeout, closeButton: !mobileUI && ((_b = item.closeButton) !== null && _b !== void 0 ? _b : closeButton), onDismiss: _this.handleDismissed.bind(_this, items.indexOf(item)), translate: translate, showIcon: (_c = item.showIcon) !== null && _c !== void 0 ? _c : showIcon, mobileUI: mobileUI }));
            })));
        });
    };
    ToastComponent.defaultProps = {
        position: 'top-center',
        closeButton: false,
        timeout: 4000,
        errorTimeout: 6000,
        items: []
    };
    ToastComponent.themeKey = 'toast';
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String, Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ToastComponent.prototype, "success", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String, Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ToastComponent.prototype, "error", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String, Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ToastComponent.prototype, "info", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String, Object]),
        tslib.__metadata("design:returntype", void 0)
    ], ToastComponent.prototype, "warning", null);
    return ToastComponent;
}(React__default["default"].Component));
var Toast = amisCore.themeable(amisCore.localeable(ToastComponent));
var ToastMessage = /** @class */ (function (_super) {
    tslib.__extends(ToastMessage, _super);
    function ToastMessage() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            visible: false
        };
        _this.mounted = false;
        return _this;
    }
    ToastMessage.prototype.componentDidMount = function () {
        this.mounted = true;
        this.setState({
            visible: true
        });
    };
    ToastMessage.prototype.componentWillUnmount = function () {
        clearTimeout(this.timer);
        this.mounted = false;
    };
    ToastMessage.prototype.handleMouseEnter = function () {
        clearTimeout(this.timer);
    };
    ToastMessage.prototype.handleMouseLeave = function () {
        this.handleEntered();
    };
    ToastMessage.prototype.handleEntered = function () {
        var timeout = this.props.timeout;
        if (this.mounted && timeout) {
            this.timer = setTimeout(this.close, timeout);
        }
    };
    ToastMessage.prototype.close = function () {
        clearTimeout(this.timer);
        this.setState({
            visible: false
        });
    };
    ToastMessage.prototype.render = function () {
        var _this = this;
        var _a = this.props, onDismiss = _a.onDismiss, cx = _a.classnames, closeButton = _a.closeButton, title = _a.title, body = _a.body, allowHtml = _a.allowHtml, level = _a.level, showIcon = _a.showIcon, mobileUI = _a.mobileUI; _a.translate; var className = _a.className;
        var iconName = mobileUI ? '' : 'alert-';
        return (_J$X_(Transition__default["default"], { mountOnEnter: true, unmountOnExit: true, in: this.state.visible, timeout: 750, onEntered: this.handleEntered, onExited: onDismiss }, function (status) {
            var _a, _b, _c, _d;
            return (_J$X_("div", { className: cx("Toast Toast--".concat(level), className, fadeStyles[status], {
                    'Toast-mobile--has-icon': mobileUI && showIcon !== false
                }), onMouseEnter: _this.handleMouseEnter, onMouseLeave: _this.handleMouseLeave, onClick: closeButton ? amisCore.noop : _this.close },
                showIcon === false ? null : (_J$X_("div", { className: cx('Toast-icon') }, level === 'success' ? (_J$X_(icons.Icon, { icon: iconName + 'success', className: "icon" })) : level == 'error' ? (_J$X_(icons.Icon, { icon: iconName + 'fail', className: "icon" })) : level == 'info' ? (_J$X_(icons.Icon, { icon: iconName + 'info', className: "icon" })) : level == 'warning' ? (_J$X_(icons.Icon, { icon: iconName + 'warning', className: "icon" })) : null)),
                _J$X_("div", { className: cx('Toast-content') },
                    typeof title === 'string' ? (_J$X_("span", { className: cx("Toast-title") }, title)) : React__default["default"].isValidElement(title) ? (React__default["default"].cloneElement(title, {
                        className: cx("Toast-title", (_b = (_a = title === null || title === void 0 ? void 0 : title.props) === null || _a === void 0 ? void 0 : _a.className) !== null && _b !== void 0 ? _b : '')
                    })) : null,
                    React__default["default"].isValidElement(body) ? (React__default["default"].cloneElement(body, {
                        className: cx("Toast-body", (_d = (_c = body === null || body === void 0 ? void 0 : body.props) === null || _c === void 0 ? void 0 : _c.className) !== null && _d !== void 0 ? _d : '')
                    })) : typeof body === 'string' || typeof body === 'object' ? (_J$X_("div", { className: cx('Toast-body') }, allowHtml ? (_J$X_(Html["default"], { html: body === null || body === void 0 ? void 0 : body.toString() })) : (body === null || body === void 0 ? void 0 : body.toString()))) : null),
                closeButton ? (_J$X_("a", { onClick: _this.close, className: cx("Toast-close") },
                    _J$X_(icons.Icon, { icon: "status-close", className: "icon" }))) : null));
        }));
    };
    ToastMessage.defaultProps = {
        timeout: 5000,
        classPrefix: '',
        position: 'top-center',
        allowHtml: true,
        level: 'info'
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ToastMessage.prototype, "handleMouseEnter", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ToastMessage.prototype, "handleMouseLeave", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ToastMessage.prototype, "handleEntered", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], ToastMessage.prototype, "close", null);
    return ToastMessage;
}(React__default["default"].Component));
var toast = {
    container: toastRef,
    success: function (content, conf) { return show(content, conf, 'success'); },
    error: function (content, conf) { return show(content, conf, 'error'); },
    info: function (content, conf) { return show(content, conf, 'info'); },
    warning: function (content, conf) { return show(content, conf, 'warning'); }
};

exports.ToastComponent = ToastComponent;
exports.ToastMessage = ToastMessage;
exports["default"] = Toast;
exports.toast = toast;
