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

const Calendar_box_destroy_registry = new FinalizationRegistry((ptr) => {
    wasm.icu4x_Calendar_destroy_mv1(ptr);
});

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

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


    /**
     * Creates a new {@link Calendar} for the specified kind, using compiled data.
     *
     * See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/calendar/enum.AnyCalendar.html#method.new) for more information.
     */
    #defaultConstructor(kind) {

        const result = wasm.icu4x_Calendar_create_mv1(kind.ffiValue);

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

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

    /**
     * Creates a new {@link Calendar} for the specified kind, using a particular data source.
     *
     * See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/calendar/enum.AnyCalendar.html#method.new) for more information.
     */
    static createWithProvider(provider, kind) {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);


        const result = wasm.icu4x_Calendar_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, kind.ffiValue);

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

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

    /**
     * Returns the kind of this calendar
     *
     * See the [Rust documentation for `kind`](https://docs.rs/icu/2.2.0/icu/calendar/enum.AnyCalendar.html#method.kind) for more information.
     */
    get kind() {

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

        try {
            return new CalendarKind(diplomatRuntime.internalConstructor, result);
        }

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

    /**
     * Creates a new {@link Calendar} for the specified kind, using compiled data.
     *
     * See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/calendar/enum.AnyCalendar.html#method.new) for more information.
     */
    constructor(kind) {
        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);
        }
    }
}