"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Statistic", {
    enumerable: true,
    get: function() {
        return Statistic;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _css = require("../utils/css");
const _text = require("../text");
const _defaultStaticStyle = require("./defaultStaticStyle");
const _tooltip = require("../tooltip");
const alignClasses = {
    left: "orderly-text-left",
    right: "orderly-text-right",
    center: "orderly-text-center"
};
const coloringClasses = {
    lose: "orderly-text-trade-loss",
    profit: "orderly-text-trade-profit",
    neutral: "orderly-text-base-contrast/50"
};
const Statistic = (props)=>{
    const { align = "left", rule: rule, asChild } = props;
    const { labelClassName, valueClassName } = (0, _react.useContext)(_defaultStaticStyle.StatisticStyleContext);
    const labelElement = (0, _react.useMemo)(()=>{
        if (typeof props.label === "string") {
            return props.label;
        }
        return props.label;
    }, [
        props.label
    ]);
    // lose, profit, neutral
    const colorClassName = (0, _react.useMemo)(()=>{
        if (!props.coloring) return "";
        if (typeof props.value !== "number" && typeof props.value !== "string") return "";
        // if (props.value === 0) return coloringClasses.neutral;
        const num = Number(props.value);
        if (Number.isNaN(num)) {
            // console.warn(`if coloring, value is need number: ${props.value}`);
            return "";
        }
        if (num === 0) return coloringClasses.neutral;
        if (num < 0) return coloringClasses.lose;
        // const firstChar = String(props.value).charAt(0);
        // if (firstChar === "-") return coloringClasses.lose;
        return coloringClasses.profit;
    }, [
        props.coloring,
        props.value
    ]);
    // ---------- create value element ----------
    const valueElement = (0, _react.useMemo)(()=>{
        if (typeof props.value === "string" || typeof props.value === "number") {
            if (rule === "price" || rule === "percentages") {
                return /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Numeral, {
                    rule: rule,
                    precision: props.precision,
                    visible: props.visible,
                    children: props.value
                });
            }
            // "date" | "address" | "text" | "symbol"
            if (rule === "date" || rule === "address" || rule === "text" || rule === "symbol" || rule === "status") {
                return /*#__PURE__*/ (0, _jsxruntime.jsx)(_text.Text, {
                    rule: rule,
                    children: props.value
                });
            }
            return /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                children: props.value
            });
        }
        return props.value ?? "--";
    }, [
        props.value,
        rule,
        props.precision,
        props.visible
    ]);
    let label;
    if (typeof props.hint === "undefined") {
        label = /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
            className: (0, _css.cn)(labelClassName, props.labelClassName),
            children: labelElement
        });
    } else {
        label = /*#__PURE__*/ (0, _jsxruntime.jsx)(_tooltip.Tooltip, {
            content: props.hint,
            className: "orderly-max-w-[200px]",
            children: /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: (0, _css.cn)(labelClassName, props.labelClassName),
                children: labelElement
            })
        });
    }
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        className: (0, _css.cn)(alignClasses[align], props.className, props.hint && "orderly-cursor-pointer hover:orderly-text-base-contrast"),
        children: [
            label,
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: (0, _css.cn)(valueClassName, props.valueClassName, colorClassName),
                children: valueElement
            })
        ]
    });
};

//# sourceMappingURL=statistic.js.map