// generated by diplomat-tool
import wasm from "./diplomat-wasm.mjs";
import * as diplomatRuntime from "./diplomat-runtime.mjs";



/**
 * The outcome of non-recursive canonical decomposition of a character.
 * `second` will be NUL when the decomposition expands to a single character
 * (which may or may not be the original one)
 *
 * See the [Rust documentation for `Decomposed`](https://docs.rs/icu/2.1.1/icu/normalizer/properties/enum.Decomposed.html) for more information.
 */
export class Decomposed {
    #first;
    get first() {
        return this.#first;
    }
    #second;
    get second() {
        return this.#second;
    }
    #internalConstructor(structObj, internalConstructor) {
        if (typeof structObj !== "object") {
            throw new Error("Decomposed's constructor takes an object of Decomposed's fields.");
        }

        if (internalConstructor !== diplomatRuntime.internalConstructor) {
            throw new Error("Decomposed is an out struct and can only be created internally.");
        }
        if ("first" in structObj) {
            this.#first = structObj.first;
        } else {
            throw new Error("Missing required field first.");
        }

        if ("second" in structObj) {
            this.#second = structObj.second;
        } else {
            throw new Error("Missing required field second.");
        }

        return this;
    }

    // Return this struct in FFI function friendly format.
    // Returns an array that can be expanded with spread syntax (...)
    _intoFFI(
        functionCleanupArena,
        appendArrayMap
    ) {
        let buffer = diplomatRuntime.DiplomatBuf.struct(wasm, 8, 4);

        this._writeToArrayBuffer(wasm.memory.buffer, buffer.ptr, functionCleanupArena, appendArrayMap);

        functionCleanupArena.alloc(buffer);

        return buffer.ptr;
    }

    static _fromSuppliedValue(internalConstructor, obj) {
        if (internalConstructor !== diplomatRuntime.internalConstructor) {
            throw new Error("_fromSuppliedValue cannot be called externally.");
        }

        if (obj instanceof Decomposed) {
            return obj;
        }

        return Decomposed.fromFields(obj);
    }

    _writeToArrayBuffer(
        arrayBuffer,
        offset,
        functionCleanupArena,
        appendArrayMap
    ) {
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#first, Uint32Array);
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#second, Uint32Array);
    }

    // This struct contains borrowed fields, so this takes in a list of
    // "edges" corresponding to where each lifetime's data may have been borrowed from
    // and passes it down to individual fields containing the borrow.
    // This method does not attempt to handle any dependencies between lifetimes, the caller
    // should handle this when constructing edge arrays.
    static _fromFFI(internalConstructor, ptr) {
        if (internalConstructor !== diplomatRuntime.internalConstructor) {
            throw new Error("Decomposed._fromFFI is not meant to be called externally. Please use the default constructor.");
        }
        let structObj = {};
        const firstDeref = (new Uint32Array(wasm.memory.buffer, ptr, 1))[0];
        structObj.first = firstDeref;
        const secondDeref = (new Uint32Array(wasm.memory.buffer, ptr + 4, 1))[0];
        structObj.second = secondDeref;

        return new Decomposed(structObj, internalConstructor);
    }


    constructor(structObj, internalConstructor) {
        return this.#internalConstructor(...arguments)
    }
}