import { DcValidation } from '../models/dc-validation.model'

/**
 * Maps SAS' semantic date/time/datetime column types to the HOT 17 Intl cell
 * type names. SAS sends a HOT-agnostic flag (`date`/`time`/`datetime`); the
 * frontend owns the HOT-specific cell-type names:
 *
 * - `date`     → `intl-date`     (native date picker)
 * - `time`     → `intl-time`     (native time picker)
 * - `datetime` → `intl-datetime` (native datetime-local picker)
 *
 * The cell display is the raw stored ISO string (locale-independent) — see
 * registerIntlCellTypes, where these types' `valueFormatter` is the identity
 * passthrough. No locale/format meta is attached; it is unused for display, and
 * the validators are ISO-based and locale-agnostic. Runs in the DcValidator
 * constructor, before the rules reach Handsontable.
 *
 * @param rules Cell validation rules passed to Handsontable's `columns`.
 */
const INTL_TYPE_MAP: Record<string, string> = {
  date: 'intl-date',
  time: 'intl-time',
  datetime: 'intl-datetime'
}

export const mapIntlCellTypes = (rules: DcValidation[]): DcValidation[] => {
  for (const rule of rules) {
    const mapped = rule.type ? INTL_TYPE_MAP[rule.type as string] : undefined
    if (mapped) rule.type = mapped
  }

  return rules
}
