package org.unicode.icu4x;
import com.sun.jna.Callback
import com.sun.jna.Library
import com.sun.jna.Native
import com.sun.jna.Pointer
import com.sun.jna.Structure

internal interface ListFormatterLib: Library {
    fun icu4x_ListFormatter_destroy_mv1(handle: Pointer)
    fun icu4x_ListFormatter_create_and_with_length_mv1(locale: Pointer, length: Int): ResultPointerInt
    fun icu4x_ListFormatter_create_and_with_length_and_provider_mv1(provider: Pointer, locale: Pointer, length: Int): ResultPointerInt
    fun icu4x_ListFormatter_create_or_with_length_mv1(locale: Pointer, length: Int): ResultPointerInt
    fun icu4x_ListFormatter_create_or_with_length_and_provider_mv1(provider: Pointer, locale: Pointer, length: Int): ResultPointerInt
    fun icu4x_ListFormatter_create_unit_with_length_mv1(locale: Pointer, length: Int): ResultPointerInt
    fun icu4x_ListFormatter_create_unit_with_length_and_provider_mv1(provider: Pointer, locale: Pointer, length: Int): ResultPointerInt
    fun icu4x_ListFormatter_format_utf16_mv1(handle: Pointer, list: Slice, write: Pointer): Unit
}
/** See the [Rust documentation for `ListFormatter`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html) for more information.
*/
class ListFormatter internal constructor (
    internal val handle: Pointer,
    // These ensure that anything that is borrowed is kept alive and not cleaned
    // up by the garbage collector.
    internal val selfEdges: List<Any>,
    internal var owned: Boolean,
)  {

    init {
        if (this.owned) {
            this.registerCleaner()
        }
    }

    private class ListFormatterCleaner(val handle: Pointer, val lib: ListFormatterLib) : Runnable {
        override fun run() {
            lib.icu4x_ListFormatter_destroy_mv1(handle)
        }
    }
    private fun registerCleaner() {
        CLEANER.register(this, ListFormatter.ListFormatterCleaner(handle, ListFormatter.lib));
    }

    companion object {
        internal val libClass: Class<ListFormatterLib> = ListFormatterLib::class.java
        internal val lib: ListFormatterLib = Native.load("icu4x", libClass)
        @JvmStatic
        
        /** Construct a new `ListFormatter` instance for And patterns from compiled data.
        *
        *See the [Rust documentation for `try_new_and`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.try_new_and) for more information.
        */
        fun createAndWithLength(locale: Locale, length: ListLength): Result<ListFormatter> {
            
            val returnVal = lib.icu4x_ListFormatter_create_and_with_length_mv1(locale.handle, length.toNative());
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ListFormatter(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `ListFormatter` instance for And patterns
        *
        *See the [Rust documentation for `try_new_and`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.try_new_and) for more information.
        */
        fun createAndWithLengthAndProvider(provider: DataProvider, locale: Locale, length: ListLength): Result<ListFormatter> {
            
            val returnVal = lib.icu4x_ListFormatter_create_and_with_length_and_provider_mv1(provider.handle, locale.handle, length.toNative());
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ListFormatter(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `ListFormatter` instance for And patterns from compiled data.
        *
        *See the [Rust documentation for `try_new_or`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.try_new_or) for more information.
        */
        fun createOrWithLength(locale: Locale, length: ListLength): Result<ListFormatter> {
            
            val returnVal = lib.icu4x_ListFormatter_create_or_with_length_mv1(locale.handle, length.toNative());
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ListFormatter(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `ListFormatter` instance for And patterns
        *
        *See the [Rust documentation for `try_new_or`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.try_new_or) for more information.
        */
        fun createOrWithLengthAndProvider(provider: DataProvider, locale: Locale, length: ListLength): Result<ListFormatter> {
            
            val returnVal = lib.icu4x_ListFormatter_create_or_with_length_and_provider_mv1(provider.handle, locale.handle, length.toNative());
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ListFormatter(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `ListFormatter` instance for And patterns from compiled data.
        *
        *See the [Rust documentation for `try_new_unit`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.try_new_unit) for more information.
        */
        fun createUnitWithLength(locale: Locale, length: ListLength): Result<ListFormatter> {
            
            val returnVal = lib.icu4x_ListFormatter_create_unit_with_length_mv1(locale.handle, length.toNative());
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ListFormatter(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `ListFormatter` instance for And patterns
        *
        *See the [Rust documentation for `try_new_unit`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.try_new_unit) for more information.
        */
        fun createUnitWithLengthAndProvider(provider: DataProvider, locale: Locale, length: ListLength): Result<ListFormatter> {
            
            val returnVal = lib.icu4x_ListFormatter_create_unit_with_length_and_provider_mv1(provider.handle, locale.handle, length.toNative());
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ListFormatter(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
    }
    
    /** See the [Rust documentation for `format`](https://docs.rs/icu/2.2.0/icu/list/struct.ListFormatter.html#method.format) for more information.
    */
    fun format(list: Array<String>): String {
        val listSliceMemory = PrimitiveArrayTools.borrowUtf16s(list)
        val write = DW.lib.diplomat_buffer_write_create(0)
        val returnVal = lib.icu4x_ListFormatter_format_utf16_mv1(handle, listSliceMemory.slice, write);
        try {
            
            val returnString = DW.writeToString(write)
            return returnString
        } finally {
            listSliceMemory.close()
        }
    }

}