/**
 * Formatea fechas de alta / columnas de fecha a `dd-mm-aaaa` para la tabla de personas.
 * @param {unknown} value
 * @returns {string}
 */
export function formatPersonaDateDdMmYyyy(value) {
  if (!value) return '';
  if (typeof value === 'string') {
    const v = value.trim();
    const m1 = v.match(/^(\d{2})[/-](\d{2})[/-](\d{4})$/);
    if (m1) return `${m1[1]}-${m1[2]}-${m1[3]}`;

    const m2 = v.match(/^(\d{4})-(\d{2})-(\d{2})/);
    if (m2) return `${m2[3]}-${m2[2]}-${m2[1]}`;

    const d = new Date(v);
    if (!Number.isNaN(d.getTime())) {
      const dd = String(d.getDate()).padStart(2, '0');
      const mm = String(d.getMonth() + 1).padStart(2, '0');
      const yyyy = String(d.getFullYear());
      return `${dd}-${mm}-${yyyy}`;
    }
    return v;
  }

  if (value instanceof Date && !Number.isNaN(value.getTime())) {
    const dd = String(value.getDate()).padStart(2, '0');
    const mm = String(value.getMonth() + 1).padStart(2, '0');
    const yyyy = String(value.getFullYear());
    return `${dd}-${mm}-${yyyy}`;
  }

  return String(value);
}

/**
 * Iniciales a partir del nombre completo (dos palabras → dos letras).
 * @param {unknown} value
 * @returns {string}
 */
export function getPersonaNombreInitials(value) {
  const text = (value || '').trim();
  if (!text) return '';
  const words = text.split(/\s+/).filter(Boolean);
  if (words.length >= 2) {
    return `${words[0][0] ?? ''}${words[1][0] ?? ''}`.toUpperCase();
  }
  return `${words[0]?.[0] ?? ''}`.toUpperCase();
}
