import {
  getConfigFile
} from "./chunk-YDVJJTKY.js";
import {
  defu
} from "./chunk-7LNAPF6J.js";
import {
  formatLogMessage,
  writeTrace,
  writeWarning
} from "./chunk-LHYMHJN7.js";
import {
  getDefaultConfig
} from "./chunk-NKL6DBVO.js";
import {
  findWorkspaceRoot
} from "./chunk-2F7SMIKE.js";
import {
  setConfigEnv
} from "./chunk-RXWR65M2.js";
import {
  getConfigEnv,
  getExtensionEnv
} from "./chunk-M3QWTBKA.js";
import {
  __name,
  init_esm_shims
} from "./chunk-65NJ2BNV.js";

// src/create-storm-config.ts
init_esm_shims();
import { StormConfigSchema } from "@storm-software/config/schema";
var _extension_cache = /* @__PURE__ */ new WeakMap();
var _static_cache = void 0;
var createStormConfig = /* @__PURE__ */ __name(async (extensionName, schema, workspaceRoot, skipLogs = false) => {
  let result;
  if (!_static_cache?.data || !_static_cache?.timestamp || _static_cache.timestamp < Date.now() - 8e3) {
    let _workspaceRoot = workspaceRoot;
    if (!_workspaceRoot) {
      _workspaceRoot = findWorkspaceRoot();
    }
    const configEnv = getConfigEnv();
    const defaultConfig = await getDefaultConfig(_workspaceRoot);
    const configFile = await getConfigFile(_workspaceRoot);
    if (!configFile && !skipLogs) {
      writeWarning("No Storm config file found in the current workspace. Please ensure this is the expected behavior - you can add a `storm.json` file to the root of your workspace if it is not.\n", {
        logLevel: "all"
      });
    }
    result = await StormConfigSchema.parseAsync(defu(configEnv, configFile, defaultConfig));
    result.workspaceRoot ??= _workspaceRoot;
  } else {
    result = _static_cache.data;
  }
  if (schema && extensionName) {
    result.extensions = {
      ...result.extensions,
      [extensionName]: createConfigExtension(extensionName, schema)
    };
  }
  _static_cache = {
    timestamp: Date.now(),
    data: result
  };
  return result;
}, "createStormConfig");
var createConfigExtension = /* @__PURE__ */ __name((extensionName, schema) => {
  const extension_cache_key = {
    extensionName
  };
  if (_extension_cache.has(extension_cache_key)) {
    return _extension_cache.get(extension_cache_key);
  }
  let extension = getExtensionEnv(extensionName);
  if (schema) {
    extension = schema.parse(extension);
  }
  _extension_cache.set(extension_cache_key, extension);
  return extension;
}, "createConfigExtension");
var loadStormConfig = /* @__PURE__ */ __name(async (workspaceRoot, skipLogs = false) => {
  const config = await createStormConfig(void 0, void 0, workspaceRoot, skipLogs);
  setConfigEnv(config);
  if (!skipLogs) {
    writeTrace(`\u2699\uFE0F  Using Storm configuration: 
${formatLogMessage(config)}`, config);
  }
  return config;
}, "loadStormConfig");

export {
  createStormConfig,
  createConfigExtension,
  loadStormConfig
};
