import { DcValidation } from '../models/dc-validation.model'
/**
 * Applying the numeric formats based on the browser locale/language
 * So that correct decimal separators are applied.
 * For example european format (thousand dot, decimal comma): 1.000,00
 *
 * Uses Intl.NumberFormat options (HOT 17+) instead of the deprecated numbro
 * `pattern`/`culture`. `maximumFractionDigits: 20` preserves all natural
 * decimals (Intl's default of 3 would round); `locale` replaces `culture`.
 *
 * @param rules Cell Validation rules to be updated
 * Those rules are passed in the `columns` property Of handsontable settings.
 */
export const applyNumericFormats = (rules: DcValidation[]): DcValidation[] => {
  for (let rule of rules) {
    if (rule.type === 'numeric') {
      rule.numericFormat = { useGrouping: true, maximumFractionDigits: 20 }
      rule.locale = window.navigator.language
    }
  }

  return rules
}
