import fs from 'fs'

interface IconfontJson {
  font_family: string;
  css_prefix_text: string;
  glyphs: {
    icon_id: string
    font_class: string
    unicode: string
    name: string
  }[];
}

export default function iconfontProcess(outPath: string) {
  return function (filePath: string, content?: string, eventType?: 'add' | 'change' | 'unlink') {
    console.log(filePath, content, eventType)
    const json = JSON.parse(content!) as IconfontJson
    const names = json.glyphs.map(glyph => glyph.font_class)
    const dtsFile = outPath + '/iconfont.d.ts'
    console.log('正在生成文件：', dtsFile)
    const dts = `export {}

declare global {
  namespace IconfontTypes {
    type name = ${names.map(name => `'${name}'`).join('\n        | ')}
  }
}
`
    fs.writeFileSync(dtsFile, dts, {encoding: 'utf-8'})

    const tsFile = outPath + '/icons.ts'
    const ts = `export default reactive([${'\n  ' + names.map(name => `{name: '${name}'}`).join(',\n  ') + '\n'}])`
    console.log('正在生成文件：', tsFile)
    fs.writeFileSync(tsFile, ts, {encoding: 'utf-8'})

    console.log('文件生成完成')
  }
}
