const _romanMap = <String, int>{
  'T': 10000,
  'A': 5000,
  'P': 100000,
  'E': 100000, // not used but kept for completeness
  'M': 1000,
  'D': 500,
  'C': 100,
  'L': 50,
  'X': 10,
  'V': 5,
  'I': 1,
};

String intToRoman(int n) {
  if (n <= 0) return '';

  final buffer = StringBuffer();

  // 로마 숫자 변환 테이블 (내림차순)
  const values = [
    (10000, 'T'),
    (9000, 'MT'),
    (5000, 'A'),
    (4000, 'MA'),
    (1000, 'M'),
    (900, 'CM'),
    (500, 'D'),
    (400, 'CD'),
    (100, 'C'),
    (90, 'XC'),
    (50, 'L'),
    (40, 'XL'),
    (10, 'X'),
    (9, 'IX'),
    (5, 'V'),
    (4, 'IV'),
    (1, 'I'),
  ];

  for (final (value, numeral) in values) {
    while (n >= value) {
      buffer.write(numeral);
      n -= value;
    }
  }

  return buffer.toString();
}

int romanToInt(String n) {
  var result = 0;
  var i = 0;
  while (i < n.length) {
    final one = _romanMap[n[i]] ?? 0;
    final two = i + 1 < n.length ? _romanMap[n[i + 1]] ?? 0 : 0;
    if (two > one) {
      result += (two - one);
      i += 2;
    } else {
      result += one;
      i += 1;
    }
  }
  return result;
}
