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

const LocaleFallbackerWithConfig_box_destroy_registry = new FinalizationRegistry((ptr) => {
    wasm.icu4x_LocaleFallbackerWithConfig_destroy_mv1(ptr);
});

/**
 * An object that runs the ICU4X locale fallback algorithm with specific configurations.
 *
 * See the [Rust documentation for `LocaleFallbackerWithConfig`](https://docs.rs/icu/2.2.0/icu/locale/fallback/struct.LocaleFallbackerWithConfig.html) for more information.
 */
export class LocaleFallbackerWithConfig {
    // 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 = [];
    #aEdge = [];

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

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

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


    /**
     * Returns the associated config.
     *
     * See the [Rust documentation for `config`](https://docs.rs/icu/2.2.0/icu/locale/fallback/struct.LocaleFallbackerWithConfig.html#method.config) for more information.
     */
    config() {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 4, 4, false);


        const result = wasm.icu4x_LocaleFallbackerWithConfig_config_mv1(diplomatReceive.buffer, this.ffiValue);

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

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

    /**
     * Creates an iterator from a locale with each step of fallback.
     *
     * See the [Rust documentation for `fallback_for`](https://docs.rs/icu/2.2.0/icu/locale/fallback/struct.LocaleFallbacker.html#method.fallback_for) for more information.
     */
    fallbackForLocale(locale) {
        // This lifetime edge depends on lifetimes 'a, 'b
        let aEdges = [this];


        const result = wasm.icu4x_LocaleFallbackerWithConfig_fallback_for_locale_mv1(this.ffiValue, locale.ffiValue);

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

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

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