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

const TimeZone_box_destroy_registry = new FinalizationRegistry((ptr) => {
    wasm.icu4x_TimeZone_destroy_mv1(ptr);
});

/**
 * See the [Rust documentation for `TimeZone`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html) for more information.
 */
export class TimeZone {
    // Internal ptr reference:
    #ptr = null;

    // Lifetimes are only to keep dependencies alive.
    // Since JS won't garbage collect until there are no incoming edges.
    #selfEdge = [];

    #internalConstructor(symbol, ptr, selfEdge) {
        if (symbol !== diplomatRuntime.internalConstructor) {
            console.error("TimeZone is an Opaque type. You cannot call its constructor.");
            return;
        }
        this.#ptr = ptr;
        this.#selfEdge = selfEdge;

        // Are we being borrowed? If not, we can register.
        if (this.#selfEdge.length === 0) {
            TimeZone_box_destroy_registry.register(this, this.#ptr);
        }

        return this;
    }
    /** @internal */
    get ffiValue() {
        return this.#ptr;
    }


    /**
     * The unknown time zone.
     *
     * See the [Rust documentation for `unknown`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZoneInfo.html#method.unknown) for more information.
     */
    static unknown() {

        const result = wasm.icu4x_TimeZone_unknown_mv1();

        try {
            return new TimeZone(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
        }
    }

    /**
     * Whether the time zone is the unknown zone.
     *
     * See the [Rust documentation for `is_unknown`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html#method.is_unknown) for more information.
     */
    isUnknown() {

        const result = wasm.icu4x_TimeZone_is_unknown_mv1(this.ffiValue);

        try {
            return result;
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
        }
    }

    /**
     * Construct a {@link TimeZone} from an IANA time zone ID.
     *
     * See the [Rust documentation for `from_iana_id`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html#method.from_iana_id) for more information.
     */
    static createFromIanaId(ianaId) {
        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
        const ianaIdSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, ianaId)));
        // This lifetime edge depends on lifetimes 'a
        let aEdges = [ianaIdSlice];


        const result = wasm.icu4x_TimeZone_create_from_iana_id_mv1(ianaIdSlice.ptr);

        try {
            return new TimeZone(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            functionGarbageCollectorGrip.releaseToGarbageCollector();

        }
    }

    /**
     * Construct a {@link TimeZone} from a Windows time zone ID and region.
     *
     * See the [Rust documentation for `from_windows_id`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html#method.from_windows_id) for more information.
     */
    static createFromWindowsId(windowsId, region) {
        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
        const windowsIdSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, windowsId)));
        const regionSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, region)));
        // This lifetime edge depends on lifetimes 'a
        let aEdges = [windowsIdSlice, regionSlice];


        const result = wasm.icu4x_TimeZone_create_from_windows_id_mv1(windowsIdSlice.ptr, regionSlice.ptr);

        try {
            return new TimeZone(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            functionGarbageCollectorGrip.releaseToGarbageCollector();

        }
    }

    /**
     * Construct a {@link TimeZone} from the platform-specific ID.
     *
     * See the [Rust documentation for `from_system_id`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html#method.from_system_id) for more information.
     */
    static createFromSystemId(id, region) {
        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
        const idSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, id)));
        const regionSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, region)));
        // This lifetime edge depends on lifetimes 'a
        let aEdges = [idSlice, regionSlice];


        const result = wasm.icu4x_TimeZone_create_from_system_id_mv1(idSlice.ptr, regionSlice.ptr);

        try {
            return new TimeZone(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            functionGarbageCollectorGrip.releaseToGarbageCollector();

        }
    }

    /**
     * Creates a time zone from a BCP-47 string.
     *
     * Returns the unknown time zone if the string is not a valid BCP-47 subtag.
     *
     * Additional information: [1](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html)
     */
    static createFromBcp47(id) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

        const idSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, id)));

        const result = wasm.icu4x_TimeZone_create_from_bcp47_mv1(idSlice.ptr);

        try {
            return new TimeZone(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            functionCleanupArena.free();

        }
    }

    /**
     * See the [Rust documentation for `with_offset`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html#method.with_offset) for more information.
     */
    withOffset(offset) {

        const result = wasm.icu4x_TimeZone_with_offset_mv1(this.ffiValue, offset.ffiValue);

        try {
            return new TimeZoneInfo(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
        }
    }

    /**
     * See the [Rust documentation for `without_offset`](https://docs.rs/icu/2.2.0/icu/time/struct.TimeZone.html#method.without_offset) for more information.
     */
    withoutOffset() {

        const result = wasm.icu4x_TimeZone_without_offset_mv1(this.ffiValue);

        try {
            return new TimeZoneInfo(diplomatRuntime.internalConstructor, result, []);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
        }
    }

    constructor(symbol, ptr, selfEdge) {
        return this.#internalConstructor(...arguments)
    }
}