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

import { __extends, __spreadArray, __read, __assign } from 'tslib';
import React__default from 'react';
import { setThemeClassName, themeable, localeable } from 'amis-core';

var SparkLine = /** @class */ (function (_super) {
    __extends(SparkLine, _super);
    function SparkLine() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SparkLine.prototype.normalizeValue = function (item) {
        if (typeof item === 'number') {
            return item;
        }
        else if (item && typeof item.value === 'number') {
            return item.value;
        }
        else {
            return Number(item) || 0;
        }
    };
    SparkLine.prototype.renderLines = function () {
        var _this = this;
        var _a = this.props, width = _a.width, height = _a.height, value = _a.value, cx = _a.classnames;
        var values = value.map(function (item) { return _this.normalizeValue(item); });
        var max = Math.max.apply(Math, __spreadArray([], __read(values), false));
        var min = Math.min.apply(Math, __spreadArray([], __read(values), false));
        var duration = max - min || 1;
        var gap = width / (values.length - 1);
        var points = [];
        values.forEach(function (value, index) {
            points.push({
                x: index * gap,
                y: height - ((value - min) * height) / duration
            });
        });
        var lineD = points
            .map(function (value, index) { return "".concat(index === 0 ? 'M' : 'L', " ").concat(value.x, " ").concat(value.y); })
            .join(' ');
        var areaD = "".concat(lineD, " V ").concat(height, " L 0 ").concat(height, " Z");
        // todo 支持鼠标 hover 显示对应数据。
        return (React__default.createElement("g", null,
            React__default.createElement("path", { className: cx("Sparkline-area"), d: areaD, stroke: "none" }),
            React__default.createElement("path", { className: cx("Sparkline-line"), d: lineD, fill: "none" })));
    };
    SparkLine.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, className = _a.className, style = _a.style, value = _a.value, width = _a.width, height = _a.height, placeholder = _a.placeholder, __ = _a.translate, onClick = _a.onClick, id = _a.id, wrapperCustomStyle = _a.wrapperCustomStyle, themeCss = _a.themeCss;
        return (React__default.createElement("div", { className: cx('Sparkline', className, onClick ? 'Sparkline--clickable' : '', setThemeClassName(__assign(__assign({}, this.props), { name: 'baseControlClassName', id: id, themeCss: themeCss })), setThemeClassName(__assign(__assign({}, this.props), { name: 'wrapperCustomStyle', id: id, themeCss: wrapperCustomStyle }))), style: style, onClick: onClick }, Array.isArray(value) && value.length > 1 ? (React__default.createElement("svg", { className: cx('Sparkline-svg'), width: width, height: height, viewBox: "0 0 ".concat(width, " ").concat(height) }, this.renderLines())) : (placeholder !== null && placeholder !== void 0 ? placeholder : __('placeholder.empty'))));
    };
    SparkLine.defaultProps = {
        width: 100,
        height: 50
    };
    return SparkLine;
}(React__default.Component));
var SparkLine$1 = themeable(localeable(SparkLine));

export { SparkLine, SparkLine$1 as default };
