package com.example.myapplication.keyboard

object FlatPageBuilder {

    fun buildFlatPages(
        categories: List<SubCategory>,
        pageSize: Int
    ): List<FlatPage> {
        val out = ArrayList<FlatPage>()
        categories.forEachIndexed { catIdx, cat ->
            val chunks = if (cat.items.isEmpty()) emptyList() else cat.items.chunked(pageSize)
            val total = chunks.size.coerceAtLeast(1)

            if (chunks.isEmpty()) {
                // 没数据也给一个空页，避免 pager 0 页
                out.add(
                    FlatPage(
                        catIndex = catIdx,
                        catLabel = cat.label,
                        pageInCat = 0,
                        catPageCount = 1,
                        items = emptyList()
                    )
                )
            } else {
                chunks.forEachIndexed { pageIdx, items ->
                    out.add(
                        FlatPage(
                            catIndex = catIdx,
                            catLabel = cat.label,
                            pageInCat = pageIdx,
                            catPageCount = total,
                            items = items
                        )
                    )
                }
            }
        }
        return out
    }
}
