import { applyConfigs } from "../config-writer.js";
import { writePersistedConfig, readPersistedConfig, defaultConfigPath } from "../../../persisted-config.js";
function toWriteResult(r) {
  if (r.ok) {
    return {
      id: r.id,
      label: r.displayName,
      status: r.alreadyInstalled ? "already_installed" : "ok",
      path: r.configPath ?? void 0
    };
  }
  return {
    id: r.id,
    label: r.displayName,
    status: "failed",
    path: r.configPath ?? void 0,
    error: r.message ?? r.code
  };
}
async function writeMcpConfig(detected, selected, opts = {}) {
  const raw = await applyConfigs(detected, selected, { dryRun: opts.dryRun });
  const results = raw.map(toWriteResult);
  const anyFailed = results.some((r) => r.status === "failed");
  return { results, anyFailed };
}
function deepSet(obj, path, value) {
  const segments = path.split(".");
  if (segments.length === 0 || segments.length === 1 && segments[0] === "") return obj;
  let cursor = obj;
  for (let i = 0; i < segments.length - 1; i++) {
    const seg = segments[i];
    const child = cursor[seg];
    if (typeof child !== "object" || child === null || Array.isArray(child)) {
      cursor[seg] = {};
    }
    cursor = cursor[seg];
  }
  cursor[segments[segments.length - 1]] = value;
  return obj;
}
async function persistKey(path, value) {
  if (!path) throw new Error("persistKey: path must be non-empty");
  const configPath = defaultConfigPath();
  const current = readPersistedConfig(configPath);
  const settings = deepSet({ ...current.settings }, path, value);
  return await Promise.resolve(writePersistedConfig(configPath, { settings }));
}
export {
  persistKey,
  writeMcpConfig
};
//# sourceMappingURL=write-config.js.map