'use strict';

var fs = require('fs');

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

var fs__default = /*#__PURE__*/_interopDefault(fs);

// src/features/token-optimizer/infrastructure/config.ts
var DEFAULT_TOKEN_OPTIMIZER_STATE = {
  enabled: false
};
var tokenOptimizerRuntimeState = globalThis.__JU_HONG_E_TOKEN_OPTIMIZER_STATE__ ?? (globalThis.__JU_HONG_E_TOKEN_OPTIMIZER_STATE__ = {
  ...DEFAULT_TOKEN_OPTIMIZER_STATE
});
function normalizeTokenOptimizerState(value) {
  if (!value || typeof value !== "object") {
    return { ...DEFAULT_TOKEN_OPTIMIZER_STATE };
  }
  const enabled = "enabled" in value ? Boolean(value.enabled) : DEFAULT_TOKEN_OPTIMIZER_STATE.enabled;
  return {
    enabled
  };
}
function readTokenOptimizerConfig(paths) {
  if (!fs__default.default.existsSync(paths.tokenOptimizerConfigPath)) {
    return { ...DEFAULT_TOKEN_OPTIMIZER_STATE };
  }
  try {
    const parsed = JSON.parse(fs__default.default.readFileSync(paths.tokenOptimizerConfigPath, "utf8"));
    return normalizeTokenOptimizerState(parsed);
  } catch {
    return { ...DEFAULT_TOKEN_OPTIMIZER_STATE };
  }
}
function hydrateTokenOptimizerState(paths) {
  const nextState = readTokenOptimizerConfig(paths);
  tokenOptimizerRuntimeState.enabled = nextState.enabled;
  return tokenOptimizerRuntimeState.enabled;
}
function isTokenOptimizerEnabled(paths) {
  if (paths) {
    return hydrateTokenOptimizerState(paths);
  }
  return tokenOptimizerRuntimeState.enabled;
}
function saveTokenOptimizerConfig(paths, enabled) {
  const nextState = {
    enabled: Boolean(enabled)
  };
  fs__default.default.writeFileSync(paths.tokenOptimizerConfigPath, `${JSON.stringify(nextState, null, 2)}
`, "utf8");
  tokenOptimizerRuntimeState.enabled = nextState.enabled;
  return nextState;
}

exports.hydrateTokenOptimizerState = hydrateTokenOptimizerState;
exports.isTokenOptimizerEnabled = isTokenOptimizerEnabled;
exports.readTokenOptimizerConfig = readTokenOptimizerConfig;
exports.saveTokenOptimizerConfig = saveTokenOptimizerConfig;
