/**
 * lib/json-merge.ts — Deep-merge JSON for skill CLIs.
 *
 * Promoted from the inlined copy in
 * `development/frontend/ui-primitives/cli/scaffold-ui-primitives/index.ts`
 * so multiple CLIs (ui-primitives, login-config, …) share ONE implementation.
 *
 * Semantics: objects merge key-by-key (recursively); arrays and primitives in
 * `patch` REPLACE those in `base`. Used so a CLI can write only the keys it owns
 * into an existing config file (e.g. appsettings.json, a locale JSON) without
 * clobbering sibling sections.
 */
export function deepMerge(base: unknown, patch: unknown): unknown {
  if (
    base !== null && typeof base === 'object' && !Array.isArray(base) &&
    patch !== null && typeof patch === 'object' && !Array.isArray(patch)
  ) {
    const out: Record<string, unknown> = { ...(base as Record<string, unknown>) };
    for (const [k, v] of Object.entries(patch as Record<string, unknown>)) {
      out[k] = deepMerge(out[k], v);
    }
    return out;
  }
  return patch;
}
