'use strict';

var chunkITBQZGEH_cjs = require('./chunk-ITBQZGEH.cjs');
var chunk56PCLWE5_cjs = require('./chunk-56PCLWE5.cjs');
var chunkRR3FI3SI_cjs = require('./chunk-RR3FI3SI.cjs');
var chunkGYZ46TGN_cjs = require('./chunk-GYZ46TGN.cjs');
var chunk2WCJPMYO_cjs = require('./chunk-2WCJPMYO.cjs');
var chunkQSKSKZ22_cjs = require('./chunk-QSKSKZ22.cjs');
var chunkJ4GZ62WR_cjs = require('./chunk-J4GZ62WR.cjs');
var chunkCL2TEPHB_cjs = require('./chunk-CL2TEPHB.cjs');
var chunkGUYJPYA3_cjs = require('./chunk-GUYJPYA3.cjs');
var chunkG5WHCB24_cjs = require('./chunk-G5WHCB24.cjs');
var chunk6R7WPC5T_cjs = require('./chunk-6R7WPC5T.cjs');
var chunkV7LFKZZ7_cjs = require('./chunk-V7LFKZZ7.cjs');
var chunkQ2RX2PCI_cjs = require('./chunk-Q2RX2PCI.cjs');
var chunkDNBPSJZB_cjs = require('./chunk-DNBPSJZB.cjs');
var chunk4UERBOUH_cjs = require('./chunk-4UERBOUH.cjs');
var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var types = require('@storm-software/config-tools/types');
var stormJson = require('@stryke/json/storm-json');
var joinPaths = require('@stryke/path/join-paths');
var helpers = require('@stryke/fs/helpers');
var exists = require('@stryke/path/exists');
var filePathFns = require('@stryke/path/file-path-fns');

async function prepareDirectories(log, context, hooks) {
  log(types.LogLevelLabel.TRACE, `Preparing the artifact directories for the Storm Stack project.`);
  if (!exists.existsSync(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir))) {
    await helpers.createDirectory(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir));
  }
  if (context.options.projectType === "application") {
    if (!exists.existsSync(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "runtime"))) {
      await helpers.createDirectory(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "runtime"));
    }
  }
  if (!exists.existsSync(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "types"))) {
    await helpers.createDirectory(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "types"));
  }
  if (!exists.existsSync(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "reflections"))) {
    await helpers.createDirectory(joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "reflections"));
  }
  await hooks.callHook("prepare:directories", context).catch((error) => {
    log(types.LogLevelLabel.ERROR, `An error occurred while preparing the artifact directories for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
    throw new Error("An error occurred while preparing the artifact directories for the Storm Stack project", {
      cause: error
    });
  });
}
chunkAF3E2D32_cjs.__name(prepareDirectories, "prepareDirectories");
async function prepareReflections(log, context, hooks) {
  log(types.LogLevelLabel.TRACE, `Preparing the reflection artifacts for the Storm Stack project.`);
  await chunkDNBPSJZB_cjs.writeDotenvReflection(log, context, context.dotenv.types.variables.reflection, "variables");
  if (context.dotenv.types.secrets?.reflection) {
    await chunkDNBPSJZB_cjs.writeDotenvReflection(log, context, context.dotenv.types.secrets?.reflection, "secrets");
  }
  await hooks.callHook("prepare:reflections", context).catch((error) => {
    log(types.LogLevelLabel.ERROR, `An error occurred while preparing the reflection artifacts for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
    throw new Error("An error occurred while preparing the reflection artifacts for the Storm Stack project", {
      cause: error
    });
  });
}
chunkAF3E2D32_cjs.__name(prepareReflections, "prepareReflections");
async function prepareRuntime(log, context, hooks) {
  log(types.LogLevelLabel.TRACE, `Preparing the runtime artifacts for the Storm Stack project.`);
  const runtimeDir = joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "runtime");
  const promises = [
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "request.ts"), chunk2WCJPMYO_cjs.writeRequest()),
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "response.ts"), chunkQSKSKZ22_cjs.writeResponse()),
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "error.ts"), chunkCL2TEPHB_cjs.writeError()),
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "id.ts"), chunkGUYJPYA3_cjs.writeId()),
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "log.ts"), chunkGYZ46TGN_cjs.writeLog(context), true),
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "storage.ts"), chunkJ4GZ62WR_cjs.writeStorage(context)),
    chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "init.ts"), chunkG5WHCB24_cjs.writeInit(context))
  ];
  if (context.options.platform === "node") {
    promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "app.ts"), chunkITBQZGEH_cjs.writeApp(context)));
    promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "context.ts"), chunk56PCLWE5_cjs.writeContext(context)));
    promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(runtimeDir, "event.ts"), chunkRR3FI3SI_cjs.writeEvent(context)));
  }
  await Promise.all(promises);
  await hooks.callHook("prepare:runtime", context).catch((error) => {
    log(types.LogLevelLabel.ERROR, `An error occurred while preparing the runtime artifacts for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
    throw new Error("An error occurred while preparing the runtime artifacts for the Storm Stack project", {
      cause: error
    });
  });
}
chunkAF3E2D32_cjs.__name(prepareRuntime, "prepareRuntime");
async function prepareTypes(log, context, hooks) {
  log(types.LogLevelLabel.TRACE, `Preparing the type declarations for the Storm Stack project.`);
  const typesDir = joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "types");
  const relativeRuntimePath = filePathFns.relativePath(joinPaths.joinPaths(context.workspaceConfig.workspaceRoot, context.options.projectRoot, "types"), joinPaths.joinPaths(context.workspaceConfig.workspaceRoot, context.options.projectRoot, "runtime"));
  const promises = [];
  if (context.dotenv.types.variables.reflection) {
    if (context.options.platform === "node") {
      promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(typesDir, "context.d.ts"), chunk6R7WPC5T_cjs.generateNodeDeclarations(context.dotenv.types.variables, context)));
    } else {
      promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(typesDir, "vars.d.ts"), chunkV7LFKZZ7_cjs.generateSharedDeclarations(context.dotenv.types.variables)));
    }
  }
  promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(typesDir, "global.d.ts"), chunkV7LFKZZ7_cjs.generateSharedGlobal(relativeRuntimePath)));
  promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(typesDir, "modules.d.ts"), chunkV7LFKZZ7_cjs.generateSharedModules(relativeRuntimePath)));
  if (context.options.platform === "node") {
    promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(typesDir, "global-node.d.ts"), chunk6R7WPC5T_cjs.generateNodeGlobal(relativeRuntimePath, context)));
    promises.push(chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(typesDir, "modules-node.d.ts"), chunk6R7WPC5T_cjs.generateNodeModules(relativeRuntimePath, context)));
  }
  await Promise.all(promises);
  await hooks.callHook("prepare:types", context).catch((error) => {
    log(types.LogLevelLabel.ERROR, `An error occurred while preparing the type declarations for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
    throw new Error("An error occurred while preparing the type declarations for the Storm Stack project", {
      cause: error
    });
  });
}
chunkAF3E2D32_cjs.__name(prepareTypes, "prepareTypes");

