#include "libft.h"

// --- UTF-8 byte sequence macros for 2-byte superscript digits ---
#define TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1 0xC2

// Superscript digits: ¹, ², ³
#define SUPERSCRIPT_1_BYTE2 0xB9
#define SUPERSCRIPT_2_BYTE2 0xB2
#define SUPERSCRIPT_3_BYTE2 0xB3

// --- UTF-8 byte sequence macros for 3-byte superscript digits ---
#define THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1 0xE2
#define THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2 0x81

// Superscript digits: ⁰, ⁴, ⁵, ⁶, ⁷, ⁸, ⁹
#define SUPERSCRIPT_0_BYTE3 0xB0
#define SUPERSCRIPT_4_BYTE3 0xB4
#define SUPERSCRIPT_5_BYTE3 0xB5
#define SUPERSCRIPT_6_BYTE3 0xB6
#define SUPERSCRIPT_7_BYTE3 0xB7
#define SUPERSCRIPT_8_BYTE3 0xB8
#define SUPERSCRIPT_9_BYTE3 0xB9

/**
 * Checks if the UTF-8 character at `input` is a superscript digit (¹, ², ³, ⁰-⁹).
 * If it is, sets `*size` to the number of bytes in the character (2 or 3).
 * Returns 1 if true, 0 otherwise.
 */
int ft_isdigit_superscript(const char *input, int *size)
{
    if (size != NULL)
    {
        *size = 0; // Default to 0 if not a superscript digit
    }

    // --- Check for 2-byte superscript digits (¹, ², ³) ---
    if ((uint8_t)*input == TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
    {
        uint8_t second_byte = (uint8_t)*(input + 1);
        if (second_byte == SUPERSCRIPT_1_BYTE2 ||
            second_byte == SUPERSCRIPT_2_BYTE2 ||
            second_byte == SUPERSCRIPT_3_BYTE2)
        {
            if (size != NULL)
            {
                *size = 2; // 2-byte character
            }
            return 1; // Valid superscript digit (¹, ², or ³)
        }
    }
    // --- Check for 3-byte superscript digits (⁰, ⁴-⁹) ---
    else if ((uint8_t)*input == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
    {
        if ((uint8_t)*(input + 1) == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2)
        {
            uint8_t third_byte = (uint8_t)*(input + 2);
            if (third_byte == SUPERSCRIPT_0_BYTE3 ||
                third_byte == SUPERSCRIPT_4_BYTE3 ||
                third_byte == SUPERSCRIPT_5_BYTE3 ||
                third_byte == SUPERSCRIPT_6_BYTE3 ||
                third_byte == SUPERSCRIPT_7_BYTE3 ||
                third_byte == SUPERSCRIPT_8_BYTE3 ||
                third_byte == SUPERSCRIPT_9_BYTE3)
            {
                if (size != NULL)
                {
                    *size = 3; // 3-byte character
                }
                return 1; // Valid superscript digit (⁰, ⁴-⁹)
            }
        }
    }
    return 0;
}