/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var hoistNonReactStatic = require('hoist-non-react-statics');
var amisCore = require('amis-core');

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

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var hoistNonReactStatic__default = /*#__PURE__*/_interopDefaultLegacy(hoistNonReactStatic);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var Badge = /** @class */ (function (_super) {
    tslib.__extends(Badge, _super);
    function Badge(props) {
        return _super.call(this, props) || this;
    }
    Badge.prototype.renderBadge = function (text, size, position, offsetStyle, sizeStyle, animationElement) {
        var _a = this.props, cx = _a.classnames, badge = _a.badge, data = _a.data;
        var _b = badge, _c = _b.mode, mode = _c === void 0 ? 'dot' : _c, _d = _b.level, level = _d === void 0 ? 'danger' : _d, style = _b.style;
        var customStyle = amisCore.buildStyle(style, data);
        if (typeof level === 'string' && level[0] === '$') {
            level = amisCore.resolveVariableAndFilter(level, data);
        }
        switch (mode) {
            case 'dot':
                return (_J$X_("span", { className: cx('Badge-dot', "Badge--".concat(position), "Badge--".concat(level)), style: tslib.__assign(tslib.__assign(tslib.__assign({}, offsetStyle), sizeStyle), customStyle) }, animationElement));
            case 'text':
                return (_J$X_("span", { className: cx('Badge-text', "Badge--".concat(position), "Badge--".concat(level)), style: tslib.__assign(tslib.__assign(tslib.__assign({}, offsetStyle), sizeStyle), customStyle) },
                    text,
                    animationElement));
            case 'ribbon':
                var outSize = size * Math.sqrt(2) + 5;
                return (_J$X_("div", { className: cx('Badge-ribbon-out', "Badge-ribbon-out--".concat(position)), style: { width: outSize, height: outSize } },
                    _J$X_("span", { className: cx('Badge-ribbon', "Badge-ribbon--".concat(position), "Badge--".concat(level)), style: tslib.__assign(tslib.__assign({}, sizeStyle), customStyle) },
                        text,
                        animationElement)));
            default:
                return null;
        }
    };
    Badge.prototype.render = function () {
        var badge = this.props.badge;
        if (!badge) {
            return this.props.children;
        }
        var _a = this.props, children = _a.children, cx = _a.classnames, data = _a.data;
        var isDisplay = true;
        if (typeof badge === 'string') {
            isDisplay = amisCore.evalExpression(badge, data) === true;
        }
        var _b = badge.mode, mode = _b === void 0 ? 'dot' : _b, text = badge.text; badge.level; var size = badge.size, style = badge.style, offset = badge.offset, _c = badge.position, position = _c === void 0 ? 'top-right' : _c, _d = badge.overflowCount, overflowCount = _d === void 0 ? 99 : _d, visibleOn = badge.visibleOn, className = badge.className, animation = badge.animation;
        if (visibleOn) {
            isDisplay = amisCore.evalExpression(visibleOn, data) === true;
        }
        if (typeof text === 'string' && text[0] === '$') {
            text = amisCore.resolveVariableAndFilter(text, data);
        }
        // 设置默认值
        if (typeof size === 'undefined') {
            if (mode === 'dot') {
                size = 6;
            }
            else if (mode === 'ribbon') {
                size = 12;
            }
            else {
                size = 16;
            }
        }
        var sizeStyle = {};
        if (mode === 'text') {
            sizeStyle = {
                borderRadius: size / 2,
                height: size + 2,
                lineHeight: size + 'px'
            };
            // 当text、overflowCount都为number类型时，进行封顶值处理
            // 当text从模版字符串解析（text: "${badge}"）数字就会变为字符串
            // 因此当设置了overflowCount属性时 如果text可以强制转换成数字 那么也进行封顶处理
            if (typeof overflowCount === 'number' &&
                (typeof text === 'number' || (text && !isNaN(Number(text))))) {
                text = (text > overflowCount
                    ? "".concat(overflowCount, "+")
                    : text);
            }
            if (!text) {
                isDisplay = false;
            }
        }
        if (mode === 'dot') {
            sizeStyle = { width: size, height: size };
        }
        if (mode === 'ribbon') {
            sizeStyle = {
                height: size,
                lineHeight: size + 'px',
                fontSize: size
            };
        }
        var offsetStyle = {};
        if (offset && offset.length) {
            var left = "calc(50% + ".concat(parseInt(offset[0], 10), "px)");
            var right = "calc(-50% + ".concat(parseInt(offset[1], 10), "px)");
            offsetStyle = {
                transform: "translate(".concat(left, ", ").concat(right, ")")
            };
        }
        var animationBackground = 'var(--danger)';
        if (style && style.background) {
            animationBackground = style.background;
        }
        var animationElement = animation ? (_J$X_("div", { style: {
                position: 'absolute',
                top: 0,
                left: 0,
                width: '100%',
                height: '100%',
                border: "1px solid ".concat(animationBackground),
                borderRadius: '50%',
                animation: 'badgeDotAnimation 1.2s infinite ease-in-out'
            } })) : null;
        return (_J$X_("div", { className: cx('Badge', className) },
            children,
            isDisplay
                ? this.renderBadge(text, size, position, offsetStyle, sizeStyle, animationElement)
                : null));
    };
    Badge.propsList = ['body', 'className', 'children'];
    return Badge;
}(React__default["default"].Component));
function withBadge(Component) {
    var _a;
    return hoistNonReactStatic__default["default"]((_a = /** @class */ (function (_super) {
            tslib.__extends(WithBadge, _super);
            function WithBadge() {
                return _super !== null && _super.apply(this, arguments) || this;
            }
            WithBadge.prototype.render = function () {
                var badge = this.props.badge;
                if (!badge) {
                    return _J$X_(Component, tslib.__assign({}, this.props));
                }
                return (_J$X_(Badge, tslib.__assign({}, this.props),
                    _J$X_(Component, tslib.__assign({}, this.props))));
            };
            return WithBadge;
        }(React__default["default"].Component)),
        _a.displayName = "WithBadge(".concat(Component.displayName || Component.name, ")"),
        _a), Component);
}

exports.Badge = Badge;
exports["default"] = Badge;
exports.withBadge = withBadge;
