// LRC parser: takes raw text → [{ time: seconds, text }] sorted by time.

export function parseLrc(text) {
  if (!text) return []
  const out = []
  for (const raw of text.split(/\r?\n/)) {
    // 一行可能有多个 time tag：[00:12.34][00:30.00]歌词
    const tags = []
    let rest = raw
    const re = /^\[(\d+):(\d+)(?:[.:](\d+))?\]/
    while (true) {
      const m = re.exec(rest)
      if (!m) break
      const min = parseInt(m[1], 10)
      const sec = parseInt(m[2], 10)
      const fracRaw = m[3] || '0'
      const frac = parseInt(fracRaw, 10) / Math.pow(10, fracRaw.length)
      tags.push(min * 60 + sec + frac)
      rest = rest.slice(m[0].length)
    }
    const txt = rest.trim()
    if (!txt || tags.length === 0) continue
    for (const t of tags) out.push({ time: t, text: txt })
  }
  out.sort((a, b) => a.time - b.time)
  return out
}
