'use strict';

/**
 * Determines the appropriate CSS class name based on a percentage value.
 *
 * This function categorizes percentage values into discrete ranges with
 * increments of 10%.
 * It returns a class name string that can be used to apply styling based
 * on the value.
 *
 * For example:
 * - A value of 35 will return "has-value-30to40-positive"
 * - A value of -75 will return "has-value-80to70-negative"
 * - A value of 0 will return "has-value-0"
 * - Values over 200 or under -200 will return "has-value-200or-larger-positive"
 * or "has-value-200or-larger-negative"
 *
 * The returned class names can be used in CSS to apply different colors,
 * transformations, or other visual treatments based on the percentage range.
 *
 * @param value - The percentage value to classify,
 * can be positive, negative, or zero
 * @returns A class name string representing the percentage range,
 * or an empty string if value is null
 */
function getPercentageClass(value) {
    if (value === 0) {
        return 'has-value-0';
    }
    if (value === null) {
        return '';
    }
    const ranges = [
        { min: 200, max: Infinity, class: 'has-value-200or-larger' },
        { min: 100, max: 200, class: 'has-value-100to200' },
        { min: 90, max: 100, class: 'has-value-90to100' },
        { min: 80, max: 90, class: 'has-value-80to90' },
        { min: 70, max: 80, class: 'has-value-70to80' },
        { min: 60, max: 70, class: 'has-value-60to70' },
        { min: 50, max: 60, class: 'has-value-50to60' },
        { min: 40, max: 50, class: 'has-value-40to50' },
        { min: 30, max: 40, class: 'has-value-30to40' },
        { min: 20, max: 30, class: 'has-value-20to30' },
        { min: 10, max: 20, class: 'has-value-10to20' },
        { min: 0, max: 10, class: 'has-value-0to10' },
        { min: -10, max: 0, class: 'has-value-10to0' },
        { min: -20, max: -10, class: 'has-value-20to10' },
        { min: -30, max: -20, class: 'has-value-30to20' },
        { min: -40, max: -30, class: 'has-value-40to30' },
        { min: -50, max: -40, class: 'has-value-50to40' },
        { min: -60, max: -50, class: 'has-value-60to50' },
        { min: -70, max: -60, class: 'has-value-70to60' },
        { min: -80, max: -70, class: 'has-value-80to70' },
        { min: -90, max: -80, class: 'has-value-90to80' },
        { min: -100, max: -90, class: 'has-value-100to90' },
        { min: -200, max: -100, class: 'has-value-200to100' },
        { min: -Infinity, max: -200, class: 'has-value-200or-larger' },
    ];
    const range = ranges.find((r) => value >= r.min && value < r.max);
    if (!range) {
        return '';
    }
    return `${range.class}-${value > 0 ? 'positive' : 'negative'}`;
}

/**
 * Normalizes a locale-formatted number string to a format
 * that `Number.parseFloat` can handle.
 *
 * @param value - The locale-formatted number string
 * @param locale - A locale string (e.g. 'sv', 'sv-SE', 'en_us'). The primary
 *   language subtag is extracted and used for matching.
 * @returns A normalized string with dot as decimal separator
 */
function normalizeValue(value, locale) {
    const language = locale.toLowerCase().split(/[-_]/)[0];
    switch (language) {
        case 'da':
        case 'de':
        case 'nl': {
            return value.replaceAll('.', '').replace(',', '.');
        }
        case 'nb':
        case 'no':
        case 'sv':
        case 'fi':
        case 'fr': {
            return value.replaceAll(/[\s\u00A0]/g, '').replace(',', '.');
        }
        case 'en': {
            return value.replaceAll(',', '');
        }
        default: {
            return value;
        }
    }
}

exports.getPercentageClass = getPercentageClass;
exports.normalizeValue = normalizeValue;
