import { getAyahMeta } from "./getAyahMeta"
import type { RiwayaData } from "./lists/types"
import type { AyahMeta, JuzPart, Surah } from "./types"
import { checkValidSurah } from "./validation"

/**
 * Retrieves metadata for all ayahs in a specific surah.
 *
 * @param surahNumber - The surah number (1-114)
 * @param data - The Lists object for the riwaya.
 * @returns Array of AyahMeta objects for each ayah in the surah
 * @throws RangeError If the surah number is not between 1 and 114
 */
export function getAyahMetasForSurah(surahNumber: Surah, data: RiwayaData): AyahMeta[] {
  checkValidSurah(surahNumber, data.meta)
  const { SurahList, SajdaList, PageList, RukuList, JuzList, HizbQuarterList } = data

  const [
    startAyahId,
    ayahCount // , surahOrder, rukuCount, name, isMeccan, page
  ] = SurahList[surahNumber]
  const endAyahId = startAyahId + ayahCount - 1
  const result: AyahMeta[] = []

  // Const rubAlHizbMeta = getRubAlHizbMetaByAyahId(startAyahId as AyahId)
  let meta = getAyahMeta(startAyahId, data)
  result.push(meta)
  for (let ayahId = startAyahId + 1; ayahId <= endAyahId; ayahId++) {
    // Most properties will be the same as previous ayah except for specific positions
    meta = structuredClone(meta)
    meta.ayah += 1
    // Console.log(ayahId, meta.ayah, meta.page, PageList[meta.page + 1])
    meta.isStartOfSurah = false
    meta.isEndOfSurah = endAyahId === ayahId

    if (PageList[meta.page + 1] === ayahId) {
      meta.page += 1
      meta.isStartOfPage = true
    } else {
      meta.isStartOfPage = false
    }
    meta.isEndOfPage = PageList[meta.page + 1] === ayahId + 1

    if (RukuList[meta.ruku + 1] === ayahId) {
      meta.ruku += 1
      meta.isStartOfRuku = true
    } else {
      meta.isStartOfRuku = false
    }
    meta.isEndOfRuku = RukuList[meta.ruku + 1] === ayahId + 1

    meta.isEndOfJuz = JuzList[meta.juz + 1] === ayahId + 1
    if (JuzList[meta.juz + 1] === ayahId) {
      meta.juz += 1
      meta.hizbId += 1
      meta.isStartOfJuz = true
    } else {
      meta.isStartOfJuz = false
    }
    meta.isEndOfQuarter = HizbQuarterList[meta.rubAlHizbId + 1] === ayahId + 1
    if (HizbQuarterList[meta.rubAlHizbId + 1] === ayahId) {
      meta.rubAlHizbId += 1
      meta.juzPart = meta.isStartOfJuz ? 1 : ((meta.juzPart + 1) as JuzPart)
      meta.isStartOfQuarter = true
      if (meta.juzPart === 5) {
        meta.hizbId += 1
      }
    } else {
      meta.isStartOfQuarter = false
    }

    meta.isSajdahAyah = SajdaList.includes(ayahId)

    result.push(meta)
  }

  return result
}
