"use strict";
/**
 * 数字格式文本提取器
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractNumberText = extractNumberText;
exports.extractCurrencyText = extractCurrencyText;
exports.extractCurrencyWithUnitText = extractCurrencyWithUnitText;
exports.extractPercentText = extractPercentText;
exports.extractMicrometerText = extractMicrometerText;
const isNil_1 = __importDefault(require("lodash/isNil"));
const cn_ui_common_1 = require("@cainiaofe/cn-ui-common");
const utils_1 = require("../../cn-table-cell-format/utils");
/**
 * 格式化数字（货币格式）
 */
function formatCurrency(val, adapterLocale) {
    if (adapterLocale) {
        return (0, cn_ui_common_1.numberFormat)(val, adapterLocale);
    }
    const [pre, suff] = val === null || val === void 0 ? void 0 : val.split('.');
    return `${Number(pre).toLocaleString('zh-CN')}${(0, isNil_1.default)(suff) ? '' : `.${suff}`}`;
}
/**
 * 从字符串获取格式化数字
 */
function getNumFromStr(val, adapterLocale) {
    return (0, utils_1.isNumberStr)(`${val}`) ? formatCurrency(`${val}`, adapterLocale) : String(val);
}
/**
 * 数字文本提取器
 */
function extractNumberText(value, _column, _record, numAdapterLocale) {
    if ((0, isNil_1.default)(value) || value === '') {
        return '';
    }
    return getNumFromStr(value, numAdapterLocale);
}
/**
 * 货币文本提取器
 */
function extractCurrencyText(value, _column, _record, numAdapterLocale) {
    if ((0, isNil_1.default)(value) || value === '') {
        return '';
    }
    return getNumFromStr(value, numAdapterLocale);
}
/**
 * 带单位货币文本提取器
 */
function extractCurrencyWithUnitText(value, column, _record, numAdapterLocale) {
    if ((0, isNil_1.default)(value)) {
        return '';
    }
    // 处理对象格式 { amount, currency }
    if (typeof value === 'object' && value !== null) {
        const { amount, currency } = value;
        if ((0, isNil_1.default)(amount)) {
            return '';
        }
        const formattedAmount = getNumFromStr(amount, numAdapterLocale);
        return currency ? `${formattedAmount} ${currency}` : formattedAmount;
    }
    // 处理简单值
    return getNumFromStr(value, numAdapterLocale);
}
/**
 * 百分比文本提取器
 */
function extractPercentText(value, _column, _record, numAdapterLocale) {
    if ((0, isNil_1.default)(value) || value === '') {
        return '';
    }
    const numValue = parseFloat(String(value));
    if (isNaN(numValue)) {
        return '';
    }
    // 百分比通常是小数，需要乘以 100
    const percentValue = numValue <= 1 && numValue >= 0 ? numValue * 100 : numValue;
    const formatted = numAdapterLocale
        ? (0, cn_ui_common_1.numberFormat)(percentValue.toFixed(2), numAdapterLocale)
        : percentValue.toFixed(2);
    return `${formatted}%`;
}
/**
 * 微米格式文本提取器
 */
function extractMicrometerText(value, _column, _record, numAdapterLocale) {
    if ((0, isNil_1.default)(value) || value === '') {
        return '';
    }
    return getNumFromStr(value, numAdapterLocale);
}
