package com.marvinelsen.cedict.api

data class PinyinSyllable(
    val syllable: String,
    val tone: Tone,
) {
    companion object {
        fun fromString(pinyinWithNumbers: String): PinyinSyllable {
            require(pinyinWithNumbers.isNotBlank()) { "Argument pinyinWithNumbers must not be blank." }

            val lastCharacter = pinyinWithNumbers.last()
            return if (lastCharacter.isDigit()) {
                PinyinSyllable(
                    syllable = pinyinWithNumbers.substring(0, pinyinWithNumbers.lastIndex),
                    tone = Tone.fromDigit(lastCharacter)
                )
            } else {
                PinyinSyllable(
                    syllable = pinyinWithNumbers,
                    tone = Tone.NONE
                )
            }
        }
    }

    override fun toString() = syllable + tone.toString()
}
