import {
  getConfigFile
} from "./chunk-6UWJE5DT.js";
import {
  defu
} from "./chunk-OL4KMEX5.js";
import {
  formatLogMessage,
  writeTrace,
  writeWarning
} from "./chunk-4RQGNE37.js";
import {
  getDefaultConfig
} from "./chunk-EGAP6KEW.js";
import {
  findWorkspaceRoot
} from "./chunk-BDW32BZK.js";
import {
  getConfigEnv,
  getExtensionEnv
} from "./chunk-RFXZQQFC.js";
import {
  StormConfigSchema
} from "./chunk-2CJNMJST.js";
import {
  setConfigEnv
} from "./chunk-E6R4LOUF.js";
import {
  __name,
  init_esm_shims
} from "./chunk-BFNZYC5O.js";

// src/create-storm-config.ts
init_esm_shims();
var _extension_cache = /* @__PURE__ */ new WeakMap();
var _static_cache = void 0;
var createConfig = /* @__PURE__ */ __name((workspaceRoot) => {
  return createStormConfig(void 0, void 0, workspaceRoot);
}, "createConfig");
var createStormConfig = /* @__PURE__ */ __name((extensionName, schema, workspaceRoot) => {
  let result;
  if (!_static_cache?.data || !_static_cache?.timestamp || _static_cache.timestamp < Date.now() - 3e4) {
    const config = getConfigEnv();
    const defaultConfig = getDefaultConfig(workspaceRoot);
    result = StormConfigSchema.parse(defu(config, defaultConfig));
    result.workspaceRoot ??= defaultConfig.workspaceRoot || findWorkspaceRoot(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) => {
  let config = {};
  if (_static_cache?.data && _static_cache?.timestamp && _static_cache.timestamp >= Date.now() + 3e4) {
    writeTrace(`Configuration cache hit - ${_static_cache.timestamp}`, _static_cache.data);
    return _static_cache.data;
  }
  let _workspaceRoot = workspaceRoot;
  if (!_workspaceRoot) {
    _workspaceRoot = findWorkspaceRoot();
  }
  const configFile = await getConfigFile(_workspaceRoot);
  if (!configFile) {
    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"
    });
  }
  config = defu(getConfigEnv(), configFile, getDefaultConfig(_workspaceRoot));
  setConfigEnv(config);
  writeTrace(`\u2699\uFE0F  Using Storm configuration: 
${formatLogMessage(config)}`, config);
  return config;
}, "loadStormConfig");

export {
  createConfig,
  createStormConfig,
  createConfigExtension,
  loadStormConfig
};
