import path from 'node:path'
import type { Plugin } from 'vite'

// https://github.com/antfu/unplugin-auto-import
import AutoImport from 'unplugin-auto-import/vite'

// https://github.com/antfu/unplugin-vue-components
import Components from 'unplugin-vue-components/vite'

/**
 * unplugin-auto-import
 * @returns Plugin
 */
export function configAutoImportPlugin(): Plugin {
    return AutoImport({
        dts: 'types/auto-imports.d.ts',
        imports: [
            'vue',
            'vue-i18n',
            'uni-app',
            {
                '@/config/app': ['$app'],
                '@/api/services': ['defs'],
                '@/api/services/mods': ['API'],
                '@/common': ['Message'],
                '@/utils/stomp': [['default', 'Stomp']],
            },
        ],
    })
}

/**
 * unplugin-vue-components
 * @returns Plugin
 */
export function configComponentsImportPlugin(): Plugin {
    return Components({
        dts: 'types/components.d.ts',
        resolvers: [
            (name: string) => {
                // FIX: 修复 easycom 在 APP 下不生效问题（原因不明
                if (name.startsWith('fui-')) {
                    return path.resolve(__dirname, `src/components/firstui/${name}/${name}.vue`).replaceAll('\\', '/')
                }
                // if (name.startsWith('uni-')) {
                //     return `@dcloudio/uni-ui/lib/${name}/${name}.vue`
                // }
            },
        ],
    })
}

export function configImportPlugin(): Plugin[] {
    // FIX: easycom 适配问题
    return [configAutoImportPlugin(), configComponentsImportPlugin()]
    // return [configAutoImportPlugin()]
}
