/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

/* Generated with cbindgen:0.29.2 */

#include <stdint.h>
#include <stddef.h>

namespace Web {
namespace HTML {
namespace Parser {

enum class CssTokenType {
    Invalid,
    EndOfFile,
    Ident,
    Function,
    AtKeyword,
    Hash,
    String,
    BadString,
    Url,
    BadUrl,
    Delim,
    Number,
    Percentage,
    Dimension,
    Whitespace,
    CDO,
    CDC,
    Colon,
    Semicolon,
    Comma,
    OpenSquare,
    CloseSquare,
    OpenParen,
    CloseParen,
    OpenCurly,
    CloseCurly,
};

enum class CssHashType {
    Id,
    Unrestricted,
};

enum class CssNumberType {
    Number,
    IntegerWithExplicitSign,
    Integer,
};

struct CssToken {
    CssTokenType token_type;
    CssHashType hash_type;
    CssNumberType number_type;
    double number_value;
    uint32_t delim;
    const uint16_t *value_ptr;
    size_t value_len;
    const uint8_t *original_source_ptr;
    size_t original_source_len;
    size_t start_line;
    size_t start_column;
    size_t end_line;
    size_t end_column;
};

extern "C" {

/// # Safety
/// - `input` and `input_len` must point to a valid string
/// - `ctx` must be a valid pointer to a CallbackContext
/// - Parameters provided to `callback` must be valid pointers
void rust_css_tokenize(const uint8_t *input, size_t input_len, void *ctx, void (*callback)(void *ctx,
                                                                                           const CssToken *token));

extern uint8_t *ladybird_rust_alloc(size_t size, size_t alignment);

extern uint8_t *ladybird_rust_alloc_zeroed(size_t size, size_t alignment);

extern void ladybird_rust_dealloc(uint8_t *ptr, size_t alignment);

extern uint8_t *ladybird_rust_realloc(uint8_t *ptr, size_t old_size, size_t new_size, size_t alignment);

/// Attempts to detect the character encoding of a byte stream using frequency analysis.
///
/// This implements step 8 of the WHATWG encoding sniffing algorithm:
/// https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding
///
/// # Safety
/// - `input` and `input_len` must describe a valid byte slice (or `input` may be null if
///   `input_len` is 0)
/// - `tld` if non-null, must describe a valid byte slice of `tld_len` bytes containing the
///   rightmost DNS label of the resource's host, with no dots, no uppercase, and only ASCII
///   characters — these constraints are required by chardetng and must be validated by the caller
/// - `out_encoding_name` and `out_encoding_name_len` must be non-null writable pointers
///
/// Returns `true` if an encoding was detected (always, unless the input pointer is invalid).
/// When `true` is returned, `*out_encoding_name` is set to a pointer into a static ASCII
/// string naming the detected encoding (e.g. `"windows-1252"`, `"Shift_JIS"`), and
/// `*out_encoding_name_len` is set to its byte length. The pointer is valid for the lifetime
/// of the process. When `false` is returned (only on null-pointer error), the output pointers
/// are left unmodified.
bool rust_detect_encoding(const uint8_t *input,
                          size_t input_len,
                          const uint8_t *tld,
                          size_t tld_len,
                          bool allow_utf8,
                          const uint8_t **out_encoding_name,
                          size_t *out_encoding_name_len);

}  // extern "C"

}  // namespace Parser
}  // namespace HTML
}  // namespace Web
