"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 bot_implementation_exports = {};
__export(bot_implementation_exports, {
  BotImplementationModule: () => BotImplementationModule
});
module.exports = __toCommonJS(bot_implementation_exports);
var consts = __toESM(require("../consts"));
var import_module = require("../module");
var import_bot_plugins = require("./bot-plugins");
var import_bot_typings = require("./bot-typings");
class BotImplementationModule extends import_module.Module {
  _typingsModule;
  _pluginsModule;
  constructor(bot) {
    super({
      exportName: "Bot",
      path: consts.INDEX_FILE
    });
    this._typingsModule = new import_bot_typings.BotTypingsModule(bot);
    this._typingsModule.unshift("typings");
    this.pushDep(this._typingsModule);
    this._pluginsModule = new import_bot_plugins.BotPluginsIndexModule(bot);
    this._pluginsModule.unshift(consts.fromOutDir.pluginsDir);
    this.pushDep(this._pluginsModule);
  }
  async getContent() {
    const {
      //
      _typingsModule: typingsModule,
      _pluginsModule: pluginsModule
    } = this;
    const typingsImport = typingsModule.import(this);
    const pluginsImport = pluginsModule.import(this);
    return [
      consts.GENERATED_HEADER,
      'import * as sdk from "@botpress/sdk"',
      `import * as ${typingsModule.name} from "./${typingsImport}"`,
      `import * as ${pluginsModule.name} from "./${pluginsImport}"`,
      "",
      `export * from "./${typingsImport}"`,
      `export * from "./${pluginsImport}"`,
      "",
      `type TPlugins = ${pluginsModule.name}.TPlugins`,
      `type TBot = sdk.DefaultBot<${typingsModule.name}.${typingsModule.exportName}>`,
      "",
      "export type BotProps = {",
      '  actions: sdk.BotProps<TBot, TPlugins>["actions"]',
      "}",
      "",
      "export class Bot extends sdk.Bot<TBot, TPlugins> {",
      "  public constructor(props: BotProps) {",
      "    super({",
      "      actions: props.actions,",
      `      plugins: ${pluginsModule.name}.${pluginsModule.exportName}`,
      "    })",
      "  }",
      "}",
      "",
      "// extra types",
      "",
      "type AsyncFunction = (...args: any[]) => Promise<any>",
      "",
      "export type BotHandlers = sdk.BotHandlers<TBot>",
      "",
      "export type EventHandlers = Required<{",
      "  [K in keyof BotHandlers['eventHandlers']]: NonNullable<BotHandlers['eventHandlers'][K]>[number]",
      "}>",
      "export type MessageHandlers = Required<{",
      "  [K in keyof BotHandlers['messageHandlers']]: NonNullable<BotHandlers['messageHandlers'][K]>[number]",
      "}>",
      "export type WorkflowHandlers = {",
      "  [TWorkflowName in keyof Required<BotHandlers['workflowHandlers'][keyof BotHandlers['workflowHandlers']]>]:",
      "    Required<BotHandlers['workflowHandlers'][keyof BotHandlers['workflowHandlers']]>[TWorkflowName] extends",
      "      ({ handler: infer U })[] ? U : never",
      "}",
      "",
      "export type MessageHandlerProps = Parameters<MessageHandlers['*']>[0]",
      "export type EventHandlerProps = Parameters<EventHandlers['*']>[0]",
      "export type WorkflowHandlerProps = {",
      "  [TWorkflowName in keyof WorkflowHandlers]: WorkflowHandlers[TWorkflowName] extends",
      "    (..._: infer U) => any ? U[0] : never",
      "}",
      "",
      "export type Client = (MessageHandlerProps | EventHandlerProps)['client']",
      "export type ClientOperation = keyof {",
      "  [K in keyof Client as Client[K] extends AsyncFunction ? K : never]: null",
      "}",
      "export type ClientInputs = {",
      "  [K in ClientOperation]: Parameters<Client[K]>[0]",
      "}",
      "export type ClientOutputs = {",
      "  [K in ClientOperation]: Awaited<ReturnType<Client[K]>>",
      "}"
    ].join("\n");
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BotImplementationModule
});
//# sourceMappingURL=bot-implementation.js.map
