// generated by diplomat-tool
import { DataProvider } from "./DataProvider.mjs"
import { DateTimeAlignment } from "./DateTimeAlignment.mjs"
import { DateTimeFormatterLoadError } from "./DateTimeFormatterLoadError.mjs"
import { DateTimeLength } from "./DateTimeLength.mjs"
import { Locale } from "./Locale.mjs"
import { Time } from "./Time.mjs"
import { TimePrecision } from "./TimePrecision.mjs"
import wasm from "./diplomat-wasm.mjs";
import * as diplomatRuntime from "./diplomat-runtime.mjs";

const TimeFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => {
    wasm.icu4x_TimeFormatter_destroy_mv1(ptr);
});

/**
 * See the [Rust documentation for `NoCalendarFormatter`](https://docs.rs/icu/2.2.0/icu/datetime/type.NoCalendarFormatter.html) for more information.
 */
export class TimeFormatter {
    // 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("TimeFormatter 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) {
            TimeFormatter_box_destroy_registry.register(this, this.#ptr);
        }

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


    /**
     * See the [Rust documentation for `try_new`](https://docs.rs/icu/2.2.0/icu/datetime/type.NoCalendarFormatter.html#method.try_new) for more information.
     *
     * See the [Rust documentation for `T`](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html) for more information.
     *
     * Additional information: [1](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.with_time_precision), [2](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.with_alignment), [3](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.for_length)
     */
    #defaultConstructor(locale, length, timePrecision, alignment) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);


        const result = wasm.icu4x_TimeFormatter_create_mv1(diplomatReceive.buffer, locale.ffiValue, diplomatRuntime.optionToBufferForCalling(wasm, length, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), diplomatRuntime.optionToBufferForCalling(wasm, timePrecision, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), diplomatRuntime.optionToBufferForCalling(wasm, alignment, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]));

        try {
            if (!diplomatReceive.resultFlag) {
                const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
                throw new globalThis.Error('DateTimeFormatterLoadError.' + cause.value, { cause });
            }
            return new TimeFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
        }

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

            diplomatReceive.free();
        }
    }

    /**
     * See the [Rust documentation for `try_new`](https://docs.rs/icu/2.2.0/icu/datetime/type.NoCalendarFormatter.html#method.try_new) for more information.
     *
     * See the [Rust documentation for `T`](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html) for more information.
     *
     * Additional information: [1](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.with_time_precision), [2](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.with_alignment), [3](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.for_length)
     */
    static createWithProvider(provider, locale, length, timePrecision, alignment) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);


        const result = wasm.icu4x_TimeFormatter_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, diplomatRuntime.optionToBufferForCalling(wasm, length, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), diplomatRuntime.optionToBufferForCalling(wasm, timePrecision, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), diplomatRuntime.optionToBufferForCalling(wasm, alignment, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]));

        try {
            if (!diplomatReceive.resultFlag) {
                const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
                throw new globalThis.Error('DateTimeFormatterLoadError.' + cause.value, { cause });
            }
            return new TimeFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
        }

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

            diplomatReceive.free();
        }
    }

    /**
     * See the [Rust documentation for `format`](https://docs.rs/icu/2.2.0/icu/datetime/type.NoCalendarFormatter.html#method.format) for more information.
     */
    format(time) {
        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);

    wasm.icu4x_TimeFormatter_format_mv1(this.ffiValue, time.ffiValue, write.buffer);

        try {
            return write.readString8();
        }

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

    /**
     * See the [Rust documentation for `try_new`](https://docs.rs/icu/2.2.0/icu/datetime/type.NoCalendarFormatter.html#method.try_new) for more information.
     *
     * See the [Rust documentation for `T`](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html) for more information.
     *
     * Additional information: [1](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.with_time_precision), [2](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.with_alignment), [3](https://docs.rs/icu/2.2.0/icu/datetime/fieldsets/struct.T.html#method.for_length)
     */
    constructor(locale, length, timePrecision, alignment) {
        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);
        }
    }
}