{"version":3,"file":"pluginSystem.cjs","names":["isPlainObject"],"sources":["../../../src/utils/hooks/pluginSystem.ts"],"sourcesContent":["import type { ModuleFederation } from '../../core';\nimport { assert, isPlainObject } from '../../utils';\n\nexport type Plugin<T extends Record<string, any>> = {\n  [k in keyof T]?: Parameters<T[k]['on']>[0];\n} & {\n  name: string;\n  version?: string;\n  apply?: (instance: ModuleFederation) => void;\n};\n\nexport class PluginSystem<T extends Record<string, any>> {\n  lifecycle: T;\n  lifecycleKeys: Array<keyof T>;\n  registerPlugins: Record<string, Plugin<T>> = {};\n\n  constructor(lifecycle: T) {\n    this.lifecycle = lifecycle;\n    this.lifecycleKeys = Object.keys(lifecycle);\n  }\n\n  applyPlugin(plugin: Plugin<T>, instance: ModuleFederation): void {\n    assert(isPlainObject(plugin), 'Plugin configuration is invalid.');\n    // The plugin's name is mandatory and must be unique\n    const pluginName = plugin.name;\n    assert(pluginName, 'A name must be provided by the plugin.');\n\n    if (!this.registerPlugins[pluginName]) {\n      this.registerPlugins[pluginName] = plugin;\n      plugin.apply?.(instance);\n\n      Object.keys(this.lifecycle).forEach((key) => {\n        const pluginLife = plugin[key as string];\n        if (pluginLife) {\n          this.lifecycle[key].on(pluginLife);\n        }\n      });\n    }\n  }\n\n  removePlugin(pluginName: string): void {\n    assert(pluginName, 'A name is required.');\n    const plugin = this.registerPlugins[pluginName];\n    assert(plugin, `The plugin \"${pluginName}\" is not registered.`);\n\n    Object.keys(plugin).forEach((key) => {\n      if (key !== 'name') {\n        this.lifecycle[key].remove(plugin[key as string]);\n      }\n    });\n  }\n}\n"],"mappings":";;;;;AAWA,IAAa,eAAb,MAAyD;CAKvD,YAAY,WAAc;yBAFmB,EAAE;AAG7C,OAAK,YAAY;AACjB,OAAK,gBAAgB,OAAO,KAAK,UAAU;;CAG7C,YAAY,QAAmB,UAAkC;AAC/D,wBAAOA,2BAAc,OAAO,EAAE,mCAAmC;EAEjE,MAAM,aAAa,OAAO;AAC1B,wBAAO,YAAY,yCAAyC;AAE5D,MAAI,CAAC,KAAK,gBAAgB,aAAa;AACrC,QAAK,gBAAgB,cAAc;AACnC,UAAO,QAAQ,SAAS;AAExB,UAAO,KAAK,KAAK,UAAU,CAAC,SAAS,QAAQ;IAC3C,MAAM,aAAa,OAAO;AAC1B,QAAI,WACF,MAAK,UAAU,KAAK,GAAG,WAAW;KAEpC;;;CAIN,aAAa,YAA0B;AACrC,wBAAO,YAAY,sBAAsB;EACzC,MAAM,SAAS,KAAK,gBAAgB;AACpC,wBAAO,QAAQ,eAAe,WAAW,sBAAsB;AAE/D,SAAO,KAAK,OAAO,CAAC,SAAS,QAAQ;AACnC,OAAI,QAAQ,OACV,MAAK,UAAU,KAAK,OAAO,OAAO,KAAe;IAEnD"}