{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "emoji-icon-picker-adapters",
  "type": "registry:lib",
  "title": "Emoji Icon Picker data adapters",
  "description": "Emoji Icon Picker data adapters. Canonical Emoji Icon Picker owner.",
  "dependencies": [],
  "registryDependencies": [
    "https://ghost-ui.com/r/emoji-icon-picker-core.json"
  ],
  "files": [
    {
      "path": "src/lib/emoji-icon-picker-adapters.ts",
      "type": "registry:lib",
      "target": "@lib/emoji-icon-picker-adapters.ts",
      "content": "/** Structural adapters. These accept data supplied by the caller and import no vendor code. */\nimport type {\n  EmojiIconContent,\n  EmojiIconItem,\n  EmojiIconTone,\n  EmojiIconVariant,\n} from \"./emoji-icon-picker\";\n\nexport interface EmojiIconAdapterResult<M = unknown> {\n  readonly items: readonly EmojiIconItem<M>[];\n  readonly categories: readonly string[];\n  readonly categoryLabels?: Readonly<Record<string, string>>;\n}\n\ntype Row = Record<string, unknown>;\nconst record = (value: unknown): Row | null =>\n  value !== null && typeof value === \"object\" ? (value as Row) : null;\nconst text = (value: unknown): string | undefined =>\n  typeof value === \"string\" && value.length ? value : undefined;\nconst strings = (value: unknown): string[] =>\n  Array.isArray(value)\n    ? value.filter((item): item is string => typeof item === \"string\")\n    : [];\nconst toneFrom = (value: unknown): EmojiIconTone | undefined => {\n  if (typeof value === \"string\") {\n    const name = value.toLowerCase().replace(/[^a-z0-9]/g, \"\");\n    const named: Record<string, EmojiIconTone> = {\n      none: 0,\n      default: 0,\n      light: 1,\n      skintone1: 1,\n      tone1: 1,\n      mediumlight: 2,\n      skintone2: 2,\n      tone2: 2,\n      medium: 3,\n      skintone3: 3,\n      tone3: 3,\n      mediumdark: 4,\n      skintone4: 4,\n      tone4: 4,\n      dark: 5,\n      skintone5: 5,\n      tone5: 5,\n    };\n    if (name in named) return named[name];\n  }\n  const n =\n    typeof value === \"number\"\n      ? value\n      : typeof value === \"string\" && /^\\d$/.test(value)\n        ? Number(value)\n        : NaN;\n  return Number.isInteger(n) && n >= 0 && n <= 5\n    ? (n as EmojiIconTone)\n    : undefined;\n};\nconst emojiContent = (\n  native: unknown,\n  hexcode?: unknown,\n): EmojiIconContent | undefined => {\n  const glyph = text(native);\n  return glyph\n    ? {\n        type: \"emoji\",\n        text: glyph,\n        ...(text(hexcode) ? { hexcode: text(hexcode) } : {}),\n      }\n    : undefined;\n};\nfunction sequenceTone(hexcode: string): EmojiIconTone | undefined {\n  const modifiers = hexcode.match(/1F3F[B-F]/gi) ?? [];\n  const first = modifiers[0];\n  return first &&\n    modifiers.every((value) => value.toUpperCase() === first.toUpperCase())\n    ? ((parseInt(first, 16) - 0x1f3fa) as EmojiIconTone)\n    : undefined;\n}\nconst categoryFrom = (\n  value: unknown,\n  _labels?: Readonly<Record<string, string>>,\n): string | undefined => {\n  const raw =\n    text(value) ?? (typeof value === \"number\" ? String(value) : undefined);\n  return raw;\n};\nconst categoryOrder = (\n  values: readonly unknown[],\n  labels?: Readonly<Record<string, string>>,\n): string[] => {\n  const result: string[] = [];\n  for (const value of values) {\n    const label = categoryFrom(value, labels);\n    if (label && !result.includes(label)) result.push(label);\n  }\n  return result;\n};\n\nexport function fromEmojibase(\n  data: readonly unknown[],\n  shortcodes?: Readonly<Record<string, string | readonly string[]>>,\n  categoryLabels?: Readonly<Record<string, string>> | readonly string[],\n): EmojiIconAdapterResult<Row> {\n  const labels: Readonly<Record<string, string>> | undefined = Array.isArray(\n    categoryLabels,\n  )\n    ? Object.fromEntries(\n        categoryLabels.map((label, index) => [String(index), label]),\n      )\n    : (categoryLabels as Readonly<Record<string, string>> | undefined);\n  const groups = [\n    ...new Set(\n      data\n        .map((entry) => record(entry)?.group)\n        .filter((value): value is number => typeof value === \"number\"),\n    ),\n  ].sort((a, b) => a - b);\n  const items = data.flatMap((entry): EmojiIconItem<Row>[] => {\n    const row = record(entry);\n    if (!row) return [];\n    const glyph = text(row.unicode) ?? text(row.emoji);\n    const hexcode = text(row.hexcode);\n    if (!glyph || !hexcode) return [];\n    const id = hexcode.toLowerCase();\n    const category = categoryFrom(row.group, labels);\n    const short = shortcodes?.[hexcode] ?? shortcodes?.[hexcode.toUpperCase()];\n    const aliases =\n      typeof short === \"string\" ? [short] : short ? [...short] : [];\n    const skins = Array.isArray(row.skins) ? row.skins : [];\n    const variants: EmojiIconVariant[] = skins.flatMap((value, index) => {\n      const skin = record(value);\n      if (!skin) return [];\n      const skinEmoji = emojiContent(skin.unicode ?? skin.emoji, skin.hexcode);\n      if (!skinEmoji) return [];\n      const skinHex = text(skin.hexcode) ?? `${hexcode}-${index + 1}`;\n      const tone = sequenceTone(skinHex);\n      return [\n        {\n          id: skinHex.toLowerCase(),\n          label: text(skin.label),\n          ...(tone ? { tone } : {}),\n          content: skinEmoji,\n        },\n      ];\n    });\n    return [\n      {\n        id,\n        label: text(row.label) ?? glyph,\n        content: { type: \"emoji\", text: glyph, hexcode },\n        ...(category ? { category } : {}),\n        keywords: strings(row.tags),\n        shortcodes: aliases,\n        ...(variants.length ? { variants } : {}),\n        metadata: { ...row, source: \"emojibase\" },\n      },\n    ];\n  });\n  const categoryNumbers = categoryOrder(groups, labels);\n  return Object.freeze({\n    items: Object.freeze(items),\n    categories: Object.freeze(categoryNumbers),\n    ...(labels ? { categoryLabels: Object.freeze({ ...labels }) } : {}),\n  });\n}\n\nfunction records(value: unknown): Row[] {\n  if (Array.isArray(value))\n    return value.map(record).filter((row): row is Row => !!row);\n  const object = record(value);\n  return object\n    ? Object.values(object)\n        .map(record)\n        .filter((row): row is Row => !!row)\n    : [];\n}\n\nexport function fromEmojiMart(\n  data: unknown,\n  custom: readonly unknown[] = [],\n): EmojiIconAdapterResult<Row> {\n  const root = record(data);\n  if (!root)\n    return Object.freeze({\n      items: Object.freeze([]),\n      categories: Object.freeze([]),\n    });\n  const groups = [\n    ...records(root.categories),\n    ...records(root.custom),\n    ...records(custom),\n  ];\n  const categoryNames = [\n    ...new Set(\n      groups\n        .map((category) => text(category.id) ?? text(category.name))\n        .filter((name): name is string => !!name),\n    ),\n  ];\n  const entries = [\n    ...records(root.emojis),\n    ...groups.flatMap((group) => records(group.emojis)),\n  ];\n  const sourceAliases = new Map<string, string[]>();\n  for (const [alias, target] of Object.entries(record(root.aliases) ?? {})) {\n    if (typeof target === \"string\")\n      sourceAliases.set(target, [...(sourceAliases.get(target) ?? []), alias]);\n  }\n  const membership = new Map<string, string>();\n  for (const group of groups) {\n    const id = text(group.id) ?? text(group.name);\n    if (id)\n      for (const emoji of strings(group.emojis)) membership.set(emoji, id);\n    if (id)\n      for (const emoji of records(group.emojis)) {\n        const emojiId = text(emoji.id) ?? text(emoji.name);\n        if (emojiId) membership.set(emojiId, id);\n      }\n  }\n  const items = entries.flatMap((row): EmojiIconItem<Row>[] => {\n    const skinRows = records(row.skins);\n    const firstSkin = skinRows[0];\n    const native =\n      text(row.native) ?? text(firstSkin?.native) ?? text(firstSkin?.emoji);\n    const id = text(row.id) ?? text(row.name) ?? text(row.unified);\n    const baseSrc = text(firstSkin?.src);\n    if ((!native && !baseSrc) || !id) return [];\n    const category =\n      text(row.category) ?? text(row.group) ?? membership.get(id);\n    const baseContent: EmojiIconContent = native\n      ? {\n          type: \"emoji\",\n          text: native,\n          ...(text(row.unified ?? firstSkin?.unified)\n            ? { hexcode: text(row.unified ?? firstSkin?.unified) }\n            : {}),\n        }\n      : {\n          type: \"image\",\n          src: baseSrc!,\n          ...(text(firstSkin?.name) ? { alt: text(firstSkin?.name) } : {}),\n        };\n    const variants: EmojiIconVariant[] = skinRows.flatMap((skin, index) => {\n      const skinNative = text(skin.native) ?? text(skin.emoji);\n      const skinSrc = text(skin.src);\n      if (\n        (!skinNative && !skinSrc) ||\n        (skinNative === native && !skinSrc) ||\n        (skinSrc === baseSrc && index === 0)\n      )\n        return [];\n      const rawTone = skin.skin_tone ?? skin.tone ?? skin.skinTone;\n      const tone =\n        rawTone !== undefined\n          ? toneFrom(rawTone)\n          : skinNative\n            ? sequenceTone(\n                text(skin.unified) ??\n                  [...skinNative]\n                    .map((char) => char.codePointAt(0)!.toString(16))\n                    .join(\"-\"),\n              )\n            : index >= 1 && index <= 5\n              ? (index as EmojiIconTone)\n              : undefined;\n      const unified =\n        text(skin.unified) ?? text(skin.id) ?? `${id}-skin-${index + 1}`;\n      const hexcode = text(skin.unified);\n      const content: EmojiIconContent = skinNative\n        ? { type: \"emoji\", text: skinNative, ...(hexcode ? { hexcode } : {}) }\n        : {\n            type: \"image\",\n            src: skinSrc!,\n            ...(text(skin.name) ? { alt: text(skin.name) } : {}),\n          };\n      return [\n        {\n          id: unified.toLowerCase(),\n          label: text(skin.name),\n          ...(tone !== undefined ? { tone } : {}),\n          content,\n        },\n      ];\n    });\n    const aliases = [\n      id,\n      ...(sourceAliases.get(id) ?? []),\n      ...strings(row.aliases),\n      ...strings(row.short_names),\n      ...strings(row.shortcodes),\n      ...strings(row.emoticons),\n    ];\n    if (text(row.shortcode)) aliases.push(row.shortcode as string);\n    const keywords = [...strings(row.keywords), ...strings(row.tags)];\n    return [\n      {\n        id,\n        label: text(row.name) ?? id,\n        content: baseContent,\n        ...(category ? { category } : {}),\n        keywords,\n        shortcodes: aliases,\n        ...(variants.length ? { variants } : {}),\n        metadata: { ...row, source: \"emoji-mart\" },\n      },\n    ];\n  });\n  const categories = categoryNames.length\n    ? categoryNames\n    : [\n        ...new Set(\n          items.map((item) => item.category).filter((x): x is string => !!x),\n        ),\n      ];\n  return Object.freeze({\n    items: Object.freeze(items),\n    categories: Object.freeze(categories),\n  });\n}\n\nexport function fromFrimousse(data: unknown): EmojiIconAdapterResult<Row> {\n  const root = record(data);\n  if (!root)\n    return Object.freeze({\n      items: Object.freeze([]),\n      categories: Object.freeze([]),\n    });\n  const emojiRows = records(root.emojis);\n  const categoryRows = records(root.categories);\n  const labels = Object.fromEntries(\n    categoryRows.flatMap((category) => {\n      const index = category.index;\n      const label = text(category.label);\n      return typeof index === \"number\" && label ? [[String(index), label]] : [];\n    }),\n  );\n  const items = emojiRows.flatMap((row): EmojiIconItem<Row>[] => {\n    const native = text(row.emoji);\n    const id = text(row.id) ?? text(row.hexcode)?.toLowerCase() ?? native;\n    if (!native || !id) return [];\n    const skinMap = record(row.skins) ?? {};\n    const variants: EmojiIconVariant[] = Object.entries(skinMap).flatMap(\n      ([key, value]) => {\n        const skinEmoji = text(value);\n        if (!skinEmoji) return [];\n        const tone = toneFrom(key);\n        return [\n          {\n            id: `tone-${key}`,\n            ...(tone !== undefined ? { tone } : {}),\n            content: { type: \"emoji\", text: skinEmoji },\n          },\n        ];\n      },\n    );\n    return [\n      {\n        id,\n        label: text(row.label) ?? id,\n        content: {\n          type: \"emoji\",\n          text: native,\n          ...(text(row.hexcode) ? { hexcode: text(row.hexcode) } : {}),\n        },\n        ...(categoryFrom(row.category, labels)\n          ? { category: categoryFrom(row.category, labels) }\n          : {}),\n        keywords: strings(row.tags),\n        shortcodes: [...strings(row.aliases), ...strings(row.shortcodes)],\n        ...(variants.length ? { variants } : {}),\n        metadata: { ...row, source: \"frimousse\" },\n      },\n    ];\n  });\n  const categories = categoryRows\n    .map((category) => categoryFrom(category.index))\n    .filter((id): id is string => !!id);\n  return Object.freeze({\n    items: Object.freeze(items),\n    categories: Object.freeze(categories),\n    categoryLabels: Object.freeze(labels),\n  });\n}\n"
    }
  ]
}
