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 ScriptWithExtensionsLib: Library {
    fun icu4x_ScriptWithExtensions_destroy_mv1(handle: Pointer)
    fun icu4x_ScriptWithExtensions_create_mv1(): Pointer
    fun icu4x_ScriptWithExtensions_create_with_provider_mv1(provider: Pointer): ResultPointerInt
    fun icu4x_ScriptWithExtensions_get_script_val_mv2(handle: Pointer, ch: Int): Int
    fun icu4x_ScriptWithExtensions_has_script_mv2(handle: Pointer, ch: Int, script: Int): Byte
    fun icu4x_ScriptWithExtensions_as_borrowed_mv1(handle: Pointer): Pointer
    fun icu4x_ScriptWithExtensions_iter_ranges_for_script_mv2(handle: Pointer, script: Int): Pointer
}
/** An ICU4X `ScriptWithExtensions` map object, capable of holding a map of codepoints to scriptextensions values
*
*See the [Rust documentation for `ScriptWithExtensions`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensions.html) for more information.
*/
class ScriptWithExtensions 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 ScriptWithExtensionsCleaner(val handle: Pointer, val lib: ScriptWithExtensionsLib) : Runnable {
        override fun run() {
            lib.icu4x_ScriptWithExtensions_destroy_mv1(handle)
        }
    }
    private fun registerCleaner() {
        CLEANER.register(this, ScriptWithExtensions.ScriptWithExtensionsCleaner(handle, ScriptWithExtensions.lib));
    }

    companion object {
        internal val libClass: Class<ScriptWithExtensionsLib> = ScriptWithExtensionsLib::class.java
        internal val lib: ScriptWithExtensionsLib = Native.load("icu4x", libClass)
        @JvmStatic
        
        /** Create a map for the `Script`/`Script_Extensions` properties, using compiled data.
        *
        *See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensions.html#method.new) for more information.
        */
        fun create(): ScriptWithExtensions {
            
            val returnVal = lib.icu4x_ScriptWithExtensions_create_mv1();
            val selfEdges: List<Any> = listOf()
            val handle = returnVal 
            val returnOpaque = ScriptWithExtensions(handle, selfEdges, true)
            return returnOpaque
        }
        @JvmStatic
        
        /** Create a map for the `Script`/`Script_Extensions` properties, using compiled data.
        *
        *See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensions.html#method.new) for more information.
        */
        fun createWithProvider(provider: DataProvider): Result<ScriptWithExtensions> {
            
            val returnVal = lib.icu4x_ScriptWithExtensions_create_with_provider_mv1(provider.handle);
            val nativeOkVal = returnVal.getNativeOk();
            if (nativeOkVal != null) {
                val selfEdges: List<Any> = listOf()
                val handle = nativeOkVal 
                val returnOpaque = ScriptWithExtensions(handle, selfEdges, true)
                return returnOpaque.ok()
            } else {
                return DataErrorError(DataError.fromNative(returnVal.getNativeErr()!!)).err()
            }
        }
    }
    
    /** Get the Script property value for a code point
    *
    *See the [Rust documentation for `get_script_val`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensionsBorrowed.html#method.get_script_val) for more information.
    */
    fun getScriptVal(ch: Int): Script {
        
        val returnVal = lib.icu4x_ScriptWithExtensions_get_script_val_mv2(handle, ch);
        return (Script.fromNative(returnVal))
    }
    
    /** Check if the `Script_Extensions` property of the given code point covers the given script
    *
    *See the [Rust documentation for `has_script`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensionsBorrowed.html#method.has_script) for more information.
    */
    fun hasScript(ch: Int, script: Script): Boolean {
        
        val returnVal = lib.icu4x_ScriptWithExtensions_has_script_mv2(handle, ch, script.toNative());
        return (returnVal > 0)
    }
    
    /** Borrow this object for a slightly faster variant with more operations
    *
    *See the [Rust documentation for `as_borrowed`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensions.html#method.as_borrowed) for more information.
    */
    fun asBorrowed(): ScriptWithExtensionsBorrowed {
        // This lifetime edge depends on lifetimes: 'a
        val aEdges: MutableList<Any> = mutableListOf(this);
        
        val returnVal = lib.icu4x_ScriptWithExtensions_as_borrowed_mv1(handle);
        val selfEdges: List<Any> = listOf()
        val handle = returnVal 
        val returnOpaque = ScriptWithExtensionsBorrowed(handle, selfEdges, aEdges, true)
        return returnOpaque
    }
    
    /** Get a list of ranges of code points that contain this script in their `Script_Extensions` values
    *
    *See the [Rust documentation for `get_script_extensions_ranges`](https://docs.rs/icu/2.2.0/icu/properties/script/struct.ScriptWithExtensionsBorrowed.html#method.get_script_extensions_ranges) for more information.
    */
    fun iterRangesForScript(script: Script): CodePointRangeIterator {
        // This lifetime edge depends on lifetimes: 'a
        val aEdges: MutableList<Any> = mutableListOf(this);
        
        val returnVal = lib.icu4x_ScriptWithExtensions_iter_ranges_for_script_mv2(handle, script.toNative());
        val selfEdges: List<Any> = listOf()
        val handle = returnVal 
        val returnOpaque = CodePointRangeIterator(handle, selfEdges, aEdges, true)
        return returnOpaque
    }

}