import { defu } from "defu";
import { toValue } from "vue";
export { extractSocialPreviewTags, toBase64Image } from "./pure.js";
export { buildOgImageUrl, decodeOgImageParams, encodeOgImageParams, extractEncodedSegment, hashOgImageOptions, parseOgImageUrl } from "./shared/urlEncoding.js";
const RE_KEBAB_CASE = /-([a-z])/g;
export function generateMeta(url, resolvedOptions) {
  const key = resolvedOptions.key || "og";
  const isTwitterOnly = key === "twitter";
  const includeTwitter = key === "og" || key === "twitter";
  const meta = [];
  if (!isTwitterOnly) {
    meta.push({ property: "og:image", content: url });
    meta.push({ property: "og:image:type", content: () => `image/${getExtension(toValue(url)) || resolvedOptions.extension}` });
  }
  if (includeTwitter) {
    meta.push({ name: "twitter:card", content: "summary_large_image" });
    meta.push({ name: "twitter:image", content: url });
    meta.push({ name: "twitter:image:src", content: url });
  }
  if (resolvedOptions.width) {
    if (!isTwitterOnly)
      meta.push({ property: "og:image:width", content: resolvedOptions.width });
    if (includeTwitter)
      meta.push({ name: "twitter:image:width", content: resolvedOptions.width });
  }
  if (resolvedOptions.height) {
    if (!isTwitterOnly)
      meta.push({ property: "og:image:height", content: resolvedOptions.height });
    if (includeTwitter)
      meta.push({ name: "twitter:image:height", content: resolvedOptions.height });
  }
  if (resolvedOptions.alt) {
    if (!isTwitterOnly)
      meta.push({ property: "og:image:alt", content: resolvedOptions.alt });
    if (includeTwitter)
      meta.push({ name: "twitter:image:alt", content: resolvedOptions.alt });
  }
  return meta;
}
export function isInternalRoute(path) {
  return path.startsWith("/_") || path.startsWith("@");
}
function filterIsOgImageOption(key) {
  const keys = [
    "url",
    "extension",
    "width",
    "height",
    "alt",
    "props",
    "renderer",
    "html",
    "component",
    "emojis",
    "_query",
    "_hash",
    "fonts",
    "satori",
    "resvg",
    "sharp",
    "screenshot",
    "takumi",
    "cacheMaxAgeSeconds",
    "cacheKey",
    "key"
  ];
  return keys.includes(key);
}
export function separateProps(options, ignoreKeys = []) {
  options = options || {};
  const _props = defu(options.props, Object.fromEntries(
    Object.entries({ ...options }).filter(([k]) => !filterIsOgImageOption(k) && !ignoreKeys.includes(k))
  ));
  const props = {};
  Object.entries(_props).forEach(([key, val]) => {
    props[key.replace(RE_KEBAB_CASE, (g) => String(g[1]).toUpperCase())] = val;
  });
  const result = Object.fromEntries(
    Object.entries({ ...options }).filter(([k]) => filterIsOgImageOption(k) || ignoreKeys.includes(k))
  );
  if (Object.keys(props).length > 0)
    result.props = props;
  return result;
}
const DANGEROUS_ATTRS = /* @__PURE__ */ new Set(["autofocus", "contenteditable", "tabindex", "accesskey"]);
export function sanitizeProps(props) {
  const clean = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("on") || DANGEROUS_ATTRS.has(key.toLowerCase()))
      continue;
    clean[key] = props[key];
  }
  return clean;
}
export function withoutQuery(path) {
  return path.split("?")[0];
}
export function getExtension(path) {
  path = withoutQuery(path);
  const lastSegment = path.split("/").pop() || path;
  const extension = lastSegment.split(".").pop() || lastSegment;
  if (extension === "jpg")
    return "jpeg";
  return extension;
}
