import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/utils/emoji_detection.dart';

void main() {
  group('standaloneEmojiFontSize', () {
    test('returns null for empty or whitespace-only text', () {
      expect(standaloneEmojiFontSize(''), isNull);
      expect(standaloneEmojiFontSize('   '), isNull);
    });

    test('returns null for plain text', () {
      expect(standaloneEmojiFontSize('Hallo'), isNull);
      expect(standaloneEmojiFontSize('ok'), isNull);
    });

    test('returns null when emoji is mixed with text', () {
      expect(standaloneEmojiFontSize('Hallo 😀'), isNull);
      expect(standaloneEmojiFontSize('😀 super'), isNull);
    });

    test('enlarges a single emoji the most', () {
      expect(standaloneEmojiFontSize('😀'), 34);
      expect(standaloneEmojiFontSize('👍'), 34);
    });

    test('enlarges up to three emojis at the largest size', () {
      expect(standaloneEmojiFontSize('😀😀😀'), 34);
      expect(standaloneEmojiFontSize('😀 😀 😀'), 34);
    });

    test('does not enlarge more than three emojis', () {
      expect(standaloneEmojiFontSize('😀😀😀😀'), isNull);
      expect(standaloneEmojiFontSize('😀😀😀😀😀😀'), isNull);
    });

    test('handles ZWJ sequences as a single emoji', () {
      // Family emoji: multiple codepoints joined via ZWJ → one cluster.
      expect(standaloneEmojiFontSize('👨‍👩‍👧‍👦'), 34);
    });

    test('handles skin-tone modified emoji as a single emoji', () {
      expect(standaloneEmojiFontSize('👍🏽'), 34);
    });

    test('handles emoji with variation selectors', () {
      expect(standaloneEmojiFontSize('❤️'), 34);
      expect(standaloneEmojiFontSize('⭐'), 34);
    });

    test('handles keycap emoji', () {
      expect(standaloneEmojiFontSize('1️⃣'), 34);
    });

    test('handles flag emoji (regional indicators)', () {
      expect(standaloneEmojiFontSize('🇩🇪'), 34);
    });

    test('bare digits without keycap are not emoji', () {
      expect(standaloneEmojiFontSize('123'), isNull);
    });
  });

  group('extractEmojis', () {
    test('returns empty list for plain text', () {
      expect(extractEmojis('Hallo Welt'), isEmpty);
      expect(extractEmojis(''), isEmpty);
    });

    test('extracts emojis from mixed text, ties keep first appearance', () {
      expect(extractEmojis('Super 👍 gemacht 🎉'), ['👍', '🎉']);
    });

    test('sorts by frequency, most-frequent first', () {
      // 🎉 appears first but 👍 occurs more often → 👍 wins.
      expect(extractEmojis('🎉 👍 super 👍'), ['👍', '🎉']);
    });

    test('dedupes repeated emojis, most-frequent first', () {
      expect(extractEmojis('😀 😀 🚀 😀'), ['😀', '🚀']);
    });

    test('extracts composite emojis as single entries', () {
      expect(extractEmojis('Familie 👨‍👩‍👧‍👦 und 👍🏽'), ['👨‍👩‍👧‍👦', '👍🏽']);
    });

    test('extracts flag emojis', () {
      expect(extractEmojis('Hallo 🇩🇪'), ['🇩🇪']);
    });
  });

  group('emojiSkinToneNeutral', () {
    test('strips skin-tone modifiers', () {
      expect(emojiSkinToneNeutral('👍🏻'), '👍');
      expect(emojiSkinToneNeutral('👍🏿'), '👍');
    });

    test('leaves emojis without skin tone unchanged', () {
      expect(emojiSkinToneNeutral('👍'), '👍');
      expect(emojiSkinToneNeutral('❤️'), '❤️');
      expect(emojiSkinToneNeutral('🎉'), '🎉');
    });

    test('tone variants compare equal after normalization', () {
      expect(
        emojiSkinToneNeutral('👍🏻'),
        emojiSkinToneNeutral('👍🏽'),
      );
    });
  });
}
