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

const DecimalFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => {
    wasm.icu4x_DecimalFormatter_destroy_mv1(ptr);
});

/**
 * An ICU4X Decimal Format object, capable of formatting a {@link Decimal} as a string.
 *
 * See the [Rust documentation for `DecimalFormatter`](https://docs.rs/icu/2.2.0/icu/decimal/struct.DecimalFormatter.html) for more information.
 */
export class DecimalFormatter {
    // 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("DecimalFormatter 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) {
            DecimalFormatter_box_destroy_registry.register(this, this.#ptr);
        }

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


    /**
     * Creates a new {@link DecimalFormatter}, using compiled data
     *
     * See the [Rust documentation for `try_new`](https://docs.rs/icu/2.2.0/icu/decimal/struct.DecimalFormatter.html#method.try_new) for more information.
     */
    static createWithGroupingStrategy(locale, groupingStrategy) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

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


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

        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 DecimalFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
        }

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

            diplomatReceive.free();
        }
    }

    /**
     * Creates a new {@link DecimalFormatter}, using a particular data source.
     *
     * See the [Rust documentation for `try_new`](https://docs.rs/icu/2.2.0/icu/decimal/struct.DecimalFormatter.html#method.try_new) for more information.
     */
    static createWithGroupingStrategyAndProvider(provider, locale, groupingStrategy) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

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


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

        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 DecimalFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
        }

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

            diplomatReceive.free();
        }
    }

    /**
     * Creates a new {@link DecimalFormatter} from preconstructed locale data.
     *
     * See the [Rust documentation for `DecimalSymbolsV1`](https://docs.rs/icu/2.2.0/icu/decimal/provider/struct.DecimalSymbolsV1.html) for more information.
     */
    static createWithManualData(plusSignPrefix, plusSignSuffix, minusSignPrefix, minusSignSuffix, decimalSeparator, groupingSeparator, primaryGroupSize, secondaryGroupSize, minGroupSize, digits, groupingStrategy) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

        const plusSignPrefixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, plusSignPrefix)));
        const plusSignSuffixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, plusSignSuffix)));
        const minusSignPrefixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, minusSignPrefix)));
        const minusSignSuffixSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, minusSignSuffix)));
        const decimalSeparatorSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, decimalSeparator)));
        const groupingSeparatorSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, groupingSeparator)));
        const digitsSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.slice(wasm, digits, "u16")));
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);


        const result = wasm.icu4x_DecimalFormatter_create_with_manual_data_mv1(diplomatReceive.buffer, plusSignPrefixSlice.ptr, plusSignSuffixSlice.ptr, minusSignPrefixSlice.ptr, minusSignSuffixSlice.ptr, decimalSeparatorSlice.ptr, groupingSeparatorSlice.ptr, primaryGroupSize, secondaryGroupSize, minGroupSize, digitsSlice.ptr, diplomatRuntime.optionToBufferForCalling(wasm, groupingStrategy, 4, 4, functionCleanupArena, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]));

        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 DecimalFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
        }

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

            diplomatReceive.free();
        }
    }

    /**
     * Formats a {@link Decimal} to a string.
     *
     * See the [Rust documentation for `format`](https://docs.rs/icu/2.2.0/icu/decimal/struct.DecimalFormatter.html#method.format) for more information.
     */
    format(value) {
        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);

    wasm.icu4x_DecimalFormatter_format_mv1(this.ffiValue, value.ffiValue, write.buffer);

        try {
            return write.readString8();
        }

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

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