{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "diff-review",
  "title": "Diff review",
  "description": "What a person or an agent decided about each change of a diff: Accept and Reject for one change, Accept all and Reject all for the rest, through onResolvedChange with the press's details.",
  "dependencies": [
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/diff-math.json"
  ],
  "files": [
    {
      "path": "registry/lib/diff-review.ts",
      "content": "import type { Resolution, Resolved } from '@/registry/lib/diff-math'\nimport { type ChangeDetails, createChange, definePrimitive } from '@ui-sdk/core'\n\n/*\n * What a person decided about each change of a diff: the Diff's primitive.\n * Accept and Reject decide one change, Accept all and Reject all every\n * change still open. A decision reaches `onResolvedChange` with who made it\n * in `details.source` and the changes it decided in `details.ids`, so an app\n * can refuse one with `details.cancel()`. An agent decides through the same\n * actions.\n */\n\n/** Why changes were decided: a press on a change's own button or on Accept all or Reject all, or code and agents (`imperative`). */\ntype DiffReviewReason = 'accept-press' | 'reject-press' | 'all-press' | 'imperative'\n\n/** What a decision reports, beside a change's usual details. */\ntype DiffReviewDetails = ChangeDetails<DiffReviewReason> & {\n  /** The changes this decision decided. */\n  ids: readonly string[]\n  resolution: Resolution\n}\n\ntype DiffReviewOptions = {\n  resolved?: Resolved\n  defaultResolved?: Resolved\n  onResolvedChange?: (resolved: Resolved, details: DiffReviewDetails) => void\n  /** Every change's id, in order: what Accept all and Reject all decide. */\n  changes?: readonly string[]\n  /** What a person and an agent hear each change called as the diff reads with `resolved`, by id, such as \"change at line 8\". */\n  namesOf?: (resolved: Resolved) => ReadonlyMap<string, string>\n  /** The changes after may still rewrite while it arrives, which wait to be decided. */\n  growing?: readonly string[]\n  /** Whether after has all arrived. Until then Accept all and Reject all wait, since more changes may come. */\n  complete?: boolean\n  /** Shows the changes with nothing to decide. */\n  readOnly?: boolean\n}\n\nconst NONE: Resolved = {}\nconst NO_IDS: readonly string[] = []\n\nconst diffReview = definePrimitive({\n  name: 'diff',\n  options: {\n    resolved: 'value',\n    defaultResolved: 'value',\n    onResolvedChange: 'callback',\n    changes: 'list',\n    namesOf: 'callback',\n    growing: 'list',\n    complete: 'boolean',\n    readOnly: 'boolean',\n  },\n  controlled: ['resolved'],\n  state: (options: DiffReviewOptions) => ({\n    resolved: options.resolved ?? options.defaultResolved ?? NONE,\n    changes: options.changes ?? NO_IDS,\n    growing: options.growing ?? NO_IDS,\n    complete: options.complete ?? true,\n    readOnly: !!options.readOnly,\n  }),\n  sync: (options) => ({\n    ...(options.resolved === undefined ? {} : { resolved: options.resolved }),\n    changes: options.changes ?? NO_IDS,\n    growing: options.growing ?? NO_IDS,\n    complete: options.complete ?? true,\n    readOnly: !!options.readOnly,\n  }),\n  api: (context) => {\n    const { store } = context\n    const change = createChange(context)\n    /** Whether change `id` can be decided now: it is one of the diff's, and after won't rewrite it. */\n    const decidable = (id: string) => {\n      const { changes, growing, readOnly } = store.get()\n      return !readOnly && !growing.includes(id) && changes.includes(id)\n    }\n    const decide = (\n      ids: readonly string[],\n      resolution: Resolution,\n      reason: DiffReviewReason,\n      event?: Event,\n    ) => {\n      const { resolved } = store.get()\n      const open = ids.filter((id) => decidable(id) && resolved[id] !== resolution)\n      if (open.length === 0) return false\n      const next = { ...resolved }\n      for (const id of open) next[id] = resolution\n      return change('resolved', next, { reason, event, ids: open, resolution })\n    }\n    /** The changes not decided yet, by id. */\n    const open = () => {\n      const { changes, resolved } = store.get()\n      return changes.filter((id) => resolved[id] === undefined)\n    }\n    /** Whether Accept all and Reject all can decide now: after has all arrived and a change is open. */\n    const canDecideAll = () => {\n      const { complete, readOnly } = store.get()\n      return complete && !readOnly && open().length > 0\n    }\n    const all = (resolution: Resolution, event?: Event) =>\n      canDecideAll() &&\n      decide(open(), resolution, event ? 'all-press' : 'imperative', event)\n    /** The changes an agent can decide now, by id and name: one after may still rewrite is left out until it can't. */\n    const items = () => {\n      const { changes, resolved } = store.get()\n      const names = context.options().namesOf?.(resolved)\n      return changes\n        .filter(decidable)\n        .map((id) => ({ value: id, label: names?.get(id) ?? id }))\n    }\n    return {\n      open,\n      items,\n      decidable,\n      canDecideAll,\n      /** Takes change `id`'s after. */\n      accept: (id: string, event?: Event) =>\n        decide([id], 'accepted', event ? 'accept-press' : 'imperative', event),\n      /** Keeps change `id`'s before. */\n      reject: (id: string, event?: Event) =>\n        decide([id], 'rejected', event ? 'reject-press' : 'imperative', event),\n      /** Takes every open change's after, once after has all arrived. */\n      acceptAll: (event?: Event) => all('accepted', event),\n      /** Keeps every open change's before, once after has all arrived. */\n      rejectAll: (event?: Event) => all('rejected', event),\n    }\n  },\n  slots: (slot) => ({\n    root: slot<Record<never, never>, { resolved: Resolved; readOnly: boolean }>({\n      state: (state) => ({ resolved: state.resolved, readOnly: state.readOnly }),\n      attrs: (state) => ({\n        role: 'group',\n        'data-read-only': state.readOnly ? '' : undefined,\n      }),\n    }),\n  }),\n  agent: {\n    actions: ['accept', 'reject', 'acceptAll', 'rejectAll'],\n    // The changes `accept` and `reject` take, by name, so an agent never guesses an id.\n    items: (api) => api.items(),\n    closed: true,\n  },\n})\n\nexport { diffReview }\nexport type { DiffReviewDetails, DiffReviewOptions, DiffReviewReason }\n",
      "type": "registry:lib",
      "target": "@lib/diff-review.ts"
    }
  ],
  "type": "registry:lib"
}