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

// src/cli/config.ts
var import_yaml = __toESM(require_dist(), 1);
import fs from "fs";
import path from "path";
var CONFIG_FILENAME = "myco.yaml";
var DAEMON_STATE_FILENAME = "daemon.json";
async function run(args, vaultDir) {
  const [subcommand, key, ...rest] = args;
  if (subcommand === "get") {
    if (!key) {
      console.error("Usage: myco config get <dot.path.key>");
      process.exit(1);
    }
    return configGet(key, vaultDir);
  }
  if (subcommand === "set") {
    const value = rest[0];
    if (!key || value === void 0) {
      console.error("Usage: myco config set <dot.path.key> <value>");
      process.exit(1);
    }
    return configSet(key, value, vaultDir);
  }
  console.error("Usage: myco config <get|set> <dot.path.key> [value]");
  process.exit(1);
}
function configGet(dotPath, vaultDir) {
  const config = loadConfig(vaultDir);
  const value = walkPath(config, dotPath);
  if (value === void 0) {
    console.error(`Key not found: ${dotPath}`);
    process.exit(1);
  }
  console.log(typeof value === "object" ? JSON.stringify(value, null, 2) : String(value));
}
function configSet(dotPath, rawValue, vaultDir) {
  const configPath = path.join(vaultDir, CONFIG_FILENAME);
  const raw = fs.readFileSync(configPath, "utf-8");
  const doc = import_yaml.default.parse(raw);
  const value = parseValue(rawValue);
  setPath(doc, dotPath, value);
  const result = MycoConfigSchema.safeParse(doc);
  if (!result.success) {
    console.error("Validation error:");
    for (const issue of result.error.issues) {
      console.error(`  ${issue.path.join(".")}: ${issue.message}`);
    }
    process.exit(1);
  }
  fs.writeFileSync(configPath, import_yaml.default.stringify(doc), "utf-8");
  console.log(`Set ${dotPath} = ${JSON.stringify(value)}`);
  if (fs.existsSync(path.join(vaultDir, DAEMON_STATE_FILENAME))) {
    console.log("Note: restart the daemon for changes to take effect (myco restart)");
  }
}
function walkPath(obj, dotPath) {
  const segments = dotPath.split(".");
  let current = obj;
  for (const segment of segments) {
    if (current === null || current === void 0 || typeof current !== "object") {
      return void 0;
    }
    current = current[segment];
  }
  return current;
}
function setPath(obj, dotPath, value) {
  const segments = dotPath.split(".");
  let current = obj;
  for (let i = 0; i < segments.length - 1; i++) {
    const segment = segments[i];
    if (current[segment] === void 0 || current[segment] === null || typeof current[segment] !== "object") {
      current[segment] = {};
    }
    current = current[segment];
  }
  current[segments[segments.length - 1]] = value;
}
function parseValue(raw) {
  try {
    return JSON.parse(raw);
  } catch {
    return raw;
  }
}
export {
  run
};
//# sourceMappingURL=config-HDUFDOQN.js.map