/**
 * 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 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 SparkLine = /** @class */ (function (_super) {
    tslib.__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, tslib.__spreadArray([], tslib.__read(values), false));
        var min = Math.min.apply(Math, tslib.__spreadArray([], tslib.__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 (_J$X_("g", null,
            _J$X_("path", { className: cx("Sparkline-area"), d: areaD, stroke: "none" }),
            _J$X_("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 (_J$X_("div", { className: cx('Sparkline', className, onClick ? 'Sparkline--clickable' : '', amisCore.setThemeClassName(tslib.__assign(tslib.__assign({}, this.props), { name: 'baseControlClassName', id: id, themeCss: themeCss })), amisCore.setThemeClassName(tslib.__assign(tslib.__assign({}, this.props), { name: 'wrapperCustomStyle', id: id, themeCss: wrapperCustomStyle }))), style: style, onClick: onClick }, Array.isArray(value) && value.length > 1 ? (_J$X_("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["default"].Component));
var SparkLine$1 = amisCore.themeable(amisCore.localeable(SparkLine));

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