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

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var rcProgress = require('rc-progress');
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 __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var Progress = /** @class */ (function (_super) {
    tslib.__extends(Progress, _super);
    function Progress() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Progress.prototype.getCurrentColor = function () {
        var color = this.props.map;
        if (!color || !color.length) {
            return 'bg-primary';
        }
        if (typeof color === 'string') {
            return color;
        }
        else {
            return this.getLevelColor(color);
        }
    };
    Progress.prototype.getLevelColor = function (color) {
        var value = this.props.value;
        var colorArray = this.getColorArray(color).sort(function (a, b) { return a.value - b.value; });
        for (var i = 0; i < colorArray.length; i++) {
            if (colorArray[i].value >= value) {
                return colorArray[i].color;
            }
        }
        return colorArray[colorArray.length - 1].color;
    };
    Progress.prototype.getColorArray = function (color) {
        var span = 100 / color.length;
        return color.map(function (item, index) {
            if (typeof item === 'string') {
                return {
                    color: item,
                    value: (index + 1) * span
                };
            }
            return item;
        });
    };
    Progress.prototype.getLabel = function (prefixCls) {
        var _a = this.props, value = _a.value, format = _a.format, showLabel = _a.showLabel, cx = _a.classnames;
        if (!showLabel) {
            return null;
        }
        var textFormatter = format || (function (value) { return "".concat(value, "%"); });
        var content = textFormatter(value);
        return (_J$X_("span", { className: cx("".concat(prefixCls, "-text")), key: "value" }, content));
    };
    Progress.prototype.render = function () {
        var _a, _b, _c, _d;
        var _e = this.props, className = _e.className, style = _e.style, progressClassName = _e.progressClassName, type = _e.type, value = _e.value, placeholder = _e.placeholder, stripe = _e.stripe, animate = _e.animate, gapDegree = _e.gapDegree, gapPosition = _e.gapPosition, strokeWidth = _e.strokeWidth, cx = _e.classnames, threshold = _e.threshold, showThresholdText = _e.showThresholdText;
        var isLineType = type === 'line';
        var prefixCls = isLineType ? 'Progress-line' : 'Progress-circle';
        var bgColor = this.getCurrentColor();
        var isColorClass = /bg-/.test(bgColor);
        var viewValue;
        if (typeof value !== 'number') {
            viewValue = _J$X_("span", { className: "text-muted" }, placeholder);
        }
        else if (type === 'line') {
            var barStyle = {
                width: "".concat(value, "%")
            };
            strokeWidth && (barStyle.height = strokeWidth);
            !isColorClass && (barStyle.backgroundColor = bgColor);
            var thresholdDom = null;
            if (threshold) {
                var temp_1 = function (text, color) { return (_J$X_("div", { style: {
                        left: text,
                        borderColor: color || 'var(--text-color)'
                    }, className: cx("".concat(prefixCls, "-threshold")), key: text }, showThresholdText ? (_J$X_("span", { className: cx("".concat(prefixCls, "-threshold-text")) }, text)) : null)); };
                if (Array.isArray(threshold)) {
                    thresholdDom = threshold.map(function (item) {
                        var text = parseFloat(item.value) + '%';
                        return temp_1(text, item.color);
                    });
                }
                else {
                    var text = parseFloat(threshold.value) + '%';
                    thresholdDom = temp_1(text, threshold.color);
                }
            }
            viewValue = [
                _J$X_("div", { key: "progress", className: cx(prefixCls, progressClassName) },
                    thresholdDom,
                    _J$X_("div", { className: cx("".concat(prefixCls, "-inter")) },
                        _J$X_("div", { className: cx("".concat(prefixCls, "-bar"), (_a = {}, _a[bgColor] = isColorClass, _a), (_b = {}, _b["".concat(prefixCls, "-bar--stripe")] = stripe, _b), (_c = {}, _c["".concat(prefixCls, "-bar--animate")] = animate && !stripe, _c), (_d = {}, _d["".concat(prefixCls, "-bar--stripe-animate")] = animate && stripe, _d)), title: "".concat(value, "%"), style: barStyle }))),
                this.getLabel(prefixCls)
            ];
        }
        else if (type === 'circle' || type === 'dashboard') {
            var circleWidth = strokeWidth || 8;
            var circleStyle = {
                width: circleWidth * 10 + 'px',
                height: circleWidth * 10 + 'px'
            };
            var gapPos = gapPosition || (type === 'dashboard' && 'bottom') || 'top';
            var getGapDegree = function () {
                if (gapDegree || gapDegree === 0) {
                    return gapDegree;
                }
                if (type === 'dashboard') {
                    return 75;
                }
                return undefined;
            };
            viewValue = [
                _J$X_("div", { className: cx(prefixCls, progressClassName), key: "circle", style: circleStyle },
                    _J$X_(rcProgress.Circle, { percent: value, strokeColor: !isColorClass ? bgColor : '', strokeWidth: circleWidth, trailWidth: circleWidth, prefixCls: isColorClass ? bgColor : '', gapDegree: getGapDegree(), gapPosition: gapPos, style: circleStyle }),
                    this.getLabel(prefixCls))
            ];
        }
        return (_J$X_("div", { className: cx('Progress', className), style: style }, viewValue));
    };
    Progress.defaultProps = {
        type: 'line',
        placeholder: '-',
        progressClassName: '',
        map: ['bg-danger', 'bg-warning', 'bg-info', 'bg-success', 'bg-success'],
        showLabel: true
    };
    return Progress;
}(React__default["default"].Component));
var Progress$1 = amisCore.themeable(Progress);

exports.Progress = Progress;
exports["default"] = Progress$1;
