"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 gen_command_exports = {};
__export(gen_command_exports, {
  GenerateCommand: () => GenerateCommand
});
module.exports = __toCommonJS(gen_command_exports);
var import_chalk = __toESM(require("chalk"));
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var codegen = __toESM(require("../code-generation"));
var errors = __toESM(require("../errors"));
var utils = __toESM(require("../utils"));
var import_project_command = require("./project-command");
class GenerateCommand extends import_project_command.ProjectCommand {
  async run() {
    const { projectType, resolveProjectDefinition } = this.readProjectDefinitionFromFS();
    if (projectType === "interface") {
      this.logger.success("Interface projects have no code to generate since they have no implementation.");
      return;
    }
    if (projectType === "integration") {
      const projectDef = await resolveProjectDefinition();
      return await this._generateIntegration(projectDef.definition);
    }
    if (projectType === "bot") {
      const projectDef = await resolveProjectDefinition();
      return await this._generateBot(projectDef.definition);
    }
    if (projectType === "plugin") {
      const projectDef = await resolveProjectDefinition();
      return await this._generatePlugin(projectDef.definition);
    }
    throw new errors.UnsupportedProjectType();
  }
  async _generateIntegration(integrationDef) {
    this._validateSecrets(integrationDef);
    const line = this.logger.line();
    const { name } = integrationDef;
    line.started(`Generating typings for integration ${import_chalk.default.bold(name)}...`);
    const fromWorkDir = this.projectPaths.rel("workDir");
    const generatedFiles = await codegen.generateIntegrationImplementation(integrationDef);
    await this._writeGeneratedFilesToOutFolder(generatedFiles);
    line.success(`Typings available at ${import_chalk.default.grey(fromWorkDir.outDir)}`);
  }
  async _generateBot(botDefinition) {
    const line = this.logger.line();
    line.started("Generating typings for bot...");
    const fromWorkDir = this.projectPaths.rel("workDir");
    const generatedFiles = await codegen.generateBotImplementation(botDefinition.dereferencePluginEntities());
    await this._writeGeneratedFilesToOutFolder(generatedFiles);
    line.success(`Typings available at ${import_chalk.default.grey(fromWorkDir.outDir)}`);
  }
  async _generatePlugin(pluginDefinition) {
    const line = this.logger.line();
    const { name } = pluginDefinition;
    line.started(`Generating typings for plugin ${import_chalk.default.bold(name)}...`);
    const fromWorkDir = this.projectPaths.rel("workDir");
    const generatedFiles = await codegen.generatePluginImplementation(pluginDefinition.dereferenceEntities());
    await this._writeGeneratedFilesToOutFolder(generatedFiles);
    line.success(`Typings available at ${import_chalk.default.grey(fromWorkDir.outDir)}`);
  }
  async _writeGeneratedFilesToOutFolder(files) {
    for (const file of files) {
      const filePath = utils.path.absoluteFrom(this.projectPaths.abs.outDir, file.path);
      const dirPath = import_path.default.dirname(filePath);
      await import_fs.default.promises.mkdir(dirPath, { recursive: true });
      await import_fs.default.promises.writeFile(filePath, file.content);
    }
  }
  _validateSecrets(integrationDef) {
    const { secrets } = integrationDef;
    if (!secrets) {
      return;
    }
    for (const secretName in secrets) {
      if (!utils.casing.is.screamingSnakeCase(secretName)) {
        throw new errors.BotpressCLIError(`Secret ${secretName} should be in SCREAMING_SNAKE_CASE`);
      }
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  GenerateCommand
});
//# sourceMappingURL=gen-command.js.map
