import microdiff from 'microdiff';

/** Human-readable microdiff paths for viewer (purely client-side, per policy). */
export function describeJsonMicrodiff(before: unknown, after: unknown): string[] {
  const lhs =
    before !== null && typeof before === 'object' && !Array.isArray(before)
      ? (before as Record<string, unknown>)
      : before === null || before === undefined
        ? {}
        : { value: before };
  const rhs =
    after !== null && typeof after === 'object' && !Array.isArray(after)
      ? (after as Record<string, unknown>)
      : after === null || after === undefined
        ? {}
        : { value: after };

  const changes = microdiff(lhs, rhs);
  return changes.map((c) => {
    const path = c.path.join('.');
    switch (c.type) {
      case 'add':
        return `+ ${path}: ${JSON.stringify(c.value)}`;
      case 'remove':
        return `- ${path}`;
      case 'change':
        return `~ ${path}: ${JSON.stringify(c.oldValue)} → ${JSON.stringify(c.value)}`;
      default:
        return String(path);
    }
  });
}
