"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var plugin_module_exports = {};
__export(plugin_module_exports, {
  BotPluginModule: () => BotPluginModule
});
module.exports = __toCommonJS(plugin_module_exports);
var consts = __toESM(require("../../consts"));
var import_module = require("../../module");
var import_plugin_typings = require("../../plugin-implementation/plugin-typings");
class BundleJsModule extends import_module.Module {
  _indexJs;
  constructor(plugin) {
    super({
      path: "bundle.js",
      exportName: "default"
    });
    this._indexJs = plugin.implementation.toString();
  }
  async getContent() {
    return this._indexJs;
  }
}
class BundleDtsModule extends import_module.Module {
  constructor(_typingsModule) {
    super({
      path: "bundle.d.ts",
      exportName: "default"
    });
    this._typingsModule = _typingsModule;
  }
  async getContent() {
    const typingsImport = this._typingsModule.import(this);
    return [
      consts.GENERATED_HEADER,
      'import * as sdk from "@botpress/sdk"',
      `import * as ${this._typingsModule.name} from "./${typingsImport}"`,
      `type TPlugin = sdk.DefaultPlugin<${this._typingsModule.name}.${this._typingsModule.exportName}>`,
      "export default new sdk.Plugin<TPlugin>({})",
      ""
    ].join("\n");
  }
}
class PluginConfigModule extends import_module.Module {
  _plugin;
  constructor(config) {
    super(config);
    this._plugin = config.plugin;
  }
  async getContent() {
    const { interfaces, configuration, alias } = this._plugin;
    const content = JSON.stringify({ alias, interfaces, configuration }, null, 2);
    return `export default ${content}`;
  }
}
class BotPluginModule extends import_module.Module {
  _typingsModule;
  _bundleJsModule;
  _bundleDtsModule;
  _configModule;
  pluginKey;
  constructor(plugin) {
    super({
      exportName: "default",
      path: consts.INDEX_FILE
    });
    this.pluginKey = plugin.alias ?? plugin.name;
    this._typingsModule = new import_plugin_typings.PluginTypingsModule(plugin.definition);
    this._typingsModule.unshift("typings");
    this.pushDep(this._typingsModule);
    this._bundleJsModule = new BundleJsModule(plugin);
    this.pushDep(this._bundleJsModule);
    this._bundleDtsModule = new BundleDtsModule(this._typingsModule);
    this.pushDep(this._bundleDtsModule);
    this._configModule = new PluginConfigModule({
      path: "config.ts",
      exportName: "default",
      plugin
    });
    this.pushDep(this._configModule);
  }
  async getContent() {
    const configImport = this._configModule.import(this);
    const typingsImport = this._typingsModule.import(this);
    return [
      consts.GENERATED_HEADER,
      'import * as sdk from "@botpress/sdk"',
      'import bundle from "./bundle"',
      `import * as ${this._typingsModule.name} from "./${typingsImport}"`,
      `import * as ${this._configModule.name} from "./${configImport}"`,
      "",
      `export type TPlugin = sdk.DefaultPlugin<${this._typingsModule.name}.${this._typingsModule.exportName}>`,
      "",
      `export const configuration = ${this._configModule.name}.${this._configModule.exportName}.configuration`,
      `export const interfaces = ${this._configModule.name}.${this._configModule.exportName}.interfaces`,
      "",
      `export default bundle.initialize(${this._configModule.name}.${this._configModule.exportName})`,
      ""
    ].join("\n");
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BotPluginModule
});
//# sourceMappingURL=plugin-module.js.map
