// =============================================================
// PC 端整合包：核心组件同步注册 + 重型组件异步全局注册（chunk 级按需加载）
// 业务代码无需改动，模板可直接使用 <RichtextEditor /> <PdfTools /> 等全局组件
// 首次渲染时才下载对应 chunk
// =============================================================

import $lang from './lang/index'
import app from '../../common/app/index'
import auth from '../../common/auth/index'
import eventBus from '../../common/eventBus/index'
import user from '../../common/user/index'

import $hequest from '../../common/request/index'
import $store from '../../common/store/index'
import $webSocket from '../../common/websocket/index'

// ---------- 核心组件（首屏必需，install 时同步注册） ----------
import { Basicform, DynamicForm, FormItem } from '../../ecwapp/Basicform/index'
import { DataTable, Toolbar, RenderAttachColumn } from '../../ecwapp/DataTable/index'
import DatetimeRange from '../../ecwapp/DatetimeRange/index'
import DoingDialog from '../../ecwapp/DoingDialog/index'
import InnerTable from '../../ecwapp/InnerTable/index'
import LRLayout from '../../ecwapp/LRLayout/index'
import LeftPanel from '../../ecwapp/LRLayout/src/LeftPanel'
import RightPanel from '../../ecwapp/LRLayout/src/RightPanel'
import PageContent from '../../ecwapp/PageContent/index'
import PageHeader from '../../ecwapp/PageHeader/index'
import ScrollPane from '../../ecwapp/ScrollPane/index'
import SelectDialog from '../../ecwapp/SelectDialog/index'
import SelectGroup from '../../ecwapp/SelectGroup/index'
import SidebarLogo from '../../ecwapp/SidebarLogo/index'
import SvgIcon from '../../ecwapp/SvgIcon/index'
import WfNames from '../../ecwapp/WfNames/index'

import '../../common/directive/index'
import '../../ecwapp/Directive/index'

const components = [app, auth, eventBus, user, Basicform, DynamicForm, FormItem, DataTable, Toolbar, RenderAttachColumn, DatetimeRange, DoingDialog, InnerTable, LRLayout, LeftPanel, RightPanel, PageHeader, PageContent, ScrollPane, SelectDialog, SelectGroup, SidebarLogo, SvgIcon, WfNames]

