import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
import {
  MycoConfigSchema,
  require_dist
} from "./chunk-JSK7L46L.js";
import {
  __toESM
} from "./chunk-PZUWP5VK.js";

// src/config/loader.ts
var import_yaml = __toESM(require_dist(), 1);
import fs2 from "fs";
import path2 from "path";

// src/config/migrations.ts
import fs from "fs";
import path from "path";
var MEMORY_TYPE_PATTERN = /type:\s*["']?memory["']?/g;
var MIGRATIONS = [
  {
    version: 1,
    name: "rename-memories-to-spores",
    migrate: (doc, vaultDir) => {
      const context = doc.context;
      const layers = context?.layers;
      if (layers && "memories" in layers && !("spores" in layers)) {
        layers.spores = layers.memories;
        delete layers.memories;
      }
      const memoriesDir = path.join(vaultDir, "memories");
      const sporesDir = path.join(vaultDir, "spores");
      if (!fs.existsSync(memoriesDir)) return;
      if (fs.existsSync(sporesDir)) {
        const moveRemaining = (srcDir, destDir) => {
          for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) {
            const srcPath = path.join(srcDir, entry.name);
            const destPath = path.join(destDir, entry.name);
            if (entry.isDirectory()) {
              if (!fs.existsSync(destPath)) fs.mkdirSync(destPath, { recursive: true });
              moveRemaining(srcPath, destPath);
            } else if (!fs.existsSync(destPath)) {
              fs.renameSync(srcPath, destPath);
            }
          }
        };
        moveRemaining(memoriesDir, sporesDir);
        fs.rmSync(memoriesDir, { recursive: true, force: true });
      } else {
        fs.renameSync(memoriesDir, sporesDir);
      }
      const walkUpdate = (dir) => {
        for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
          const fullPath = path.join(dir, entry.name);
          if (entry.isDirectory()) {
            walkUpdate(fullPath);
            continue;
          }
          if (!entry.name.endsWith(".md")) continue;
          const content = fs.readFileSync(fullPath, "utf-8");
          MEMORY_TYPE_PATTERN.lastIndex = 0;
          if (MEMORY_TYPE_PATTERN.test(content)) {
            MEMORY_TYPE_PATTERN.lastIndex = 0;
            fs.writeFileSync(fullPath, content.replace(MEMORY_TYPE_PATTERN, "type: spore"));
          }
        }
      };
      walkUpdate(sporesDir);
      const walkLinks = (dir) => {
        for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
          const fullPath = path.join(dir, entry.name);
          if (entry.isDirectory()) {
            walkLinks(fullPath);
            continue;
          }
          if (!entry.name.endsWith(".md")) continue;
          const content = fs.readFileSync(fullPath, "utf-8");
          if (content.includes("memories/")) {
            fs.writeFileSync(fullPath, content.replace(/memories\//g, "spores/"));
          }
        }
      };
      walkLinks(vaultDir);
    }
  },
  {
    version: 2,
    name: "consolidation-boolean-to-object",
    migrate: (doc) => {
      const digest = doc.digest;
      if (!digest) return;
      const consolidation = digest.consolidation;
      if (typeof consolidation === "boolean") {
        digest.consolidation = { enabled: consolidation, max_tokens: 2048 };
      }
    }
  }
];
var CURRENT_MIGRATION_VERSION = MIGRATIONS[MIGRATIONS.length - 1]?.version ?? 0;
function runMigrations(doc, vaultDir, log) {
  const currentVersion = doc.config_version ?? 0;
  let ran = false;
  for (const migration of MIGRATIONS) {
    if (migration.version <= currentVersion) continue;
    migration.migrate(doc, vaultDir);
    doc.config_version = migration.version;
    ran = true;
  }
  if (ran) {
    const from = currentVersion;
    const to = doc.config_version ?? 0;
    log?.(`Migrated config from v${from} to v${to}`);
  }
  return ran;
}

// src/config/loader.ts
var CONFIG_FILENAME = "myco.yaml";
function loadConfig(vaultDir) {
  const configPath = path2.join(vaultDir, CONFIG_FILENAME);
  if (!fs2.existsSync(configPath)) {
    throw new Error(`myco.yaml not found in ${vaultDir}`);
  }
  const raw = fs2.readFileSync(configPath, "utf-8");
  const parsed = import_yaml.default.parse(raw);
  if (parsed.version === 1 || parsed.intelligence?.backend) {
    throw new Error(
      "Myco config uses v1 format. Run /myco:setup-llm to reconfigure for v2."
    );
  }
  const intel = parsed.intelligence;
  const llm = intel?.llm;
  if (llm?.provider === "haiku") {
    llm.provider = "anthropic";
  }
  const migrationsRan = runMigrations(parsed, vaultDir, (msg) => {
    process.stderr.write(`[myco migration] ${msg}
`);
  });
  const config = MycoConfigSchema.parse(parsed);
  const needsWrite = migrationsRan || (parsed.config_version ?? 0) < CURRENT_MIGRATION_VERSION || !("digest" in parsed);
  if (needsWrite) {
    const fullConfig = JSON.parse(JSON.stringify(config));
    fs2.writeFileSync(configPath, import_yaml.default.stringify(fullConfig), "utf-8");
  }
  return config;
}
function saveConfig(vaultDir, config) {
  const validated = MycoConfigSchema.parse(config);
  const configPath = path2.join(vaultDir, CONFIG_FILENAME);
  fs2.mkdirSync(vaultDir, { recursive: true });
  fs2.writeFileSync(configPath, import_yaml.default.stringify(validated), "utf-8");
}

export {
  CONFIG_FILENAME,
  loadConfig,
  saveConfig
};
//# sourceMappingURL=chunk-YG6MLLGL.js.map