// Unicode 17.0.0 Default Case Conversion uppercase (und root — byte-identical
// for every tag without a casing tailoring; only tr/az and lt differ and
// would be separate components).
// Full mappings from UnicodeData + unconditional SpecialCasing (ß -> SS,
// ligatures -> letter sequences, ypogegrammeni -> prosgegrammeni). Unicode
// default uppercase has no context rules, unlike lowercase's Final_Sigma.
// input_utf8_cap makes valid UTF-8 a host precondition.
// Conformance: compliance/unicode-17-uppercase.comply.wasm.
// Tables generated by tools/casegen.
const tables = @import("lib/unicode-17-uppercase-tables.zig");
const utf8 = @import("lib/utf8.zig");
const std = @import("std");

const INPUT_CAP: usize = 1024 * 1024;
// Worst-case growth is 3x (e.g. U+0390, 2 bytes -> U+0399 U+0308 U+0301, 6 bytes).
const OUTPUT_CAP: usize = INPUT_CAP * 3;

var input_buf: [INPUT_CAP]u8 = undefined;
var output_buf: [OUTPUT_CAP]u8 = undefined;

export fn input_ptr() u32 {
    return @as(u32, @intCast(@intFromPtr(&input_buf)));
}

export fn input_utf8_cap() u32 {
    return @as(u32, @intCast(INPUT_CAP));
}

export fn output_utf8_cap() u32 {
    return @as(u32, @intCast(OUTPUT_CAP));
}

// Returns the mapped UTF-8 bytes, or null when cp uppercases to itself.
fn lookupUpper(cp: u32) ?[]const u8 {
    var lo: usize = 0;
    var hi: usize = tables.map_keys.len;
    while (lo < hi) {
        const mid = lo + (hi - lo) / 2;
        if (tables.map_keys[mid] < cp) {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }
    if (lo >= tables.map_keys.len or tables.map_keys[lo] != cp) return null;
    const packed_val = tables.map_packed[lo];
    const off = packed_val >> 8;
    const len = packed_val & 0xFF;
    return tables.map_blob[off .. off + len];
}

fn emit(out: *usize, bytes: []const u8) void {
    if (out.* + bytes.len > OUTPUT_CAP) @trap();
    for (bytes) |b| {
        output_buf[out.*] = b;
        out.* += 1;
    }
}

fn renderImpl(input_size_in: u32) u32 {
    const input_size: usize = @intCast(input_size_in);
    if (input_size > INPUT_CAP) @trap();

    const input = input_buf[0..input_size];
    var out: usize = 0;

    var i: usize = 0;
    while (i < input_size) {
        const d = utf8.decode(input[i..]);

        if (!d.valid) @trap();

        if (lookupUpper(d.cp)) |upper| {
            emit(&out, upper);
        } else {
            emit(&out, input[i .. i + d.size]);
        }

        i += d.size;
    }

    return @as(u32, @intCast(out));
}

export fn render(input_size_in: u32) packed struct(u64) {
    output_size: u32,
    output_ptr: u31,
    failed: u1,
} {
    return .{
        .output_size = renderImpl(input_size_in),
        .output_ptr = @intCast(@intFromPtr(&output_buf)),
        .failed = 0,
    };
}

fn utf8Len(cp: u32) usize {
    if (cp <= 0x7f) return 1;
    if (cp <= 0x7ff) return 2;
    if (cp <= 0xffff) return 3;
    return 4;
}

test "every generated mapping fits the three-times byte bound" {
    for (tables.map_keys, tables.map_packed) |cp, packed_value| {
        const mapped_len: usize = @intCast(packed_value & 0xff);
        try std.testing.expect(mapped_len <= utf8Len(cp) * 3);
    }
}

test "maximum input fits at the worst-case uppercase expansion" {
    var i: usize = 0;
    while (i < INPUT_CAP) : (i += 2) {
        input_buf[i] = 0xce;
        input_buf[i + 1] = 0x90; // U+0390
    }

    try std.testing.expectEqual(@as(u32, OUTPUT_CAP), renderImpl(INPUT_CAP));
}
