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 TitlecaseMapperLib: Library {
    fun icu4x_TitlecaseMapper_destroy_mv1(handle: Pointer)
    fun icu4x_TitlecaseMapper_create_mv1(): ResultPointerInt
    fun icu4x_TitlecaseMapper_create_with_provider_mv1(provider: Pointer): ResultPointerInt
    fun icu4x_TitlecaseMapper_titlecase_segment_v1_mv1(handle: Pointer, s: Slice, locale: Pointer, options: TitlecaseOptionsNative, write: Pointer): Unit
    fun icu4x_TitlecaseMapper_titlecase_segment_with_compiled_data_v1_mv1(s: Slice, locale: Pointer, options: TitlecaseOptionsNative, write: Pointer): Unit
}
/** See the [Rust documentation for `TitlecaseMapper`](https://docs.rs/icu/2.2.0/icu/casemap/struct.TitlecaseMapper.html) for more information.
*/
class TitlecaseMapper 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 TitlecaseMapperCleaner(val handle: Pointer, val lib: TitlecaseMapperLib) : Runnable {
        override fun run() {
            lib.icu4x_TitlecaseMapper_destroy_mv1(handle)
        }
    }
    private fun registerCleaner() {
        CLEANER.register(this, TitlecaseMapper.TitlecaseMapperCleaner(handle, TitlecaseMapper.lib));
    }

    companion object {
        internal val libClass: Class<TitlecaseMapperLib> = TitlecaseMapperLib::class.java
        internal val lib: TitlecaseMapperLib = Native.load("icu4x", libClass)
        @JvmStatic
        
        /** Construct a new `TitlecaseMapper` instance using compiled data.
        *
        *See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/casemap/struct.TitlecaseMapper.html#method.new) for more information.
        */
        fun create(): Result<TitlecaseMapper> {
            
            val returnVal = lib.icu4x_TitlecaseMapper_create_mv1();
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = TitlecaseMapper(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `TitlecaseMapper` instance using a particular data source.
        *
        *See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/casemap/struct.TitlecaseMapper.html#method.new) for more information.
        */
        fun createWithProvider(provider: DataProvider): Result<TitlecaseMapper> {
            
            val returnVal = lib.icu4x_TitlecaseMapper_create_with_provider_mv1(provider.handle);
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = TitlecaseMapper(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Returns the full titlecase mapping of the given string, using compiled data (avoids having to allocate a `TitlecaseMapper` object)
        *
        *The `v1` refers to the version of the options struct, which may change as we add more options
        *
        *See the [Rust documentation for `titlecase_segment`](https://docs.rs/icu/2.2.0/icu/casemap/struct.TitlecaseMapperBorrowed.html#method.titlecase_segment) for more information.
        */
        fun titlecase_segment_with_compiled_data(s: String, locale: Locale, options: TitlecaseOptions): String {
            val sSliceMemory = PrimitiveArrayTools.borrowUtf8(s)
            val write = DW.lib.diplomat_buffer_write_create(0)
            val returnVal = lib.icu4x_TitlecaseMapper_titlecase_segment_with_compiled_data_v1_mv1(sSliceMemory.slice, locale.handle, options.toNative(), write);
            try {
                
                val returnString = DW.writeToString(write)
                return returnString
            } finally {
                sSliceMemory.close()
            }
        }
    }
    
    /** Returns the full titlecase mapping of the given string
    *
    *The `v1` refers to the version of the options struct, which may change as we add more options
    *
    *See the [Rust documentation for `titlecase_segment`](https://docs.rs/icu/2.2.0/icu/casemap/struct.TitlecaseMapperBorrowed.html#method.titlecase_segment) for more information.
    */
    fun titlecase_segment(s: String, locale: Locale, options: TitlecaseOptions): String {
        val sSliceMemory = PrimitiveArrayTools.borrowUtf8(s)
        val write = DW.lib.diplomat_buffer_write_create(0)
        val returnVal = lib.icu4x_TitlecaseMapper_titlecase_segment_v1_mv1(handle, sSliceMemory.slice, locale.handle, options.toNative(), write);
        try {
            
            val returnString = DW.writeToString(write)
            return returnString
        } finally {
            sSliceMemory.close()
        }
    }

}