{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "allergy-chip",
  "type": "zoblocks:component",
  "title": "Allergy Chip",
  "description": "Substance first at full weight, criticality as the primary signal, the worst past reaction as secondary text, and verification as a hairline affix. AllergyList carries the other half: a no-known-allergies assertion and an unasked question look nothing alike, and an assertion missing its author degrades to the second.",
  "categories": [
    "clinical",
    "data-display"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "allergy-core",
    "clinical-status"
  ],
  "files": [
    {
      "path": "registry/zoblocks/allergy-chip/allergy-chip.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/allergy-chip.tsx",
      "content": "/**\n * AllergyChip — the chip that refuses to conflate how bad the last reaction\n * was with how bad the next one could be.\n *\n *     <AllergyChip record={penicillin} />\n *     <AllergyList records={records} noneKnown={assertion} />\n *\n * Substance first and at full weight. Criticality — future risk — as the\n * primary signal. The worst past reaction as secondary text, never as the\n * headline, because leading with it is the mistake the component exists to\n * correct: a patient whose only documented reaction was mild urticaria can\n * still be high criticality, and that is the whole reason the field exists.\n *\n * `AllergyList` carries the other half. \"No known allergies\" is a positive\n * clinical finding with an author and a date; \"nobody asked\" is an absence.\n * They look nothing alike here, and an assertion missing its author degrades\n * to the second — because an empty list beside a prescribing button is a claim\n * the software has not earned.\n *\n * Styling lives in `styles/zoblocks-allergy.css`, installed alongside.\n */\n\nimport * as React from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { ClinicalStatus } from \"@/components/zoblocks/clinical-status\";\nimport {\n  KIND_LABEL,\n  NOT_ASKED_SENTENCE,\n  SEVERITY_LABEL,\n  VERIFICATION_LABEL,\n  describeAllergy,\n  describeNoKnown,\n  isActive,\n  resolveListState,\n  worstReaction,\n  type AllergyRecord,\n  type ClassExpander,\n  type Criticality,\n  type NoKnownAllergies,\n} from \"@/lib/zoblocks-allergy\";\n\nexport {\n  CRITICALITY_LABEL,\n  INACTIVE_VERIFICATIONS,\n  KIND_LABEL,\n  NOT_ASKED_SENTENCE,\n  NO_KNOWN_ALLERGY_CODES,\n  SEVERITY_LABEL,\n  VERIFICATION_LABEL,\n  describeAllergy,\n  describeNoKnown,\n  fromAllergyIntolerance,\n  isActive,\n  noKnownFromFHIR,\n  resolveListState,\n  toKind,\n  toVerification,\n  worstReaction,\n  type AllergyKind,\n  type AllergyListState,\n  type AllergyRecord,\n  type ClassExpander,\n  type Criticality,\n  type NoKnownAllergies,\n  type Reaction,\n  type ReactionSeverity,\n  type Verification,\n} from \"@/lib/zoblocks-allergy\";\n\n/** Criticality maps onto the shared vocabulary so the shape agrees library-wide. */\nconst CRITICALITY_STEP: Record<Criticality, string> = {\n  high: \"critical\",\n  low: \"moderate\",\n  \"unable-to-assess\": \"not-assessed\",\n};\n\nexport interface AllergyChipProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  /**\n   * The AllergyIntolerance, already adapted. Carries `criticality` and `reaction.severity`\n   * separately, because they mean opposite things and merging them is the defect this\n   * component exists to prevent.\n   */\n  record: AllergyRecord;\n  /** Row height and type scale. Inherited from the nearest density provider when omitted. */\n  density?: \"compact\" | \"default\";\n  /** Expands a substance into the class it implicates. Injected, never bundled. */\n  expandClass?: ClassExpander;\n  /** Opens the reaction history, asserter and source. */\n  onOpenDetail?: (record: AllergyRecord) => void;\n}\n\nexport const AllergyChip = React.forwardRef<HTMLElement, AllergyChipProps>(function AllergyChip(\n  { record, density = \"default\", expandClass, onOpenDetail, className, ...rest },\n  ref,\n) {\n  const worst = worstReaction(record);\n  const expansion = expandClass?.(record.substance) ?? null;\n  const active = isActive(record.verification);\n\n  const shell = {\n    className: cn(\"zb-allergy\", className),\n    \"data-zb-allergy\": \"\",\n    \"data-zb-kind\": record.kind,\n    \"data-zb-criticality\": record.criticality ?? undefined,\n    \"data-zb-verification\": record.verification ?? undefined,\n    \"data-zb-density\": density,\n    // Refuted and entered-in-error are not warnings any more, and the record\n    // has to keep saying so rather than being deleted — a refuted allergy that\n    // vanishes gets re-reported at the next intake.\n    \"data-zb-inactive\": active ? undefined : \"\",\n    \"aria-label\": describeAllergy(record),\n    ...rest,\n  };\n\n  const content = (\n    <>\n      <span className=\"zb-allergy__head\" aria-hidden=\"true\">\n        <span className=\"zb-allergy__substance\">{record.substance}</span>\n        {record.kind !== \"allergy\" ? (\n          <span className=\"zb-allergy__kind\">{KIND_LABEL[record.kind]}</span>\n        ) : null}\n        {expansion ? (\n          <span className=\"zb-allergy__class\">\n            Class: {expansion.label} · {expansion.count} members\n          </span>\n        ) : null}\n      </span>\n\n      <span className=\"zb-allergy__signals\" aria-hidden=\"true\">\n        {/*\n          Future risk, as the primary signal and with a shape.\n          The two fields are rendered as different things on purpose: a chip\n          for what could happen, plain text for what did.\n        */}\n        {record.criticality ? (\n          <ClinicalStatus\n            scale=\"criticality\"\n            step={CRITICALITY_STEP[record.criticality]}\n            density=\"compact\"\n            aria-hidden=\"true\"\n          />\n        ) : null}\n\n        {record.verification ? (\n          <span className=\"zb-allergy__verification\">\n            {VERIFICATION_LABEL[record.verification]}\n          </span>\n        ) : null}\n      </span>\n\n      {/*\n        The past, as secondary text. Never the headline.\n        \"Urticaria — mild, 1998\" beside \"High criticality\" reads as a\n        contradiction only until you know the two fields answer different\n        questions, which is why both are always present when both are known.\n      */}\n      <span className=\"zb-allergy__past\" aria-hidden=\"true\">\n        {worst ? (\n          <>\n            <span className=\"zb-allergy__manifestation\">{worst.manifestation}</span>\n            {worst.severity ? (\n              <span className=\"zb-allergy__severity\" data-zb-severity={worst.severity}>\n                {SEVERITY_LABEL[worst.severity]}\n              </span>\n            ) : null}\n            {worst.onset ? <span className=\"zb-allergy__when\">{worst.onset}</span> : null}\n            {worst.note ? <span className=\"zb-allergy__when\">{worst.note}</span> : null}\n          </>\n        ) : record.criticality ? (\n          <span className=\"zb-allergy__manifestation\" data-zb-none=\"\">\n            No reaction recorded\n          </span>\n        ) : null}\n        {record.note ? <span className=\"zb-allergy__when\">{record.note}</span> : null}\n      </span>\n    </>\n  );\n\n  if (onOpenDetail) {\n    return (\n      <button\n        {...(shell as React.ButtonHTMLAttributes<HTMLButtonElement>)}\n        ref={ref as React.Ref<HTMLButtonElement>}\n        type=\"button\"\n        onClick={() => onOpenDetail(record)}\n      >\n        {content}\n      </button>\n    );\n  }\n\n  return (\n    <div\n      {...(shell as React.HTMLAttributes<HTMLDivElement>)}\n      ref={ref as React.Ref<HTMLDivElement>}\n      role=\"group\"\n    >\n      {content}\n    </div>\n  );\n});\n\n/* ------------------------------------------------------------------ */\n/* The list, and its two empty states                                  */\n/* ------------------------------------------------------------------ */\n\nexport interface AllergyListProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  /**\n   * The allergies, in the order they should be read. An empty array and a `null` mean\n   * different things — see `askLabel`.\n   */\n  records?: readonly AllergyRecord[];\n  /**\n   * A no-known-allergies assertion.\n   *\n   * Requires both an asserter and a date. Supplying one without the other\n   * renders not-asked, because an unattributed assertion of absence is not an\n   * assertion — and it is the one that gets prescribed against.\n   */\n  noneKnown?: Partial<NoKnownAllergies>;\n  /** Row height and type scale. Inherited from the nearest density provider when omitted. */\n  density?: \"compact\" | \"default\";\n  expandClass?: ClassExpander;\n  onOpenDetail?: (record: AllergyRecord) => void;\n  /** Rendered inside the not-asked state. \"Ask and record\", typically. */\n  onAsk?: () => void;\n  /**\n   * What to render when nobody has asked. \"No known allergies\" is a clinical assertion\n   * somebody made; an empty list is not, and the two must not look alike.\n   */\n  askLabel?: string;\n}\n\nexport const AllergyList = React.forwardRef<HTMLDivElement, AllergyListProps>(function AllergyList(\n  {\n    records,\n    noneKnown,\n    density = \"default\",\n    expandClass,\n    onOpenDetail,\n    onAsk,\n    askLabel = \"Ask and record\",\n    className,\n    ...rest\n  },\n  ref,\n) {\n  const state = resolveListState({ records: records ?? [], noneKnown: noneKnown ?? {} });\n\n  return (\n    <div\n      {...rest}\n      ref={ref}\n      className={cn(\"zb-allergy-list\", className)}\n      data-zb-allergy-list={state.kind}\n    >\n      {state.kind === \"records\" ? (\n        state.records.map((record) => (\n          <AllergyChip\n            key={record.id}\n            record={record}\n            density={density}\n            {...(expandClass ? { expandClass } : {})}\n            {...(onOpenDetail ? { onOpenDetail } : {})}\n          />\n        ))\n      ) : state.kind === \"none-known\" ? (\n        /*\n         * A positive clinical finding, and it looks like one.\n         *\n         * Green, with an author and a date, because that is what makes it safe\n         * to prescribe against — and the author and date are what separate it\n         * from the state below.\n         */\n        <div className=\"zb-allergy-none\" role=\"group\" aria-label={describeNoKnown(state.assertion)}>\n          <span className=\"zb-allergy-none__head\" aria-hidden=\"true\">\n            <ClinicalStatus scale=\"criticality\" step=\"normal\" shape=\"dot\" density=\"compact\" />\n            No known{state.assertion.scope ? ` ${state.assertion.scope}` : \"\"} allergies\n          </span>\n          <span className=\"zb-allergy-none__meta\" aria-hidden=\"true\">\n            Asserted by {state.assertion.asserter} · {state.assertion.assertedAt}\n            {state.assertion.context ? ` · ${state.assertion.context}` : \"\"}\n          </span>\n        </div>\n      ) : (\n        /*\n         * Not an empty list. A gap, and a gate.\n         *\n         * Deliberately unlike the state above in shape as well as colour: a\n         * reader scanning a chart should be able to tell these apart without\n         * reading either, because the consequence of confusing them is a\n         * prescription written against an allergy history nobody took.\n         */\n        <div className=\"zb-allergy-unknown\" role=\"group\" aria-label={NOT_ASKED_SENTENCE}>\n          <span className=\"zb-allergy-unknown__head\" aria-hidden=\"true\">\n            <ClinicalStatus scale=\"criticality\" step=\"not-assessed\" shape=\"dot\" density=\"compact\" />\n            Allergy status not recorded\n          </span>\n          <span className=\"zb-allergy-unknown__meta\" aria-hidden=\"true\">\n            No entry, and no assertion that there is nothing to enter.\n          </span>\n          {onAsk ? (\n            <button type=\"button\" className=\"zb-allergy-unknown__ask\" onClick={onAsk}>\n              {askLabel}\n            </button>\n          ) : null}\n        </div>\n      )}\n    </div>\n  );\n});\n"
    }
  ]
}
