import type { FieldConfigPlugin, FieldConfigContext } from '@/core/plugins/fieldPlugin'

/**
 * Module Def 字段配置插件
 * 为 app_name 字段动态加载已安装应用列表
 */
export default {
  pagetype: 'Module Def',
  fieldname: 'app_name',
  config: async ({ setOptions }: FieldConfigContext) => {
    try {
      const response = await fetch(
        '/api/action/jingrow.core.pagetype.module_def.module_def.get_installed_apps'
      )
      const data = await response.json()
      const apps = data.message || []
      setOptions(Array.isArray(apps) ? apps : JSON.parse(apps))
    } catch (error) {
      console.error('[ModuleDef] Failed to load installed apps:', error)
    }
  },
  defaultValue: 'jingrow'
} as FieldConfigPlugin
