import { Emoji, EmojiStyle } from "emoji-picker-react";

function emojiUnicode(emoji: string): string {
  let comp;
  if (emoji.length === 1) {
    comp = emoji.charCodeAt(0);
  }
  comp =
    (emoji.charCodeAt(0) - 0xd800) * 0x400 +
    (emoji.charCodeAt(1) - 0xdc00) +
    0x10000;
  if (comp < 0) {
    comp = emoji.charCodeAt(0);
  }
  const s = comp.toString(16);
  return s.includes("f") ? s : `${s}-fe0f`;
}

export function EmojiIcon(p: {
  emojiKey: string;
  size?: number;
}): React.ReactElement {
  const unified = emojiUnicode(p.emojiKey);
  return (
    <Emoji
      unified={unified ?? ""}
      emojiStyle={EmojiStyle.NATIVE}
      size={p.size ?? 18}
    />
  );
}
