/**
 * 数字格式文本提取器
 */
import isNil from 'lodash/isNil';
import { numberFormat } from '@cainiaofe/cn-ui-common';
import { isNumberStr } from '../../cn-table-cell-format/utils';
/**
 * 格式化数字（货币格式）
 */
function formatCurrency(val, adapterLocale) {
    if (adapterLocale) {
        return numberFormat(val, adapterLocale);
    }
    const [pre, suff] = val === null || val === void 0 ? void 0 : val.split('.');
    return `${Number(pre).toLocaleString('zh-CN')}${isNil(suff) ? '' : `.${suff}`}`;
}
/**
 * 从字符串获取格式化数字
 */
function getNumFromStr(val, adapterLocale) {
    return isNumberStr(`${val}`) ? formatCurrency(`${val}`, adapterLocale) : String(val);
}
/**
 * 数字文本提取器
 */
export function extractNumberText(value, _column, _record, numAdapterLocale) {
    if (isNil(value) || value === '') {
        return '';
    }
    return getNumFromStr(value, numAdapterLocale);
}
/**
 * 货币文本提取器
 */
export function extractCurrencyText(value, _column, _record, numAdapterLocale) {
    if (isNil(value) || value === '') {
        return '';
    }
    return getNumFromStr(value, numAdapterLocale);
}
/**
 * 带单位货币文本提取器
 */
export function extractCurrencyWithUnitText(value, column, _record, numAdapterLocale) {
    if (isNil(value)) {
        return '';
    }
    // 处理对象格式 { amount, currency }
    if (typeof value === 'object' && value !== null) {
        const { amount, currency } = value;
        if (isNil(amount)) {
            return '';
        }
        const formattedAmount = getNumFromStr(amount, numAdapterLocale);
        return currency ? `${formattedAmount} ${currency}` : formattedAmount;
    }
    // 处理简单值
    return getNumFromStr(value, numAdapterLocale);
}
/**
 * 百分比文本提取器
 */
export function extractPercentText(value, _column, _record, numAdapterLocale) {
    if (isNil(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
        ? numberFormat(percentValue.toFixed(2), numAdapterLocale)
        : percentValue.toFixed(2);
    return `${formatted}%`;
}
/**
 * 微米格式文本提取器
 */
export function extractMicrometerText(value, _column, _record, numAdapterLocale) {
    if (isNil(value) || value === '') {
        return '';
    }
    return getNumFromStr(value, numAdapterLocale);
}
