"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_implementation_exports = {};
__export(plugin_implementation_exports, {
  PluginImplementationModule: () => PluginImplementationModule
});
module.exports = __toCommonJS(plugin_implementation_exports);
var consts = __toESM(require("../consts"));
var import_module = require("../module");
var import_plugin_typings = require("./plugin-typings");
class PluginImplementationModule extends import_module.Module {
  _typingsModule;
  constructor(plugin) {
    super({
      exportName: "Plugin",
      path: consts.INDEX_FILE
    });
    this._typingsModule = new import_plugin_typings.PluginTypingsModule(plugin);
    this._typingsModule.unshift("typings");
    this.pushDep(this._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}"`,
      `export * from "./${typingsImport}"`,
      "",
      `type TPlugin = sdk.DefaultPlugin<${this._typingsModule.name}.${this._typingsModule.exportName}>`,
      "",
      "export class Plugin extends sdk.Plugin<TPlugin> {}",
      "",
      "export type PluginProps = sdk.PluginProps<TPlugin>",
      "export type PluginRuntimeProps = sdk.PluginRuntimeProps<TPlugin>",
      "",
      "// extra types",
      "",
      "type ValueOf<T> = T[keyof T]",
      "type AsyncFunction = (...args: any[]) => Promise<any>",
      "",
      "export type PluginHandlers = sdk.PluginHandlers<TPlugin>",
      "",
      "export type EventHandlers = Required<{",
      "  [K in keyof PluginHandlers['eventHandlers']]: NonNullable<PluginHandlers['eventHandlers'][K]>[number]",
      "}>",
      "export type MessageHandlers = Required<{",
      "  [K in keyof PluginHandlers['messageHandlers']]: NonNullable<PluginHandlers['messageHandlers'][K]>[number]",
      "}>",
      "export type HookHandlers = Required<{",
      "  [H in keyof PluginHandlers['hookHandlers']]: Required<{",
      "    [K in keyof PluginHandlers['hookHandlers'][H]]: NonNullable<PluginHandlers['hookHandlers'][H][K]>[number]",
      "  }>",
      "}>",
      "export type WorkflowHandlers = {",
      "  [TWorkflowName in keyof Required<PluginHandlers['workflowHandlers'][keyof PluginHandlers['workflowHandlers']]>]:",
      "    Required<PluginHandlers['workflowHandlers'][keyof PluginHandlers['workflowHandlers']]>[TWorkflowName] extends",
      "      ({ handler: infer U })[] ? U : never",
      "}",
      "",
      "export type AnyMessageHandler = MessageHandlers['*']",
      "export type AnyEventHandler = EventHandlers['*']",
      "export type AnyActionHandler = ValueOf<PluginHandlers['actionHandlers']>",
      "export type AnyHookHanders = {",
      "  [H in keyof HookHandlers]: NonNullable<HookHandlers[H]['*']>",
      "}",
      "",
      "export type MessageHandlerProps = Parameters<AnyMessageHandler>[0]",
      "export type EventHandlerProps = Parameters<AnyEventHandler>[0]",
      "export type ActionHandlerProps = Parameters<AnyActionHandler>[0]",
      "export type HookHandlerProps = {",
      "  [H in keyof AnyHookHanders]: Parameters<NonNullable<AnyHookHanders[H]>>[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 = {
  PluginImplementationModule
});
//# sourceMappingURL=plugin-implementation.js.map
