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



/**
 * See the [Rust documentation for `TimeZoneAndCanonicalAndNormalized`](https://docs.rs/icu/2.1.1/icu/time/zone/iana/struct.TimeZoneAndCanonicalAndNormalized.html) for more information.
 */
export class TimeZoneAndCanonicalAndNormalized {
    #timeZone;
    get timeZone() {
        return this.#timeZone;
    }
    #canonical;
    get canonical() {
        return this.#canonical;
    }
    #normalized;
    get normalized() {
        return this.#normalized;
    }
    #internalConstructor(structObj, internalConstructor) {
        if (typeof structObj !== "object") {
            throw new Error("TimeZoneAndCanonicalAndNormalized's constructor takes an object of TimeZoneAndCanonicalAndNormalized's fields.");
        }

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

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

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

        return this;
    }

    // Return this struct in FFI function friendly format.
    // Returns an array that can be expanded with spread syntax (...)// If this struct contains any slices, their lifetime-edge-relevant information will be
    // set up here, and can be appended to any relevant lifetime arrays here. <lifetime>AppendArray accepts a list
    // of arrays for each lifetime to do so. It accepts multiple lists per lifetime in case the caller needs to tie a lifetime to multiple
    // output arrays. Null is equivalent to an empty list: this lifetime is not being borrowed from.
    _intoFFI(
        functionCleanupArena,
        appendArrayMap
    ) {
        let buffer = diplomatRuntime.DiplomatBuf.struct(wasm, 20, 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 TimeZoneAndCanonicalAndNormalized) {
            return obj;
        }

        return TimeZoneAndCanonicalAndNormalized.fromFields(obj);
    }

    _writeToArrayBuffer(
        arrayBuffer,
        offset,
        functionCleanupArena,
        appendArrayMap
    ) {
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#timeZone.ffiValue, Uint32Array);
        diplomatRuntime.CleanupArena.maybeCreateWith(functionCleanupArena, ...appendArrayMap['aAppendArray']).alloc(diplomatRuntime.DiplomatBuf.str8(wasm, this.#canonical)).writePtrLenToArrayBuffer(arrayBuffer, offset + 4);
        diplomatRuntime.CleanupArena.maybeCreateWith(functionCleanupArena, ...appendArrayMap['aAppendArray']).alloc(diplomatRuntime.DiplomatBuf.str8(wasm, this.#normalized)).writePtrLenToArrayBuffer(arrayBuffer, offset + 12);
    }

    static _fromFFI(internalConstructor, ptr, aEdges) {
        if (internalConstructor !== diplomatRuntime.internalConstructor) {
            throw new Error("TimeZoneAndCanonicalAndNormalized._fromFFI is not meant to be called externally. Please use the default constructor.");
        }
        let structObj = {};
        const timeZoneDeref = diplomatRuntime.ptrRead(wasm, ptr);
        structObj.timeZone = new TimeZone(diplomatRuntime.internalConstructor, timeZoneDeref, []);
        const canonicalDeref = ptr + 4;
        structObj.canonical = new diplomatRuntime.DiplomatSliceStr(wasm, canonicalDeref,  "string8", aEdges).getValue();
        const normalizedDeref = ptr + 12;
        structObj.normalized = new diplomatRuntime.DiplomatSliceStr(wasm, normalizedDeref,  "string8", aEdges).getValue();

        return new TimeZoneAndCanonicalAndNormalized(structObj, internalConstructor);
    }

    // Return all fields corresponding to lifetime `'a`
    // without handling lifetime dependencies (this is the job of the caller)
    // This is all fields that may be borrowed from if borrowing `'a`,
    // assuming that there are no `'other: a`. bounds. In case of such bounds,
    // the caller should take care to also call _fieldsForLifetimeOther
    get _fieldsForLifetimeA() {
        return [this.#canonical, this.#normalized];
    };


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