import { readFileSync, readdirSync } from 'fs'
import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

/**
 * 获取所有 SVG 图标
 * @param dir 图标目录
 */
export function getSvgIcons(dir) {
  try {
    const icons = []
    const files = readdirSync(dir)
    files.forEach((file) => {
      if (file.endsWith('.svg')) {
        const content = readFileSync(resolve(dir, file), 'utf-8')
        icons.push({
          name: file.replace('.svg', ''),
          content
        })
      }
    })
    return icons
  } catch (error) {
    console.error('Error reading SVG files:', error)
    return []
  }
}

/**
 * Vite SVG 插件配置
 * @param {string} iconDirs 图标目录
 */
export function configSvgIconsPlugin(iconDirs) {
  return createSvgIconsPlugin({
    iconDirs: [iconDirs],
    symbolId: 'icon-[dir]-[name]',
    inject: 'body-last',
    customDomId: '__svg__icons__dom__'
  })
} 