"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 bundle_command_exports = {};
__export(bundle_command_exports, {
  BundleCommand: () => BundleCommand
});
module.exports = __toCommonJS(bundle_command_exports);
var import_chalk = __toESM(require("chalk"));
var errors = __toESM(require("../errors"));
var utils = __toESM(require("../utils"));
var import_project_command = require("./project-command");
class BundleCommand extends import_project_command.ProjectCommand {
  async run(buildContext) {
    const { projectType, resolveProjectDefinition } = this.readProjectDefinitionFromFS();
    const abs = this.projectPaths.abs;
    const rel = this.projectPaths.rel("workDir");
    const line = this.logger.line();
    if (projectType === "interface") {
      this.logger.success("Interface projects have no implementation to bundle.");
    } else if (projectType === "integration") {
      const projectDef = await resolveProjectDefinition();
      const { name, __advanced } = projectDef.definition;
      line.started(`Bundling integration ${import_chalk.default.bold(name)}...`);
      await this._bundle(abs.outFileCJS, buildContext, __advanced?.esbuild ?? {});
    } else if (projectType === "bot") {
      line.started("Bundling bot...");
      await this._bundle(abs.outFileCJS, buildContext);
    } else if (projectType === "plugin") {
      line.started("Bundling plugin for node and browser platforms...");
      await Promise.all([
        this._bundle(abs.outFileCJS, buildContext),
        this._bundle(abs.outFileESM, buildContext, { platform: "browser", format: "esm" })
      ]);
    } else {
      throw new errors.UnsupportedProjectType();
    }
    line.success(`Bundle available at ${import_chalk.default.grey(rel.outDir)}`);
  }
  async _bundle(outfile, buildContext, props = {}) {
    const abs = this.projectPaths.abs;
    const context = buildContext ?? new utils.esbuild.BuildCodeContext();
    await context.rebuild(
      {
        outfile,
        absWorkingDir: abs.workDir,
        code: this._code
      },
      {
        ...this._buildOptions,
        ...props
      }
    );
  }
  get _code() {
    const rel = this.projectPaths.rel("workDir");
    const unixPath = utils.path.toUnix(rel.entryPoint);
    const importFrom = utils.path.rmExtension(unixPath);
    return `import x from './${importFrom}'; export default x; export const handler = x.handler;`;
  }
  get _buildOptions() {
    return {
      logLevel: this.argv.verbose ? "info" : "silent",
      sourcemap: this.argv.sourceMap,
      minify: this.argv.minify
    };
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BundleCommand
});
//# sourceMappingURL=bundle-command.js.map
