import {readFile} from "fs";
import path from "path";
interface Word{
    bsm:string
    hz:string
    pyJs:string[]
    id:number
}
interface Ret{
    data:Word[]
    message:string
    haveMess:boolean
}
let words:Word[]
readFile("./word.json",'utf-8',(err, data)=>{
    if (err)console.error(err)
    words = JSON.parse(data)
    for (let i =0 ;i<words.length;i++){
        words[i].id=i
    }
})
export default defineEventHandler((event):Ret=>{
    const retWords:Word[] = []
    const query = getQuery(event)
    if (query.words=="错误"||query.method=="错误"||!(query.method=="bsmf"||query.method=="bsmc"||query.method=="hz")||query.words.length>20){
        return {
            data:[],
            message:"参数错误",
            haveMess:true
        }
    }
    const word:string = query.words as unknown as string
    if (word==""){
        return {
            data:words,
            message:"",
            haveMess:false
        }
    }
    if (query.method=="hz"){
        for (let i =0;i<word.length;i++){
            for (let j=0;j<words.length;j++){
                if (words[j].hz.includes(word[i])){
                    let newWords:Word = words[j]
                    retWords.push(newWords)
                }
            }
        }
    }else if (query.method=="bsmf"){
        words.forEach((wf,index)=>{
            if (wf.bsm.startsWith(word)) retWords.push(wf)
        })
    }else if(query.method=="bsmc"){
        words.forEach((wf,index)=>{
            if (wf.bsm.includes(word)) retWords.push(wf)
        })
    }
    return {
        data:retWords,
        message:"",
        haveMess:false
    }
})