// src/prepare/index.ts
async function prepare(log, context, hooks) {
  await chunk4UERBOUH_cjs.writeFile(log, joinPaths.joinPaths(context.options.projectRoot, context.artifactsDir, "meta.json"), stormJson.StormJSON.stringify(context.meta));
  context.persistedMeta = context.meta;
  await hooks.callHook("prepare:begin", context).catch((error) => {
    log(types.LogLevelLabel.ERROR, `An error occured while starting the prepare process for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
    throw new Error("An error occured while starting the prepare process for the Storm Stack project", {
      cause: error
    });
  });
  await prepareDirectories(log, context, hooks);
  await prepareReflections(log, context, hooks);
  await prepareTypes(log, context, hooks);
  if (context.options.projectType === "application") {
    await context.unimport.dumpImports();
    await prepareRuntime(log, context, hooks);
    await hooks.callHook("prepare:entry", context).catch((error) => {
      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 hooks.callHook("prepare:deploy", context).catch((error) => {
      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 hooks.callHook("prepare:misc", context).catch((error) => {
    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
    });
  });
  context.tsconfig = await chunkQ2RX2PCI_cjs.getParsedTypeScriptConfig(context);
  if (!context.tsconfig) {
    throw new Error("Failed to parse the TypeScript configuration file.");
  }
  await hooks.callHook("prepare:complete", context).catch((error) => {
    log(types.LogLevelLabel.ERROR, `An error occured while finishing the prepare process for the Storm Stack project: ${error.message} 
${error.stack ?? ""}`);
    throw new Error("An error occured while finishing the prepare process for the Storm Stack project", {
      cause: error
    });
  });
}
chunkAF3E2D32_cjs.__name(prepare, "prepare");

exports.prepare = prepare;
//# sourceMappingURL=chunk-5K6FNPZH.cjs.map
//# sourceMappingURL=chunk-5K6FNPZH.cjs.map