import { emojiCache } from "#og-image-cache";
import { $fetch } from "ofetch";
import { getEmojiCodePoint, getEmojiIconNames } from "./emoji-utils.js";
export async function getEmojiSvg(ctx, emojiChar) {
  const emojiSet = ctx.options.emojis;
  const codePoint = getEmojiCodePoint(emojiChar);
  const possibleNames = getEmojiIconNames(codePoint, emojiSet);
  let svg = null;
  for (const iconName of possibleNames) {
    const key = ["1", emojiSet, iconName].join("|");
    if (await emojiCache.hasItem(key)) {
      svg = await emojiCache.getItem(key);
      if (svg)
        break;
    }
  }
  if (!svg) {
    for (const iconName of possibleNames) {
      try {
        svg = await $fetch(`https://api.iconify.design/${emojiSet}/${iconName}.svg`, {
          responseType: "text",
          retry: 2,
          retryDelay: 500
        });
        if (svg && svg !== "404") {
          const key = ["1", emojiSet, iconName].join("|");
          await emojiCache.setItem(key, svg);
          break;
        }
      } catch {
        svg = null;
      }
    }
  }
  if (svg) {
    return svg.replace("<svg ", '<svg data-emoji style="margin: 0 .05em 0 .15em; vertical-align: -0.1em;" ');
  }
  return null;
}
