/* eslint-disable no-console */
import path from 'node:path'
import fs from 'fs-extra'

const FILE_PATH = path.join(__dirname, '../asset/词语听写.json')

async function cleanCiyuJson() {
  try {
    if (!fs.existsSync(FILE_PATH)) {
      console.error(`File not found: ${FILE_PATH}`)
      return
    }

    const data = await fs.readJSON(FILE_PATH)

    if (data.question_bank && Array.isArray(data.question_bank)) {
      let modifiedCount = 0
      data.question_bank.forEach((q: any) => {
        if (q.Name && typeof q.Name === 'string') {
          // Remove HTML tags using regex
          const cleanName = q.Name.replace(/<[^>]*>/g, '').trim()
          if (cleanName !== q.Name) {
            console.log(`Cleaning: ${q.Name} -> ${cleanName}`)
            q.Name = cleanName
            modifiedCount++
          }
        }
      })

      if (modifiedCount > 0) {
        await fs.writeJSON(FILE_PATH, data, { spaces: 2 })
        console.log(`Successfully cleaned ${modifiedCount} items in 词语听写.json`)
      }
      else {
        console.log('No changes needed for 词语听写.json')
      }
    }
  }
  catch (error) {
    console.error('Error processing file:', error)
  }
}

cleanCiyuJson()
