'use strict';

var chunkN72GV5HY_cjs = require('./chunk-N72GV5HY.cjs');
var chunkTKKDUSJ2_cjs = require('./chunk-TKKDUSJ2.cjs');
var chunkOUQKSIVE_cjs = require('./chunk-OUQKSIVE.cjs');
var chunkF77R4YDI_cjs = require('./chunk-F77R4YDI.cjs');
var chunk2ITILIBC_cjs = require('./chunk-2ITILIBC.cjs');
var chunkF5JNSG3I_cjs = require('./chunk-F5JNSG3I.cjs');
var chunk4JZ27NWL_cjs = require('./chunk-4JZ27NWL.cjs');
var chunkVC2OEBZN_cjs = require('./chunk-VC2OEBZN.cjs');
var chunkOTH2GDR2_cjs = require('./chunk-OTH2GDR2.cjs');
var chunk3MH6X7IX_cjs = require('./chunk-3MH6X7IX.cjs');
var chunkUJJENY47_cjs = require('./chunk-UJJENY47.cjs');
var chunkMDPKU5OG_cjs = require('./chunk-MDPKU5OG.cjs');
var chunkSK5IZL6A_cjs = require('./chunk-SK5IZL6A.cjs');
var chunkDC2ELZRE_cjs = require('./chunk-DC2ELZRE.cjs');
var chunkFDZXJN3J_cjs = require('./chunk-FDZXJN3J.cjs');
var chunkF4PTKZYT_cjs = require('./chunk-F4PTKZYT.cjs');
var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var superdiff = require('@donedeal0/superdiff');
var types = require('@storm-software/config-tools/types');
var parseTypeDefinition = require('@stryke/convert/parse-type-definition');
var getEnvPaths = require('@stryke/env/get-env-paths');
var helpers = require('@stryke/fs/helpers');
var install = require('@stryke/fs/install');
var packageFns = require('@stryke/fs/package-fns');
var readFile = require('@stryke/fs/read-file');
var removeFile = require('@stryke/fs/remove-file');
var hash = require('@stryke/hash/hash');
var hashFiles = require('@stryke/hash/hash-files');
var stormJson = require('@stryke/json/storm-json');
var exists = require('@stryke/path/exists');
var filePathFns = require('@stryke/path/file-path-fns');
var joinPaths = require('@stryke/path/join-paths');
var titleCase = require('@stryke/string-format/title-case');
var isNumber = require('@stryke/type-checks/is-number');
var isSetString = require('@stryke/type-checks/is-set-string');
var isString = require('@stryke/type-checks/is-string');
var isUndefined = require('@stryke/type-checks/is-undefined');
var nanoidClient = require('@stryke/unique-id/nanoid-client');
var defu = require('defu');
var jiti = require('jiti');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var defu__default = /*#__PURE__*/_interopDefault(defu);

