package rasel.lunar.launcher.utils

import android.annotation.SuppressLint

object JamoUtils {
    val CHOSUNG = listOf(
        "ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ",
        "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ",
    )
    val JUNGSUNG = listOf(
        "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅘ",
        "ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", "ㅣ",
    )
    val JONGSUNG = listOf(
        "", "ㄱ", "ㄲ", "ᆪ", "ᆫ", "ᆬ", "ᆭ", "ㄷ",
        "ㄹ", "ᆰ", "ᆱ", "ᆲ", "ᆳ", "ᆴ", "ᆵ", "ᆶ", "ㅁ", "ㅂ", "ᆹ", "ᆺ", "ᆻ", "ᆼ",
        "ᆽ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ",
    )

    fun split(target: String?): List<String> {
        if (target.isNullOrEmpty()) return arrayListOf()
        return target.split("")
            .filter(String::isNotEmpty)
            .map(JamoUtils::splitOne)
            .toList()
    }

    @SuppressLint("SuspiciousIndentation")
    fun splitOne(target: String): String {
        val codePoint = Character.codePointAt(target, 0)
        return if (codePoint in 0xAC00..0xD79D) {
            val startValue = codePoint - 0xAC00
            val jong = startValue % 28
            val jung = (startValue - jong) / 28 % 21
            val cho = ((startValue - jong) / 28 - jung) / 21
            CHOSUNG[cho]
        } else {
            ""
        }
    }
}