// ---------- 重型组件异步全局注册清单（chunk 级按需加载） ----------
// 格式：[全局组件名, 异步工厂函数]
// 通过 webpackChunkName magic-comment 控制分包；同 chunkName 会合并到同一个 chunk
const asyncComponents = [
    // ========== ecwapp 业务功能 ==========
    ['BatchProcessing', () => import(/* webpackChunkName: "func-batch" */ '../../ecwapp/BatchProcessing/index').then((m) => m.BatchProcessing)],
    ['TaskItemTypeTree', () => import(/* webpackChunkName: "func-batch" */ '../../ecwapp/BatchProcessing/index').then((m) => m.TaskItemTypeTree)],
    ['ModifyFormTrace', () => import(/* webpackChunkName: "func-formtrace" */ '../../ecwapp/ModifyFormTrace/index')],
    ['PdfTools', () => import(/* webpackChunkName: "func-pdf" */ '../../ecwapp/PdfTools')],
    ['RichtextEditor', () => import(/* webpackChunkName: "func-richtext" */ '../../ecwapp/RichtextEditor')],
    ['PDFInjector', () => import(/* webpackChunkName: "func-textrecog" */ '../../ecwapp/TextRecognition/index').then((m) => m.PDFInjector)],
    ['TextRecognition', () => import(/* webpackChunkName: "func-textrecog" */ '../../ecwapp/TextRecognition/index').then((m) => m.TextRecognition)],

    // ========== ecwapp/Wflowform 工作流表单子组件 → chunk: wflow-form ==========
    ['Attachment', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.Attachment)],
    ['BodyFile', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.BodyFile)],
    ['CommandBar', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.CommandBar)],
    ['HandleIdea', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.HandleIdea)],
    ['IdeaInputBox', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.IdeaInputBox)],
    ['ModifyWflogAddUser', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ModifyWflogAddUser)],
    ['ModifyWflogIdea', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ModifyWflogIdea)],
    ['WriteToReadIdea', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.WriteToReadIdea)],
    ['ModifyWflogSubmitFile', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ModifyWflogSubmitFile)],
    ['ModifyWfNodelog', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ModifyWfNodelog)],
    ['SelectOuterDeputy', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.SelectOuterDeputy)],
    ['SubmitHandle', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.SubmitHandle)],
    ['SubmitDialog', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.SubmitDialog)],
    ['WfNodelogList', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.WfNodelogList)],
    ['WflogAddReader', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.WflogAddReader)],
    ['WflowView', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.WflowView)],
    ['ImgView', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ImgView)],
    ['IdeaSignature', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.IdeaSignature)],
    ['ReplaceFile', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ReplaceFile)],
    ['ViewBodyFile', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.ViewBodyFile)],
    ['WformLayout', () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform').then((m) => m.WformLayout)],

    // ========== ecwplat 平台管理 → chunk: plat-* ==========
    ['Product', () => import(/* webpackChunkName: "plat-product" */ '../../ecwplat/Product').then((m) => m.Product)],
    ['ProductRoleMgr', () => import(/* webpackChunkName: "plat-product" */ '../../ecwplat/Product').then((m) => m.ProductRoleMgr)],
    ['ResRoles', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.ResRoles)],
    ['RoleInfoForm', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.RoleInfoForm)],
    ['RolePermissionForm', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.RolePermissionForm)],
    ['PermissionMatrixMgr', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.PermissionMatrixMgr)],
    ['RoleMgr', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.RoleMgr)],
    ['RolePermissionSet', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.RolePermissionSet)],
    ['UserPermissionSet', () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index').then((m) => m.UserPermissionSet)],
    ['ModuleIndex', () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index').then((m) => m.ModuleIndex)],
    ['CopyMenu', () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index').then((m) => m.CopyMenu)],
    ['ModuleMenu', () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index').then((m) => m.ModuleMenu)],
    ['ModuleLRLayout', () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index').then((m) => m.ModuleLRLayout)],
    ['SysSetup', () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index').then((m) => m.SysSetup)],
    ['FrameLoader', () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index').then((m) => m.FrameLoader)],
    ['ImportPersonOrg', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.ImportPersonOrg)],
    ['ImportWorkgroupMember', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.ImportWorkgroupMember)],
    ['OrgPersonMgr', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.OrgPersonMgr)],
    ['OrgPersonForm', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.OrgPersonForm)],
    ['OrgPersonOther', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.OrgPersonOther)],
    ['OrgTreeMgr', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.OrgTreeMgr)],
    ['JobMatrixMgr', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.JobMatrixMgr)],
    ['GroupMatrixMgr', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.GroupMatrixMgr)],
    ['OrgUserList', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.OrgUserList)],
    ['OrgTreeLRLayout', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.OrgTreeLRLayout)],
    ['ColumnEdit', () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index').then((m) => m.ColumnEdit)],
    ['WfdList', () => import(/* webpackChunkName: "plat-wfdefine" */ '../../ecwplat/WfDefine').then((m) => m.WfdList)],
    ['EditorConfig', () => import(/* webpackChunkName: "plat-editor" */ '../../ecwplat/EditorConfig').then((m) => m.EditorConfig)],

    // ========== ecwplus 设计器系列 → chunk: devtool-* ==========
    ['BusiformDesigner', () => import(/* webpackChunkName: "devtool-busiform" */ '../../ecwplus/BusiformDesigner').then((m) => m.BusiformDesigner)],
    ['FromEventEdit', () => import(/* webpackChunkName: "devtool-busiform" */ '../../ecwplus/BusiformDesigner').then((m) => m.FromEventEdit)],
    ['ListPageDesigner', () => import(/* webpackChunkName: "devtool-listpage" */ '../../ecwplus/ListPageDesigner/index')],
    ['DbSearchPanel', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.DbSearchPanel)],
    ['TableEntityBatchAdd', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.TableEntityBatchAdd)],
    ['TableEntityForm', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.TableEntityForm)],
    ['TableFieldForm', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.TableFieldForm)],
    ['TableFieldList', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.TableFieldList)],
    ['ImportModuleCode', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.ImportModuleCode)],
    ['ExportModuleCode', () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner').then((m) => m.ExportModuleCode)],
    ['PortalPageDesigner', () => import(/* webpackChunkName: "devtool-portal" */ '../../ecwplus/PortalPageDesigner').then((m) => m.PortalPageDesigner)],
    ['PageEvent', () => import(/* webpackChunkName: "devtool-portal" */ '../../ecwplus/PortalPageDesigner').then((m) => m.PageEvent)],
    ['ModuleStyle', () => import(/* webpackChunkName: "devtool-portal" */ '../../ecwplus/PortalPageDesigner').then((m) => m.ModuleStyle)],
    ['UdmGenerator', () => import(/* webpackChunkName: "devtool-udm" */ '../../ecwplus/UdmGenerator').then((m) => m.UdmGenerator)],
    ['UserModuleForm', () => import(/* webpackChunkName: "devtool-udm" */ '../../ecwplus/UdmGenerator').then((m) => m.UserModuleForm)],
    ['BusiformList', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.BusiformList)],
    ['BusiformMgr', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.BusiformMgr)],
    ['ListpageList', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.ListpageList)],
    ['ListpageMgr', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.ListpageMgr)],
    ['CodeEditor', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.CodeEditor)],
    ['SyncRestfulApi', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.SyncRestfulApi)],
    ['TableEntityList', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.TableEntityList)],
    ['TableEntityMgr', () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole').then((m) => m.TableEntityMgr)],

    // ========== 工作流设计器 + 系统设计器 ==========
    ['WflowDesigner', () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/index')],
    ['ImportWf', () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/src/components/ImportWf')],
    ['WflowTrace', () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/src/components/FlowTrace')],
    ['WflowTester', () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/src/components/Tester')],
    ['SystemDesigner', () => import(/* webpackChunkName: "devtool-system" */ '../../ecwplus/SystemDesigner/index').then((m) => m.SystemDesigner)]
]

// 定义 install 方法
const install = function (Vue) {
    if (install.installed) return
    install.installed = true

    // 1. 同步注册核心组件
    components.map((component) => {
        Vue.use(component)
    })

    // 2. 异步全局注册重型组件（首次渲染时才下载 chunk）
    asyncComponents.forEach(([name, factory]) => {
        Vue.component(name, factory)
    })

    // 3. 异步加载工作流引擎插件（独立 chunk）
    import(/* webpackChunkName: "wflow-engine" */ '../../ecwflow/wfengine/index').then((m) => {
        Vue.use(m.default || m)
    })
}

if (typeof window !== 'undefined' && window.Vue) {
    setTimeout(() => {
        window.$alert = (message, title, options) => {
            const msgbox = document.querySelector('.el-message-box')
            if (!msgbox) {
                if (typeof title === 'string') {
                    window.Vue.prototype.$alert(message, title, options)
                } else {
                    window.Vue.prototype.$alert(message, options)
                }
            }
        }

        window.Vue.prototype.$tx = $lang.$tx
        window.$tx = $lang.$tx
        window.$http = $hequest
    }, 0)
}

export const lang = $lang
export const httpRequest = $hequest
export const store = $store
export const webSocket = $webSocket

export default {
    install,
    ...components
}

// =============================================================
// 兼容性 Module 工厂函数 export（路由/动态场景主动调用）
// 使用示例：
//   const { WflowDesignerModule } = require('ecinc-cloud-wappaio')
//   components: { WflowDesigner: () => WflowDesignerModule().then(m => m.default) }
// =============================================================

// ---------- 工作流相关 ----------
export const WfengineModule = () => import(/* webpackChunkName: "wflow-engine" */ '../../ecwflow/wfengine/index')
export const WflowformModule = () => import(/* webpackChunkName: "wflow-form" */ '../../ecwapp/Wflowform')
export const WflowDesignerModule = () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/index')
export const WflowTraceModule = () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/src/components/FlowTrace')
export const ImportWfModule = () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/src/components/ImportWf')
export const WflowTesterModule = () => import(/* webpackChunkName: "wflow-designer" */ '../../ecwplus/WorkflowDesigner/src/components/Tester')

// ---------- 平台管理 ----------
export const ProductModule = () => import(/* webpackChunkName: "plat-product" */ '../../ecwplat/Product')
export const ResRolesModule = () => import(/* webpackChunkName: "plat-roles" */ '../../ecwplat/ResRoles/index')
export const ModuleIndexModule = () => import(/* webpackChunkName: "plat-module" */ '../../ecwplat/Module/index')
export const OrganModule = () => import(/* webpackChunkName: "plat-organ" */ '../../ecwplat/Organ/index')
export const WfdListModule = () => import(/* webpackChunkName: "plat-wfdefine" */ '../../ecwplat/WfDefine')
export const EditorConfigModule = () => import(/* webpackChunkName: "plat-editor" */ '../../ecwplat/EditorConfig')

// ---------- 开发者工具（设计器系列） ----------
export const BusiformDesignerModule = () => import(/* webpackChunkName: "devtool-busiform" */ '../../ecwplus/BusiformDesigner')
export const ListPageDesignerModule = () => import(/* webpackChunkName: "devtool-listpage" */ '../../ecwplus/ListPageDesigner/index')
export const TableEntityDesignerModule = () => import(/* webpackChunkName: "devtool-tableentity" */ '../../ecwplus/TableEntityDesigner')
export const PortalPageDesignerModule = () => import(/* webpackChunkName: "devtool-portal" */ '../../ecwplus/PortalPageDesigner')
export const UdmGeneratorModule = () => import(/* webpackChunkName: "devtool-udm" */ '../../ecwplus/UdmGenerator')
export const AdminConsoleModule = () => import(/* webpackChunkName: "devtool-admin" */ '../../ecwplus/AdminConsole')
export const SystemDesignerModule = () => import(/* webpackChunkName: "devtool-system" */ '../../ecwplus/SystemDesigner/index')

// ---------- 其他重型功能 ----------
export const PdfToolsModule = () => import(/* webpackChunkName: "func-pdf" */ '../../ecwapp/PdfTools')
export const RichtextEditorModule = () => import(/* webpackChunkName: "func-richtext" */ '../../ecwapp/RichtextEditor')
export const ModifyFormTraceModule = () => import(/* webpackChunkName: "func-formtrace" */ '../../ecwapp/ModifyFormTrace/index')
export const BatchProcessingModule = () => import(/* webpackChunkName: "func-batch" */ '../../ecwapp/BatchProcessing/index')
export const TextRecognitionModule = () => import(/* webpackChunkName: "func-textrecog" */ '../../ecwapp/TextRecognition/index')
