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



/**
 * See the [Rust documentation for `TitlecaseOptions`](https://docs.rs/icu/2.1.1/icu/casemap/options/struct.TitlecaseOptions.html) for more information.
 */
export class TitlecaseOptions {
    #leadingAdjustment;
    get leadingAdjustment() {
        return this.#leadingAdjustment;
    }
    set leadingAdjustment(value){
        this.#leadingAdjustment = value;
    }
    #trailingCase;
    get trailingCase() {
        return this.#trailingCase;
    }
    set trailingCase(value){
        this.#trailingCase = value;
    }
    /** @internal */
    static fromFields(structObj) {
        return new TitlecaseOptions(diplomatRuntime.exposeConstructor, structObj);
    }

    #internalConstructor(structObj) {
        if (typeof structObj !== "object") {
            throw new Error("TitlecaseOptions's constructor takes an object of TitlecaseOptions's fields.");
        }

        if ("leadingAdjustment" in structObj) {
            this.#leadingAdjustment = structObj.leadingAdjustment;
        } else {
            this.#leadingAdjustment = null;
        }

        if ("trailingCase" in structObj) {
            this.#trailingCase = structObj.trailingCase;
        } else {
            this.#trailingCase = null;
        }

        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, 16, 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 TitlecaseOptions) {
            return obj;
        }

        return TitlecaseOptions.fromFields(obj);
    }

    _writeToArrayBuffer(
        arrayBuffer,
        offset,
        functionCleanupArena,
        appendArrayMap
    ) {
        diplomatRuntime.writeOptionToArrayBuffer(arrayBuffer, offset + 0, this.#leadingAdjustment, 4, 4, (arrayBuffer, offset, jsValue) => diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array));
        diplomatRuntime.writeOptionToArrayBuffer(arrayBuffer, offset + 8, this.#trailingCase, 4, 4, (arrayBuffer, offset, jsValue) => diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array));
    }

    // 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("TitlecaseOptions._fromFFI is not meant to be called externally. Please use the default constructor.");
        }
        let structObj = {};
        const leadingAdjustmentDeref = ptr;
        structObj.leadingAdjustment = diplomatRuntime.readOption(wasm, leadingAdjustmentDeref, 4, (wasm, offset) => { const deref = diplomatRuntime.enumDiscriminant(wasm, offset); return new LeadingAdjustment(diplomatRuntime.internalConstructor, deref) });
        const trailingCaseDeref = ptr + 8;
        structObj.trailingCase = diplomatRuntime.readOption(wasm, trailingCaseDeref, 4, (wasm, offset) => { const deref = diplomatRuntime.enumDiscriminant(wasm, offset); return new TrailingCase(diplomatRuntime.internalConstructor, deref) });

        return new TitlecaseOptions(diplomatRuntime.exposeConstructor, structObj);
    }


    /**
     * See the [Rust documentation for `default`](https://docs.rs/icu/2.1.1/icu/casemap/options/struct.TitlecaseOptions.html#method.default) for more information.
     */
    #defaultConstructor() {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 16, 4, false);


        const result = wasm.icu4x_TitlecaseOptionsV1_default_mv1(diplomatReceive.buffer);

        try {
            return TitlecaseOptions._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
        }

        finally {
            diplomatReceive.free();
        }
    }

    /**
     * See the [Rust documentation for `default`](https://docs.rs/icu/2.1.1/icu/casemap/options/struct.TitlecaseOptions.html#method.default) for more information.
     */
    constructor() {
        if (arguments[0] === diplomatRuntime.exposeConstructor) {
            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
            return this.#internalConstructor(...arguments);
        } else {
            return this.#defaultConstructor(...arguments);
        }
    }
}