// ../../node_modules/.pnpm/hookable@5.5.3/node_modules/hookable/dist/index.mjs
function flatHooks(configHooks, hooks = {}, parentName) {
  for (const key in configHooks) {
    const subHook = configHooks[key];
    const name = parentName ? `${parentName}:${key}` : key;
    if (typeof subHook === "object" && subHook !== null) {
      flatHooks(subHook, hooks, name);
    } else if (typeof subHook === "function") {
      hooks[name] = subHook;
    }
  }
  return hooks;
}
chunkAF3E2D32_cjs.__name(flatHooks, "flatHooks");
var defaultTask = { run: /* @__PURE__ */ chunkAF3E2D32_cjs.__name((function_) => function_(), "run") };
var _createTask = /* @__PURE__ */ chunkAF3E2D32_cjs.__name(() => defaultTask, "_createTask");
var createTask = typeof console.createTask !== "undefined" ? console.createTask : _createTask;
function serialTaskCaller(hooks, args) {
  const name = args.shift();
  const task = createTask(name);
  return hooks.reduce(
    (promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
    Promise.resolve()
  );
}
chunkAF3E2D32_cjs.__name(serialTaskCaller, "serialTaskCaller");
function parallelTaskCaller(hooks, args) {
  const name = args.shift();
  const task = createTask(name);
  return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
}
chunkAF3E2D32_cjs.__name(parallelTaskCaller, "parallelTaskCaller");
function callEachWith(callbacks, arg0) {
  for (const callback of [...callbacks]) {
    callback(arg0);
  }
}
chunkAF3E2D32_cjs.__name(callEachWith, "callEachWith");
var Hookable = class {
  static {
    chunkAF3E2D32_cjs.__name(this, "Hookable");
  }
  constructor() {
    this._hooks = {};
    this._before = void 0;
    this._after = void 0;
    this._deprecatedMessages = void 0;
    this._deprecatedHooks = {};
    this.hook = this.hook.bind(this);
    this.callHook = this.callHook.bind(this);
    this.callHookWith = this.callHookWith.bind(this);
  }
  hook(name, function_, options = {}) {
    if (!name || typeof function_ !== "function") {
      return () => {
      };
    }
    const originalName = name;
    let dep;
    while (this._deprecatedHooks[name]) {
      dep = this._deprecatedHooks[name];
      name = dep.to;
    }
    if (dep && !options.allowDeprecated) {
      let message = dep.message;
      if (!message) {
        message = `${originalName} hook has been deprecated` + (dep.to ? `, please use ${dep.to}` : "");
      }
      if (!this._deprecatedMessages) {
        this._deprecatedMessages = /* @__PURE__ */ new Set();
      }
      if (!this._deprecatedMessages.has(message)) {
        console.warn(message);
        this._deprecatedMessages.add(message);
      }
    }
    if (!function_.name) {
      try {
        Object.defineProperty(function_, "name", {
          get: /* @__PURE__ */ chunkAF3E2D32_cjs.__name(() => "_" + name.replace(/\W+/g, "_") + "_hook_cb", "get"),
          configurable: true
        });
      } catch {
      }
    }
    this._hooks[name] = this._hooks[name] || [];
    this._hooks[name].push(function_);
    return () => {
      if (function_) {
        this.removeHook(name, function_);
        function_ = void 0;
      }
    };
  }
  hookOnce(name, function_) {
    let _unreg;
    let _function = /* @__PURE__ */ chunkAF3E2D32_cjs.__name((...arguments_) => {
      if (typeof _unreg === "function") {
        _unreg();
      }
      _unreg = void 0;
      _function = void 0;
      return function_(...arguments_);
    }, "_function");
    _unreg = this.hook(name, _function);
    return _unreg;
  }
  removeHook(name, function_) {
    if (this._hooks[name]) {
      const index = this._hooks[name].indexOf(function_);
      if (index !== -1) {
        this._hooks[name].splice(index, 1);
      }
      if (this._hooks[name].length === 0) {
        delete this._hooks[name];
      }
    }
  }
  deprecateHook(name, deprecated) {
    this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
    const _hooks = this._hooks[name] || [];
    delete this._hooks[name];
    for (const hook of _hooks) {
      this.hook(name, hook);
    }
  }
  deprecateHooks(deprecatedHooks) {
    Object.assign(this._deprecatedHooks, deprecatedHooks);
    for (const name in deprecatedHooks) {
      this.deprecateHook(name, deprecatedHooks[name]);
    }
  }
  addHooks(configHooks) {
    const hooks = flatHooks(configHooks);
    const removeFns = Object.keys(hooks).map(
      (key) => this.hook(key, hooks[key])
    );
    return () => {
      for (const unreg of removeFns.splice(0, removeFns.length)) {
        unreg();
      }
    };
  }
  removeHooks(configHooks) {
    const hooks = flatHooks(configHooks);
    for (const key in hooks) {
      this.removeHook(key, hooks[key]);
    }
  }
  removeAllHooks() {
    for (const key in this._hooks) {
      delete this._hooks[key];
    }
  }
  callHook(name, ...arguments_) {
    arguments_.unshift(name);
    return this.callHookWith(serialTaskCaller, name, ...arguments_);
  }
  callHookParallel(name, ...arguments_) {
    arguments_.unshift(name);
    return this.callHookWith(parallelTaskCaller, name, ...arguments_);
  }
  callHookWith(caller, name, ...arguments_) {
    const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
    if (this._before) {
      callEachWith(this._before, event);
    }
    const result = caller(
      name in this._hooks ? [...this._hooks[name]] : [],
      arguments_
    );
    if (result instanceof Promise) {
      return result.finally(() => {
        if (this._after && event) {
          callEachWith(this._after, event);
        }
      });
    }
    if (this._after && event) {
      callEachWith(this._after, event);
    }
    return result;
  }
  beforeEach(function_) {
    this._before = this._before || [];
    this._before.push(function_);
    return () => {
      if (this._before !== void 0) {
        const index = this._before.indexOf(function_);
        if (index !== -1) {
          this._before.splice(index, 1);
        }
      }
    };
  }
  afterEach(function_) {
    this._after = this._after || [];
    this._after.push(function_);
    return () => {
      if (this._after !== void 0) {
        const index = this._after.indexOf(function_);
        if (index !== -1) {
          this._after.splice(index, 1);
        }
      }
    };
  }
};
function createHooks() {
  return new Hookable();
}
chunkAF3E2D32_cjs.__name(createHooks, "createHooks");
var Engine = class {
  static {
    chunkAF3E2D32_cjs.__name(this, "Engine");
  }
  #initialized = false;
  /**
  * The engine hooks - these allow the plugins to hook into the engines processing
  */
  #hooks;
  /**
  * The plugins provided in the options
  */
  #plugins = [];
  /**
  * The options provided to Storm Stack
  */
  options;
  /**
  * The resolved options provided to Storm Stack
  */
  context;
  /**
  * The logger for the plugin
  */
  log;
  /**
  * The default environment variables to apply
  */
  defaultEnv = {};
  constructor(options, workspaceConfig) {
    this.options = options;
    this.log = chunk3MH6X7IX_cjs.createLog("engine", this.options);
    this.context = this.options;
    this.context.options = options;
    this.context.override ??= {};
    this.context.workspaceConfig = workspaceConfig;
    this.context.errorsFile ??= joinPaths.joinPaths(this.context.workspaceConfig.workspaceRoot, "errors.json");
    this.context.envPaths = getEnvPaths.getEnvPaths({
      orgId: "storm-software",
      appId: "storm-stack",
      workspaceRoot: this.context.workspaceConfig?.workspaceRoot
    });
    if (!this.context.envPaths.cache) {
      throw new Error("The cache directory could not be determined.");
    }
    this.context.envPaths.cache = joinPaths.joinPaths(this.context.envPaths.cache, hash.hash(this.options.projectRoot));
    this.context.resolver = jiti.createJiti(this.context.workspaceConfig.workspaceRoot, {
      interopDefault: true,
      fsCache: joinPaths.joinPaths(this.context.envPaths.cache, "jiti"),
      moduleCache: true
    });
  }
  /**
  * Initialize the engine
  */
  async init() {
    this.log(types.LogLevelLabel.TRACE, "Initializing Storm Stack engine");
    this.#hooks = createHooks();
    this.context = defu__default.default(this.context, await this.loadConfig() ?? {});
    this.context.minify ??= this.context.mode === "production";
    const checksum = await hashFiles.hashDirectory(this.context.projectRoot, {
      ignore: [
        "node_modules",
        ".git",
        ".nx",
        ".cache",
        ".storm",
        "tmp"
      ]
    });
    this.context.meta ??= {
      buildId: nanoidClient.nanoid(24),
      releaseId: nanoidClient.nanoid(24),
      checksum,
      timestamp: Date.now()
    };
    this.context.tsconfig ??= chunk4JZ27NWL_cjs.getTsconfigFilePath(this.context);
    this.context.artifactsDir ??= ".storm";
    this.context.runtimeDir = joinPaths.joinPaths(this.context.artifactsDir, "runtime");
    this.context.typesDir = joinPaths.joinPaths(this.context.artifactsDir, "types");
    const runtimeDir = joinPaths.joinPaths(this.context.workspaceConfig.workspaceRoot, this.context.projectRoot, this.context.runtimeDir);
    this.context.unimportPresets = [
      {
        imports: [
          "StormJSON"
        ],
        from: "@stryke/json"
      },
      {
        imports: [
          "StormURL"
        ],
        from: "@stryke/url"
      },
      {
        imports: [
          "StormError",
          "createStormError",
          "isStormError",
          "getErrorFromUnknown"
        ],
        from: joinPaths.joinPaths(runtimeDir, "error")
      },
      {
        imports: [
          "StormRequest"
        ],
        from: joinPaths.joinPaths(runtimeDir, "request")
      },
      {
        imports: [
          "StormResponse"
        ],
        from: joinPaths.joinPaths(runtimeDir, "response")
      },
      {
        imports: [
          "StormLog"
        ],
        from: joinPaths.joinPaths(runtimeDir, "log")
      },
      {
        imports: [
          "uniqueId",
          "getRandom"
        ],
        from: joinPaths.joinPaths(runtimeDir, "id")
      }
    ];
    const metaFilePath = joinPaths.joinPaths(this.context.projectRoot, this.context.artifactsDir, "meta.json");
    try {
      if (exists.existsSync(metaFilePath)) {
        this.context.persistedMeta = await readFile.readJsonFile(metaFilePath);
      }
    } catch {
      this.context.persistedMeta = void 0;
      await removeFile.removeFile(metaFilePath);
    }
    this.context.outputPath ??= joinPaths.joinPaths("dist", this.context.projectRoot);
    for (const preset of this.context.presets ?? []) {
      await this.addPreset(preset);
    }
    for (const plugin of this.context.plugins ?? []) {
      await this.addPlugin(plugin);
    }
    if (!this.context.plugins) {
      this.log(types.LogLevelLabel.WARN, "No Storm Stack plugins were specified in the options. Please ensure this is correct, as it is generally not recommended.");
    } else {
      for (const plugin of this.#plugins) {
        await Promise.resolve(plugin.addHooks(this.#hooks));
      }
    }
    this.log(types.LogLevelLabel.TRACE, "Checking the Storm Stack project configuration");
    const packageJsonPath = joinPaths.joinPaths(this.context.projectRoot, "package.json");
    if (!exists.existsSync(packageJsonPath)) {
      throw new Error(`Cannot find a \`package.json\` configuration file in ${this.context.projectRoot}.`);
    }
    this.context.packageJson = await readFile.readJsonFile(packageJsonPath);
    const projectJsonPath = joinPaths.joinPaths(this.context.projectRoot, "project.json");
    if (exists.existsSync(projectJsonPath)) {
      this.context.projectJson = await readFile.readJsonFile(projectJsonPath);
      this.context.name ??= this.context.projectJson?.name;
      this.context.projectType ??= this.context.projectJson?.projectType;
    }
    if (this.context.projectType === "application" && this.context.options.entry) {
      if (isSetString.isSetString(this.context.options.entry)) {
        this.context.resolvedEntry = [
          {
            ...this.resolveEntry(this.context, this.context.options.entry),
            input: parseTypeDefinition.parseTypeDefinition(this.context.options.entry)
          }
        ];
      } else if (Array.isArray(this.context.options.entry) && this.context.options.entry.filter(Boolean).length > 0) {
        this.context.resolvedEntry = this.context.options.entry.map((entry) => ({
          ...this.resolveEntry(this.context, entry),
          input: parseTypeDefinition.parseTypeDefinition(entry)
        })).filter(Boolean);
      }
    }
    await this.#hooks.callHook("init:context", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while checking the Storm Stack project configuration: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while checking the Storm Stack project configuration", {
        cause: error
      });
    });
    if (!packageFns.isPackageExists("typescript")) {
      throw new Error(`The TypeScript package is not installed. Please install the package using the command: "npm install typescript --save-dev"`);
    }
    this.log(types.LogLevelLabel.TRACE, `Checking and installing missing project dependencies.`);
    await Promise.all([
      chunk2ITILIBC_cjs.installPackage(this.log, this.context, "@stryke/types", true),
      chunk2ITILIBC_cjs.installPackage(this.log, this.context, "@storm-stack/types", true),
      this.context.projectType === "application" && chunk2ITILIBC_cjs.installPackage(this.log, this.context, "@stryke/type-checks"),
      this.context.projectType === "application" && chunk2ITILIBC_cjs.installPackage(this.log, this.context, "@stryke/json"),
      this.context.projectType === "application" && chunk2ITILIBC_cjs.installPackage(this.log, this.context, "@stryke/url")
    ].filter(Boolean));
    await this.#hooks.callHook("init:installs", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while installing project dependencies: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while installing project dependencies", {
        cause: error
      });
    });
    this.log(types.LogLevelLabel.TRACE, `Checking the TypeScript configuration file (tsconfig.json): ${this.context.tsconfig}`);
    const originalTsconfigJson = await readFile.readJsonFile(this.context.tsconfig);
    const json = await chunk4JZ27NWL_cjs.getTsconfigChanges(this.context);
    if (!json.include?.some((filterPattern) => filterPattern.includes(joinPaths.joinPaths(this.context.artifactsDir, "**/*.ts")))) {
      json.include ??= [];
      json.include.push(joinPaths.joinPaths(this.context.artifactsDir, "**/*.ts"));
    }
    await this.writeFile(this.context.tsconfig, stormJson.StormJSON.stringify(json));
    await this.#hooks.callHook("init:tsconfig", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while resolving the TypeScript options: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while resolving the TypeScript options", {
        cause: error
      });
    });
    const result = superdiff.getObjectDiff(originalTsconfigJson, await readFile.readJsonFile(this.context.tsconfig), {
      ignoreArrayOrder: true,
      showOnly: {
        statuses: [
          "added",
          "deleted",
          "updated"
        ],
        granularity: "deep"
      }
    });
    const changes = [];
    const getChanges = /* @__PURE__ */ chunkAF3E2D32_cjs.__name((difference, property) => {
      if (difference.status === "added" || difference.status === "deleted" || difference.status === "updated") {
        if (difference.diff) {
          for (const diff of difference.diff) {
            getChanges(diff, property ? `${property}.${difference.property}` : difference.property);
          }
        } else {
          changes.push({
            field: property ? `${property}.${difference.property}` : difference.property,
            status: difference.status,
            previous: difference.status === "added" ? "---" : stormJson.StormJSON.stringify(difference.previousValue),
            current: difference.status === "deleted" ? "---" : stormJson.StormJSON.stringify(difference.currentValue)
          });
        }
      }
    }, "getChanges");
    for (const diff of result.diff) {
      getChanges(diff);
    }
    if (changes.length > 0) {
      this.log(types.LogLevelLabel.WARN, `Updating the following configuration values in the "tsconfig.json" file:

  ${changes.map((change, i) => `${i + 1}. ${titleCase.titleCase(change.status)} the ${change.field} field:
   - Previous: ${change.previous}
   - Current: ${change.current}
  `).join("\n")}
  `);
    }
    this.context.resolvedTsconfig = await chunk4JZ27NWL_cjs.getParsedTypeScriptConfig(this.context);
    if (!this.context.resolvedTsconfig) {
      throw new Error("Failed to parse the TypeScript configuration file.");
    }
    this.context.unimport = chunkVC2OEBZN_cjs.createUnimport(this.log, this.context);
    await this.context.unimport.init();
    const handleTransform = /* @__PURE__ */ chunkAF3E2D32_cjs.__name(async (context, sourceFile) => {
      await this.#hooks.callHook("build:transform", this.context, sourceFile).catch((error) => {
        this.log(types.LogLevelLabel.ERROR, `An error occured while transforming the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
        throw new Error("An error occured while transforming the Storm Stack project", {
          cause: error
        });
      });
    }, "handleTransform");
    this.context.compiler = new chunkTKKDUSJ2_cjs.Compiler(this.context, handleTransform);
    this.context.resolvedDotenv = await this.resolveDotenvOptions();
    if (isSetString.isSetString(this.context.resolvedDotenv.docgen)) {
      this.context.resolvedDotenv.docgen = this.context.resolvedDotenv.docgen.replace(this.context.projectRoot, "").trim();
    }
    this.log(types.LogLevelLabel.TRACE, "Storm Stack engine has been initialized");
    this.#initialized = true;
    return this.context;
  }
  /**
  * Clean any previously prepared artifacts
  */
  async clean() {
    if (!this.#initialized) {
      await this.init();
    }
    this.log(types.LogLevelLabel.INFO, "\u{1F9F9} Cleaning the previous Storm Stack artifacts");
    await Promise.all([
      helpers.removeDirectory(joinPaths.joinPaths(this.context.projectRoot, this.context.artifactsDir)),
      helpers.removeDirectory(joinPaths.joinPaths(this.context.workspaceConfig.workspaceRoot, this.context.outputPath?.replace(this.context.workspaceConfig.workspaceRoot, "") || joinPaths.joinPaths("dist", this.context.projectRoot)))
    ]);
    await this.#hooks.callHook("clean:execute", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while cleaning the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while cleaning the Storm Stack project", {
        cause: error
      });
    });
    this.log(types.LogLevelLabel.TRACE, "Storm Stack - Clean command completed");
  }
  /**
  * Prepare the Storm Stack project prior to building
  *
  * @remarks
  * This method will create the necessary directories, and write the artifacts files to the project.
  *
  * @param autoClean - Whether to automatically clean the previous build artifacts before preparing the project
  */
  async prepare(autoClean = true) {
    if (!this.#initialized) {
      await this.init();
    }
    if (exists.existsSync(joinPaths.joinPaths(this.context.projectRoot, this.context.artifactsDir)) && autoClean) {
      await this.clean();
    }
    this.log(types.LogLevelLabel.INFO, "Preparing the Storm Stack project");
    if (!exists.existsSync(joinPaths.joinPaths(this.context.projectRoot, this.context.artifactsDir))) {
      await helpers.createDirectory(joinPaths.joinPaths(this.context.projectRoot, this.context.artifactsDir));
    }
    if (!exists.existsSync(joinPaths.joinPaths(this.context.projectRoot, this.context.runtimeDir))) {
      await helpers.createDirectory(joinPaths.joinPaths(this.context.projectRoot, this.context.runtimeDir));
    }
    if (!exists.existsSync(joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir))) {
      await helpers.createDirectory(joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir));
    }
    await this.writeDotenvReflection();
    await this.writeFile(joinPaths.joinPaths(this.context.projectRoot, this.context.artifactsDir, "meta.json"), stormJson.StormJSON.stringify(this.context.meta));
    this.context.persistedMeta = this.context.meta;
    await this.writeDeclarations();
    await this.#hooks.callHook("prepare:types", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while writing type artifacts: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while generating the type artifacts", {
        cause: error
      });
    });
    if (this.context.projectType === "application") {
      await this.context.unimport.dumpImports();
      const runtimeDir = joinPaths.joinPaths(this.context.projectRoot, this.context.runtimeDir);
      this.log(types.LogLevelLabel.TRACE, `Writing to runtime directory "${runtimeDir}"`);
      await Promise.all([
        this.writeFile(joinPaths.joinPaths(runtimeDir, "request.ts"), chunkSK5IZL6A_cjs.writeRequest()),
        this.writeFile(joinPaths.joinPaths(runtimeDir, "response.ts"), chunkDC2ELZRE_cjs.writeResponse()),
        this.writeFile(joinPaths.joinPaths(runtimeDir, "error.ts"), chunkFDZXJN3J_cjs.writeError()),
        this.writeFile(joinPaths.joinPaths(runtimeDir, "id.ts"), chunkF4PTKZYT_cjs.writeId()),
        this.writeFile(joinPaths.joinPaths(runtimeDir, "log.ts"), chunkMDPKU5OG_cjs.writeLog())
      ]);
      await this.#hooks.callHook("prepare:runtime", this.context).catch((error) => {
        this.log(types.LogLevelLabel.ERROR, `An error occured while generating the runtime artifacts: ${error.message} 
${error.stack ?? ""}`);
        throw new Error("An error occured while generating the runtime artifacts", {
          cause: error
        });
      });
      await this.#hooks.callHook("prepare:entry", this.context).catch((error) => {
        this.log(types.LogLevelLabel.ERROR, `An error occured while creating entry artifacts: ${error.message} 
${error.stack ?? ""}`);
        throw new Error("An error occured while creating entry artifacts", {
          cause: error
        });
      });
      await this.#hooks.callHook("prepare:deploy", this.context).catch((error) => {
        this.log(types.LogLevelLabel.ERROR, `An error occured while creating deployment artifacts: ${error.message} 
${error.stack ?? ""}`);
        throw new Error("An error occured while creating deployment artifacts", {
          cause: error
        });
      });
    }
    await this.#hooks.callHook("prepare:misc", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while creating miscellaneous artifacts: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while creating miscellaneous artifacts", {
        cause: error
      });
    });
    this.context.resolvedTsconfig = await chunk4JZ27NWL_cjs.getParsedTypeScriptConfig(this.context);
    if (!this.context.resolvedTsconfig) {
      throw new Error("Failed to parse the TypeScript configuration file.");
    }
  }
  /**
  * Lint the project
  *
  * @param autoPrepare - Whether to automatically prepare the project if it has not been prepared
  * @param autoClean - Whether to automatically clean the previous build artifacts before preparing the project
  * @param lintDuringBuild - Whether to lint the project during the build process
  */
  async lint(autoPrepare = true, autoClean = true, lintDuringBuild = false) {
    if (!this.#initialized) {
      await this.init();
    }
    if (this.context.persistedMeta?.checksum !== this.context.meta.checksum) {
      if (autoPrepare) {
        this.log(types.LogLevelLabel.INFO, "The Storm Stack project has been modified since the last time `prepare` was ran. Re-preparing the project.");
        await this.prepare(autoClean);
      } else {
        throw new Error("The Storm Stack project has not been prepared. Please run the `prepare` command before trying to lint the project.");
      }
    }
    this.log(types.LogLevelLabel.INFO, "Linting the Storm Stack project");
    await chunkN72GV5HY_cjs.runLintCheck(this.log, this.context, {
      lintDuringBuild,
      eslintOptions: {
        cacheLocation: joinPaths.joinPaths(this.context.envPaths.cache, "eslint")
      }
    });
    this.log(types.LogLevelLabel.TRACE, "Storm Stack documentation generation completed");
  }
  /**
  * Build the project
  *
  * @param autoPrepare - Whether to automatically prepare the project if it has not been prepared
  * @param autoClean - Whether to automatically clean the previous build artifacts before preparing the project
  */
  async build(autoPrepare = true, autoClean = true) {
    if (!this.#initialized) {
      await this.init();
    }
    if (this.context.persistedMeta?.checksum !== this.context.meta.checksum) {
      if (autoPrepare) {
        this.log(types.LogLevelLabel.INFO, "The Storm Stack project has been modified since the last time `prepare` was ran. Re-preparing the project.");
        await this.prepare(autoClean);
      } else {
        throw new Error("The Storm Stack project has not been prepared. Please run the `prepare` command before trying to build the project.");
      }
    }
    if (this.context.skipLint === false) {
      await this.lint(autoPrepare, autoClean, true);
    }
    this.log(types.LogLevelLabel.INFO, "Building the Storm Stack project");
    await this.#hooks.callHook("build:execute", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while building the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while building the Storm Stack project", {
        cause: error
      });
    });
  }
  /**
  * Generate the documentation for the project
  *
  * @param autoPrepare - Whether to automatically prepare the project if it has not been prepared
  * @param autoClean - Whether to automatically clean the previous build artifacts before preparing the project
  */
  async docs(autoPrepare = true, autoClean = true) {
    if (!this.#initialized) {
      await this.init();
    }
    if (this.context.persistedMeta?.checksum !== this.context.meta.checksum) {
      if (autoPrepare) {
        this.log(types.LogLevelLabel.INFO, "The Storm Stack project has been modified since the last time `prepare` was ran. Re-preparing the project.");
        await this.prepare(autoClean);
      } else {
        throw new Error("The Storm Stack project has not been prepared. Please run the `prepare` command before trying to build the project.");
      }
    }
    this.log(types.LogLevelLabel.INFO, "Generating documentation for the Storm Stack project");
    await this.writeDotenvDoc();
    await this.#hooks.callHook("docs:generate", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while generating documentation for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while generating documentation for the Storm Stack project", {
        cause: error
      });
    });
    this.log(types.LogLevelLabel.TRACE, "Storm Stack documentation generation completed");
  }
  /**
  * Finalization process
  *
  * @remarks
  * This step includes any final processes or clean up required by Storm Stack. It will be run after each Storm Stack command.
  */
  async finalize() {
    this.log(types.LogLevelLabel.TRACE, "Storm Stack finalize execution started");
    await this.#hooks.callHook("finalize:execute", this.context).catch((error) => {
      this.log(types.LogLevelLabel.ERROR, `An error occured while finalizing the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
      throw new Error("An error occured while finalizing the Storm Stack project", {
        cause: error
      });
    });
    this.log(types.LogLevelLabel.TRACE, "Storm Stack finalize execution completed");
  }
  resolveEntry(options, entry) {
    const parsed = parseTypeDefinition.parseTypeDefinition(entry);
    const entryFile = parsed.file.replace(options.projectRoot, "").replaceAll("\\", "/").replaceAll(/^(?:\.\/)*/g, "");
    return {
      file: joinPaths.joinPaths(options.projectRoot, options.artifactsDir, `entry-${hash.hash({
        file: entryFile,
        name: parsed.name
      }, {
        maxLength: 24
      }).replaceAll("-", "0").replaceAll("_", "1")}.ts`),
      name: parsed.name
    };
  }
  /**
  * Add a Storm Stack preset to the build process
  *
  * @param preset - The import path of the preset to add
  */
  async addPreset(preset) {
    if (preset && !this.#plugins.some((p) => typeof preset === "string" ? p.installPath === preset : p.installPath === preset[0])) {
      const pluginConfig = typeof preset === "string" ? [
        preset,
        {}
      ] : preset;
      const isInstalled = packageFns.isPackageExists(pluginConfig[0], {
        paths: [
          this.context.workspaceConfig.workspaceRoot,
          this.context.projectRoot
        ]
      });
      if (!isInstalled && this.context.skipInstalls !== true) {
        this.log(types.LogLevelLabel.WARN, `The preset package "${pluginConfig[0]}" is not installed. It will be installed automatically.`);
        const result = await install.install(pluginConfig[0], {
          cwd: this.context.projectRoot
        });
        if (isNumber.isNumber(result.exitCode) && result.exitCode > 0) {
          this.log(types.LogLevelLabel.ERROR, result.stderr);
          throw new Error(`An error occurred while installing the build preset package "${pluginConfig[0]}" `);
        }
      }
      let presetInstance;
      try {
        const module = await this.context.resolver.import(this.context.resolver.esmResolve(pluginConfig[0]));
        const PresetConstructor = module.default;
        presetInstance = new PresetConstructor(pluginConfig[1]);
      } catch (error) {
        if (!isInstalled) {
          throw new Error(`The preset package "${pluginConfig[0]}" is not installed. Please install the package using the command: "npm install ${pluginConfig[0]} --save-dev"`);
        } else {
          throw new Error(`An error occurred while importing the build preset package "${pluginConfig[0]}":
${error.message}

Note: Please ensure the preset package's default export is a class that extends \`Plugin\` with a constructor that excepts zero arguments.`);
        }
      }
      if (!presetInstance) {
        throw new Error(`The preset package "${pluginConfig[0]}" does not export a valid module.`);
      }
      if (!presetInstance.name) {
        throw new Error(`The module in the build preset package "${pluginConfig[0]}" must export a \`name\` string value.`);
      }
      if (!presetInstance.addHooks) {
        throw new Error(`The build preset "${presetInstance.name}" must export an \`addHooks\` function.`);
      }
      if (presetInstance.dependencies) {
        for (const dependency of presetInstance.dependencies) {
          await this.addPlugin(dependency);
        }
      }
      this.log(types.LogLevelLabel.INFO, `Successfully initialized the "${presetInstance.name}" preset`);
      this.#plugins.push(presetInstance);
    }
  }
  /**
  * Add a Storm Stack plugin to the build process
  *
  * @param plugin - The import path of the plugin to add
  */
  async addPlugin(plugin) {
    if (plugin && !this.#plugins.some((p) => typeof plugin === "string" ? p.installPath === plugin : p.installPath === plugin[0])) {
      const pluginConfig = typeof plugin === "string" ? [
        plugin,
        {}
      ] : plugin;
      const isInstalled = packageFns.isPackageExists(pluginConfig[0], {
        paths: [
          this.context.workspaceConfig.workspaceRoot,
          this.context.projectRoot
        ]
      });
      if (!isInstalled && this.context.skipInstalls !== true) {
        this.log(types.LogLevelLabel.WARN, `The plugin package "${pluginConfig[0]}" is not installed. It will be installed automatically.`);
        const result = await install.install(pluginConfig[0], {
          cwd: this.context.projectRoot
        });
        if (isNumber.isNumber(result.exitCode) && result.exitCode > 0) {
          this.log(types.LogLevelLabel.ERROR, result.stderr);
          throw new Error(`An error occurred while installing the build plugin package "${pluginConfig[0]}" `);
        }
      }
      let pluginInstance;
      try {
        const module = await this.context.resolver.import(this.context.resolver.esmResolve(pluginConfig[0]));
        const PluginConstructor = module.default;
        pluginInstance = new PluginConstructor(pluginConfig[1]);
      } catch (error) {
        if (!isInstalled) {
          throw new Error(`The plugin package "${pluginConfig[0]}" is not installed. Please install the package using the command: "npm install ${pluginConfig[0]} --save-dev"`);
        } else {
          throw new Error(`An error occurred while importing the build plugin package "${pluginConfig[0]}":
${error.message}

Note: Please ensure the plugin package's default export is a class that extends \`Plugin\` with a constructor that excepts zero arguments.`);
        }
      }
      if (!pluginInstance) {
        throw new Error(`The plugin package "${pluginConfig[0]}" does not export a valid module.`);
      }
      if (!pluginInstance.name) {
        throw new Error(`The module in the build plugin package "${pluginConfig[0]}" must export a \`name\` string value.`);
      }
      if (!pluginInstance.addHooks) {
        throw new Error(`The build plugin "${pluginInstance.name}" must export an \`addHooks\` function.`);
      }
      this.log(types.LogLevelLabel.INFO, `Successfully initialized the "${pluginInstance.name}" plugin`);
      this.#plugins.push(pluginInstance);
    }
  }
  /**
  * Load the Storm Stack configuration
  *
  * @returns The Storm Stack configuration
  */
  async loadConfig() {
    return chunkF5JNSG3I_cjs.loadConfig(this.context.projectRoot, this.context.mode, joinPaths.joinPaths(this.context.envPaths.cache, "jiti"));
  }
  /**
  * Resolve the dotenv options
  *
  * @returns The resolved dotenv options
  */
  async resolveDotenvOptions() {
    const dotenv = {};
    dotenv.types = await chunkOUQKSIVE_cjs.reflectDotenvTypes(this.log, this.context);
    dotenv.additionalFiles = this.context.dotenv?.additionalFiles ?? [];
    dotenv.docgen = this.context.dotenv?.docgen ?? joinPaths.joinPaths(this.context.projectRoot, "docs", "dotenv.md");
    dotenv.replace = Boolean(this.context.dotenv?.replace ?? this.context.projectType === "application");
    const env = defu__default.default(await chunkOUQKSIVE_cjs.loadEnv(this.context, dotenv, this.context.envPaths.cache, this.context.envPaths.config, this.context.packageJson, this.context.workspaceConfig), this.defaultEnv, {
      APP_NAME: this.context.name || this.context.packageJson.name?.replace(`/${this.context.workspaceConfig.namespace}`, ""),
      APP_VERSION: this.context.packageJson.version,
      BUILD_ID: this.context.meta.buildId,
      BUILD_TIMESTAMP: this.context.meta.timestamp,
      BUILD_CHECKSUM: this.context.meta.checksum,
      RELEASE_ID: this.context.meta.releaseId,
      MODE: this.context.mode,
      ORG: this.context.workspaceConfig.organization,
      ORGANIZATION: this.context.workspaceConfig.organization,
      PLATFORM: this.context.platform,
      STACKTRACE: this.context.mode === "development",
      ENVIRONMENT: this.context.mode,
      DEVELOPMENT: this.context.mode === "development",
      STAGING: this.context.mode === "staging",
      PRODUCTION: this.context.mode === "production",
      DEBUG: this.context.mode === "development"
    });
    dotenv.values = Object.keys(env).reduce((ret, key) => {
      let value = env[key];
      if (isString.isString(value)) {
        value = `"${value.replaceAll('"', "")}"`;
      } else if (!isUndefined.isUndefined(value)) {
        value = String(value);
      }
      ret[key.replaceAll("(", "").replaceAll(")", "")] = value;
      return ret;
    }, env);
    return dotenv;
  }
  /**
  * Write the dotenv reflection file
  */
  async writeDotenvReflection() {
    await chunkTKKDUSJ2_cjs.writeDotenvReflection(this.log, this.context, this.context.resolvedDotenv.types.variables.reflection, "variables");
    if (this.context.resolvedDotenv.types.secrets?.reflection) {
      await chunkTKKDUSJ2_cjs.writeDotenvReflection(this.log, this.context, this.context.resolvedDotenv.types.secrets?.reflection, "secrets");
    }
  }
  /**
  * Write the Typescript declarations
  */
  async writeDeclarations() {
    const dtsFile = joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir, "env.d.ts");
    this.log(types.LogLevelLabel.TRACE, `Writing a declaration file in "${dtsFile}"`);
    let content = "";
    if (this.context.resolvedDotenv.types.variables.reflection) {
      content = chunkF77R4YDI_cjs.generateDeclarations(this.context.resolvedDotenv.types.variables);
    }
    const dtsFilePath = filePathFns.findFilePath(dtsFile);
    if (dtsFilePath && !exists.existsSync(dtsFilePath)) {
      await helpers.createDirectory(dtsFilePath);
    }
    return Promise.all([
      content && this.writeFile(dtsFile, content),
      this.writeFile(joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir, "modules.d.ts"), chunkF77R4YDI_cjs.generateImports(filePathFns.relativePath(joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir), joinPaths.joinPaths(this.context.projectRoot, this.context.runtimeDir)))),
      this.writeFile(joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir, "global.d.ts"), chunkF77R4YDI_cjs.generateGlobal(filePathFns.relativePath(joinPaths.joinPaths(this.context.projectRoot, this.context.typesDir), joinPaths.joinPaths(this.context.projectRoot, this.context.runtimeDir))))
    ].filter(Boolean));
  }
  /**
  * Write the dotenv documentation markdown file
  */
  async writeDotenvDoc() {
    if (isSetString.isSetString(this.context.resolvedDotenv.docgen)) {
      const vars = await chunkOTH2GDR2_cjs.resolveDotenvProperties(this.log, this.context, "variables");
      const docgenFile = joinPaths.joinPaths(this.context.projectRoot, this.context.resolvedDotenv.docgen.replace(this.context.projectRoot, "").trim());
      this.log(types.LogLevelLabel.TRACE, `Documenting environment variables configuration in "${docgenFile}"`);
      const docgenFilePath = filePathFns.findFilePath(docgenFile);
      if (docgenFilePath && !exists.existsSync(docgenFilePath)) {
        await helpers.createDirectory(docgenFilePath);
      }
      return this.writeFile(docgenFile, chunkOUQKSIVE_cjs.generateDotenvMarkdown(this.context.packageJson, vars));
    }
    this.log(types.LogLevelLabel.INFO, "The `dotenv.docgen` option was set to `false`. Skipping dotenv documentation.");
  }
  /**
  * Writes a file to the file system
  */
  async writeFile(filepath, content) {
    this.log(types.LogLevelLabel.TRACE, `Writing file ${filepath} to disk`);
    return chunkUJJENY47_cjs.writeFile(this.log, filepath, content);
  }
};

exports.Engine = Engine;
//# sourceMappingURL=chunk-NUVFI655.cjs.map
//# sourceMappingURL=chunk-NUVFI655.cjs.map