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 CaseMapCloserLib: Library {
    fun icu4x_CaseMapCloser_destroy_mv1(handle: Pointer)
    fun icu4x_CaseMapCloser_create_mv1(): ResultPointerInt
    fun icu4x_CaseMapCloser_create_with_provider_mv1(provider: Pointer): ResultPointerInt
    fun icu4x_CaseMapCloser_add_case_closure_to_mv1(handle: Pointer, c: Int, builder: Pointer): Unit
    fun icu4x_CaseMapCloser_add_string_case_closure_to_mv1(handle: Pointer, s: Slice, builder: Pointer): Byte
}
/** See the [Rust documentation for `CaseMapCloser`](https://docs.rs/icu/2.2.0/icu/casemap/struct.CaseMapCloser.html) for more information.
*/
class CaseMapCloser 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 CaseMapCloserCleaner(val handle: Pointer, val lib: CaseMapCloserLib) : Runnable {
        override fun run() {
            lib.icu4x_CaseMapCloser_destroy_mv1(handle)
        }
    }
    private fun registerCleaner() {
        CLEANER.register(this, CaseMapCloser.CaseMapCloserCleaner(handle, CaseMapCloser.lib));
    }

    companion object {
        internal val libClass: Class<CaseMapCloserLib> = CaseMapCloserLib::class.java
        internal val lib: CaseMapCloserLib = Native.load("icu4x", libClass)
        @JvmStatic
        
        /** Construct a new `CaseMapCloser` instance using compiled data.
        *
        *See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/casemap/struct.CaseMapCloser.html#method.new) for more information.
        */
        fun create(): Result<CaseMapCloser> {
            
            val returnVal = lib.icu4x_CaseMapCloser_create_mv1();
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = CaseMapCloser(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
        @JvmStatic
        
        /** Construct a new `CaseMapCloser` instance using a particular data source.
        *
        *See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/casemap/struct.CaseMapCloser.html#method.new) for more information.
        */
        fun createWithProvider(provider: DataProvider): Result<CaseMapCloser> {
            
            val returnVal = lib.icu4x_CaseMapCloser_create_with_provider_mv1(provider.handle);
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = CaseMapCloser(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
    }
    
    /** Adds all simple case mappings and the full case folding for `c` to `builder`.
    *Also adds special case closure mappings.
    *
    *See the [Rust documentation for `add_case_closure_to`](https://docs.rs/icu/2.2.0/icu/casemap/struct.CaseMapCloserBorrowed.html#method.add_case_closure_to) for more information.
    */
    fun addCaseClosureTo(c: Int, builder: CodePointSetBuilder): Unit {
        
        val returnVal = lib.icu4x_CaseMapCloser_add_case_closure_to_mv1(handle, c, builder.handle /* note this is a mutable reference. Think carefully about using, especially concurrently */);
        
    }
    
    /** Finds all characters and strings which may casemap to `s` as their full case folding string
    *and adds them to the set.
    *
    *Returns true if the string was found
    *
    *See the [Rust documentation for `add_string_case_closure_to`](https://docs.rs/icu/2.2.0/icu/casemap/struct.CaseMapCloserBorrowed.html#method.add_string_case_closure_to) for more information.
    */
    fun addStringCaseClosureTo(s: String, builder: CodePointSetBuilder): Boolean {
        val sSliceMemory = PrimitiveArrayTools.borrowUtf8(s)
        
        val returnVal = lib.icu4x_CaseMapCloser_add_string_case_closure_to_mv1(handle, sSliceMemory.slice, builder.handle /* note this is a mutable reference. Think carefully about using, especially concurrently */);
        try {
            return (returnVal > 0)
        } finally {
            sSliceMemory.close()
        }
    }

}