import { SettingsManager } from "@earendil-works/pi-coding-agent"; import { type LazyMode, normalizeMode } from "./modes.ts"; export interface LazySettings { defaultMode: LazyMode; status: boolean; } export const LAZY_DEFAULTS: LazySettings = { defaultMode: "full", status: true, }; function isPlainObject(value: unknown): value is Record { return value !== null && typeof value === "object" && !Array.isArray(value); } export function pickLazy(raw: unknown): Partial { if (!isPlainObject(raw) || !isPlainObject(raw.lazy)) return {}; const out: Partial = {}; const defaultMode = normalizeMode(raw.lazy.defaultMode); if (defaultMode) out.defaultMode = defaultMode; if (typeof raw.lazy.status === "boolean") out.status = raw.lazy.status; return out; } export function mergeLazySettings( ...partials: Partial[] ): LazySettings { return partials.reduce( (settings, partial) => ({ defaultMode: partial.defaultMode ?? settings.defaultMode, status: typeof partial.status === "boolean" ? partial.status : settings.status, }), { ...LAZY_DEFAULTS }, ); } export function loadLazySettings(runtime?: unknown): LazySettings { return mergeLazySettings(pickLazy(runtime)); } export function loadLazySettingsForCwd(cwd: string): LazySettings { const manager = SettingsManager.create(cwd); return mergeLazySettings( pickLazy(manager.getGlobalSettings()), pickLazy(manager.getProjectSettings()), ); } export function settingsSnippet(mode: LazyMode): string { return JSON.stringify( { lazy: { defaultMode: mode, status: true } }, null, "\t", ); }