const GSM7_BASIC =
  "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ§¿abcdefghijklmnopqrstuvwxyzäöñüà";
const GSM7_EXT = new Set(["^", "{", "}", "\\", "[", "~", "]", "|", "€"]);
const GSM7 = new Set([...GSM7_BASIC]);

function isGsm7(text: string): boolean {
  for (const ch of text) {
    if (!GSM7.has(ch) && !GSM7_EXT.has(ch)) {
      return false;
    }
  }
  return true;
}

export function smsSegmentStats(text: string): { chars: number; segments: number } {
  const chars = [...text].length;
  if (chars === 0) {
    return { chars: 0, segments: 0 };
  }
  if (isGsm7(text)) {
    let units = 0;
    for (const ch of text) {
      units += GSM7_EXT.has(ch) ? 2 : 1;
    }
    return { chars, segments: units <= 160 ? 1 : Math.ceil(units / 153) };
  }
  return { chars, segments: chars <= 70 ? 1 : Math.ceil(chars / 67) };
}
