"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "valueSetter", {
    enumerable: true,
    get: function() {
        return valueSetter;
    }
});
const _number = require("../../../helpers/number");
const _utils = require("../../../dataMap/metaManager/utils");
/**
 * Gets the decimal separator preferred by the cell.
 *
 * @param {object} [cellMeta] The cell meta object.
 * @returns {'.'|','|undefined}
 */ function getCellDecimalSeparator(cellMeta) {
    const locale = cellMeta?.numericFormat?.culture ?? cellMeta?.locale;
    if (typeof locale === 'string' && locale.length > 0) {
        try {
            const decimalPart = new Intl.NumberFormat(locale).formatToParts(1.1).find(({ type })=>type === 'decimal');
            if (decimalPart?.value === '.' || decimalPart?.value === ',') {
                return decimalPart.value;
            }
        } catch  {
        // Invalid locale values are ignored and fall back to the pattern-based detection.
        }
    }
    const pattern = cellMeta?.numericFormat?.pattern;
    if (typeof pattern === 'string') {
        const dotIndex = pattern.lastIndexOf('.');
        const commaIndex = pattern.lastIndexOf(',');
        if (dotIndex > -1 && commaIndex > -1) {
            return dotIndex > commaIndex ? '.' : ',';
        }
        if (commaIndex > -1 && dotIndex === -1) {
            return '.';
        }
        if (dotIndex > -1 && commaIndex === -1) {
            return '.';
        }
    }
}
function valueSetter(newValue, row, column, cellMeta) {
    void row;
    void column;
    if (typeof newValue !== 'string') {
        return newValue;
    }
    const decimalSeparator = getCellDecimalSeparator(cellMeta);
    if ((0, _number.isNumericLike)(newValue) || (0, _number.isCommaThousandsGroupedInteger)(newValue, decimalSeparator)) {
        const parsedNumber = (0, _number.getParsedNumber)(newValue, {
            decimalSeparator
        });
        return (0, _utils.isNullish)(parsedNumber) ? newValue : parsedNumber;
    }
    return newValue;
}
