import { writeApp } from './chunk-K6U4ZMUD.js';
import { writeContext } from './chunk-3QBUUTSI.js';
import { writeEvent } from './chunk-KZHNONBH.js';
import { writeLog } from './chunk-SP3MMBOT.js';
import { writeRequest } from './chunk-S6HDEYLE.js';
import { writeResponse } from './chunk-367I7J2U.js';
import { writeStorage } from './chunk-RQILOZ7Z.js';
import { writeError } from './chunk-XH26CPTN.js';
import { writeId } from './chunk-ZLYR6CJC.js';
import { writeInit } from './chunk-REVDLJOK.js';
import { generateNodeDeclarations, generateNodeGlobal, generateNodeModules } from './chunk-Q2XB6XLY.js';
import { generateSharedDeclarations, generateSharedGlobal, generateSharedModules } from './chunk-2EVVSKOX.js';
import { getParsedTypeScriptConfig } from './chunk-JRP7TVNX.js';
import { writeDotenvReflection } from './chunk-CBI6CPYK.js';
import { writeFile } from './chunk-4SEZDGHZ.js';
import { __name } from './chunk-6AXVLTU5.js';
import { LogLevelLabel } from '@storm-software/config-tools/types';
import { StormJSON } from '@stryke/json/storm-json';
import { joinPaths } from '@stryke/path/join-paths';
import { createDirectory } from '@stryke/fs/helpers';
import { existsSync } from '@stryke/path/exists';
import { relativePath } from '@stryke/path/file-path-fns';

async function prepareDirectories(log, context, hooks) {
  log(LogLevelLabel.TRACE, `Preparing the artifact directories for the Storm Stack project.`);
  if (!existsSync(joinPaths(context.options.projectRoot, context.artifactsDir))) {
    await createDirectory(joinPaths(context.options.projectRoot, context.artifactsDir));
  }
  if (context.options.projectType === "application") {
    if (!existsSync(joinPaths(context.options.projectRoot, context.artifactsDir, "runtime"))) {
      await createDirectory(joinPaths(context.options.projectRoot, context.artifactsDir, "runtime"));
    }
  }
  if (!existsSync(joinPaths(context.options.projectRoot, context.artifactsDir, "types"))) {
    await createDirectory(joinPaths(context.options.projectRoot, context.artifactsDir, "types"));
  }
  if (!existsSync(joinPaths(context.options.projectRoot, context.artifactsDir, "reflections"))) {
    await createDirectory(joinPaths(context.options.projectRoot, context.artifactsDir, "reflections"));
  }
  await hooks.callHook("prepare:directories", context).catch((error) => {
    log(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
    });
  });
}
__name(prepareDirectories, "prepareDirectories");
async function prepareReflections(log, context, hooks) {
  log(LogLevelLabel.TRACE, `Preparing the reflection artifacts for the Storm Stack project.`);
  await writeDotenvReflection(log, context, context.dotenv.types.variables.reflection, "variables");
  if (context.dotenv.types.secrets?.reflection) {
    await writeDotenvReflection(log, context, context.dotenv.types.secrets?.reflection, "secrets");
  }
  await hooks.callHook("prepare:reflections", context).catch((error) => {
    log(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
    });
  });
}
__name(prepareReflections, "prepareReflections");
async function prepareRuntime(log, context, hooks) {
  log(LogLevelLabel.TRACE, `Preparing the runtime artifacts for the Storm Stack project.`);
  const runtimeDir = joinPaths(context.options.projectRoot, context.artifactsDir, "runtime");
  const promises = [
    writeFile(log, joinPaths(runtimeDir, "request.ts"), writeRequest()),
    writeFile(log, joinPaths(runtimeDir, "response.ts"), writeResponse()),
    writeFile(log, joinPaths(runtimeDir, "error.ts"), writeError()),
    writeFile(log, joinPaths(runtimeDir, "id.ts"), writeId()),
    writeFile(log, joinPaths(runtimeDir, "log.ts"), writeLog(context), true),
    writeFile(log, joinPaths(runtimeDir, "storage.ts"), writeStorage(context)),
    writeFile(log, joinPaths(runtimeDir, "init.ts"), writeInit(context))
  ];
  if (context.options.platform === "node") {
    promises.push(writeFile(log, joinPaths(runtimeDir, "app.ts"), writeApp(context)));
    promises.push(writeFile(log, joinPaths(runtimeDir, "context.ts"), writeContext(context)));
    promises.push(writeFile(log, joinPaths(runtimeDir, "event.ts"), writeEvent(context)));
  }
  await Promise.all(promises);
  await hooks.callHook("prepare:runtime", context).catch((error) => {
    log(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
    });
  });
}
__name(prepareRuntime, "prepareRuntime");
async function prepareTypes(log, context, hooks) {
  log(LogLevelLabel.TRACE, `Preparing the type declarations for the Storm Stack project.`);
  const typesDir = joinPaths(context.options.projectRoot, context.artifactsDir, "types");
  const relativeRuntimePath = relativePath(joinPaths(context.workspaceConfig.workspaceRoot, context.options.projectRoot, "types"), joinPaths(context.workspaceConfig.workspaceRoot, context.options.projectRoot, "runtime"));
  const promises = [];
  if (context.dotenv.types.variables.reflection) {
    if (context.options.platform === "node") {
      promises.push(writeFile(log, joinPaths(typesDir, "context.d.ts"), generateNodeDeclarations(context.dotenv.types.variables, context)));
    } else {
      promises.push(writeFile(log, joinPaths(typesDir, "vars.d.ts"), generateSharedDeclarations(context.dotenv.types.variables)));
    }
  }
  promises.push(writeFile(log, joinPaths(typesDir, "global.d.ts"), generateSharedGlobal(relativeRuntimePath)));
  promises.push(writeFile(log, joinPaths(typesDir, "modules.d.ts"), generateSharedModules(relativeRuntimePath)));
  if (context.options.platform === "node") {
    promises.push(writeFile(log, joinPaths(typesDir, "global-node.d.ts"), generateNodeGlobal(relativeRuntimePath, context)));
    promises.push(writeFile(log, joinPaths(typesDir, "modules-node.d.ts"), generateNodeModules(relativeRuntimePath, context)));
  }
  await Promise.all(promises);
  await hooks.callHook("prepare:types", context).catch((error) => {
    log(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
    });
  });
}
__name(prepareTypes, "prepareTypes");

// src/prepare/index.ts
async function prepare(log, context, hooks) {
  await writeFile(log, joinPaths(context.options.projectRoot, context.artifactsDir, "meta.json"), StormJSON.stringify(context.meta));
  context.persistedMeta = context.meta;
  await hooks.callHook("prepare:begin", context).catch((error) => {
    log(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(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(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(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 getParsedTypeScriptConfig(context);
  if (!context.tsconfig) {
    throw new Error("Failed to parse the TypeScript configuration file.");
  }
  await hooks.callHook("prepare:complete", context).catch((error) => {
    log(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
    });
  });
}
__name(prepare, "prepare");

export { prepare };
//# sourceMappingURL=chunk-YTC7QPYX.js.map
//# sourceMappingURL=chunk-YTC7QPYX.js.map