import { Revert } from '../types/Revert';

function hexCharToValue(char: u8): u8 {
    if (char >= 48 && char <= 57) {
        // '0' to '9'
        return char - 48;
    } else if (char >= 97 && char <= 102) {
        // 'a' to 'f'
        return char - 97 + 10;
    } else if (char >= 65 && char <= 70) {
        // 'A' to 'F'
        return char - 65 + 10;
    } else {
        throw new Revert('Invalid hex character: ' + String.fromCharCode(char));
    }
}

export function decodeHexArray(hex: string): u8[] {
    // Remove 0x prefix if present
    if (hex.startsWith('0x') || hex.startsWith('0X')) {
        hex = hex.substring(2);
    }

    // Validate length is even
    if (hex.length % 2 !== 0) {
        throw new Revert('Hex string must have even length');
    }

    const result = new Array<u8>(hex.length / 2);
    const hexBytes = String.UTF8.encode(hex);

    for (let i = 0; i < hex.length; i += 2) {
        const high = hexCharToValue(load<u8>(changetype<usize>(hexBytes) + i));
        const low = hexCharToValue(load<u8>(changetype<usize>(hexBytes) + i + 1));
        result[i / 2] = (high << 4) | low;
    }

    return result;
}

const hexLookupTable: StaticArray<u8> = [
    48, 48, 48, 49, 48, 50, 48, 51, 48, 52, 48, 53, 48, 54, 48, 55, 48, 56, 48, 57, 48, 97, 48, 98,
    48, 99, 48, 100, 48, 101, 48, 102, 49, 48, 49, 49, 49, 50, 49, 51, 49, 52, 49, 53, 49, 54, 49,
    55, 49, 56, 49, 57, 49, 97, 49, 98, 49, 99, 49, 100, 49, 101, 49, 102, 50, 48, 50, 49, 50, 50,
    50, 51, 50, 52, 50, 53, 50, 54, 50, 55, 50, 56, 50, 57, 50, 97, 50, 98, 50, 99, 50, 100, 50,
    101, 50, 102, 51, 48, 51, 49, 51, 50, 51, 51, 51, 52, 51, 53, 51, 54, 51, 55, 51, 56, 51, 57,
    51, 97, 51, 98, 51, 99, 51, 100, 51, 101, 51, 102, 52, 48, 52, 49, 52, 50, 52, 51, 52, 52, 52,
    53, 52, 54, 52, 55, 52, 56, 52, 57, 52, 97, 52, 98, 52, 99, 52, 100, 52, 101, 52, 102, 53, 48,
    53, 49, 53, 50, 53, 51, 53, 52, 53, 53, 53, 54, 53, 55, 53, 56, 53, 57, 53, 97, 53, 98, 53, 99,
    53, 100, 53, 101, 53, 102, 54, 48, 54, 49, 54, 50, 54, 51, 54, 52, 54, 53, 54, 54, 54, 55, 54,
    56, 54, 57, 54, 97, 54, 98, 54, 99, 54, 100, 54, 101, 54, 102, 55, 48, 55, 49, 55, 50, 55, 51,
    55, 52, 55, 53, 55, 54, 55, 55, 55, 56, 55, 57, 55, 97, 55, 98, 55, 99, 55, 100, 55, 101, 55,
    102, 56, 48, 56, 49, 56, 50, 56, 51, 56, 52, 56, 53, 56, 54, 56, 55, 56, 56, 56, 57, 56, 97, 56,
    98, 56, 99, 56, 100, 56, 101, 56, 102, 57, 48, 57, 49, 57, 50, 57, 51, 57, 52, 57, 53, 57, 54,
    57, 55, 57, 56, 57, 57, 57, 97, 57, 98, 57, 99, 57, 100, 57, 101, 57, 102, 97, 48, 97, 49, 97,
    50, 97, 51, 97, 52, 97, 53, 97, 54, 97, 55, 97, 56, 97, 57, 97, 97, 97, 98, 97, 99, 97, 100, 97,
    101, 97, 102, 98, 48, 98, 49, 98, 50, 98, 51, 98, 52, 98, 53, 98, 54, 98, 55, 98, 56, 98, 57,
    98, 97, 98, 98, 98, 99, 98, 100, 98, 101, 98, 102, 99, 48, 99, 49, 99, 50, 99, 51, 99, 52, 99,
    53, 99, 54, 99, 55, 99, 56, 99, 57, 99, 97, 99, 98, 99, 99, 99, 100, 99, 101, 99, 102, 100, 48,
    100, 49, 100, 50, 100, 51, 100, 52, 100, 53, 100, 54, 100, 55, 100, 56, 100, 57, 100, 97, 100,
    98, 100, 99, 100, 100, 100, 101, 100, 102, 101, 48, 101, 49, 101, 50, 101, 51, 101, 52, 101, 53,
    101, 54, 101, 55, 101, 56, 101, 57, 101, 97, 101, 98, 101, 99, 101, 100, 101, 101, 101, 102,
    102, 48, 102, 49, 102, 50, 102, 51, 102, 52, 102, 53, 102, 54, 102, 55, 102, 56, 102, 57, 102,
    97, 102, 98, 102, 99, 102, 100, 102, 101, 102, 102,
];

export function encodeHexUTF8(start: usize, len: usize): ArrayBuffer {
    const result = new ArrayBuffer(2 + <i32>len * 2);
    store<u16>(changetype<usize>(result), <u16>0x7830); // Stores "0x" prefix
    for (let i: usize = 0; i < len; i++) {
        store<u16>(
            2 + changetype<usize>(result) + i * 2,
            load<u16>(changetype<usize>(hexLookupTable) + 2 * load<u8>(start + i)),
        );
    }
    return result;
}

export function encodeHex(start: usize, len: usize): string {
    return String.UTF8.decode(encodeHexUTF8(start, len));
}

export function encodeHexFromBuffer(data: ArrayBuffer): string {
    return encodeHex(changetype<usize>(data), data.byteLength);
}
