import { withQuery } from 'ufo';
import { useQuery } from 'h3';
import { p as parse } from '../index.mjs';
import { d as defineCachedEventHandler, u as useRuntimeConfig } from '../nitro/node-server.mjs';
import 'pathe';
import 'defu';
import 'remark-emoji';
import 'rehype-slug';
import 'remark-squeeze-paragraphs';
import 'rehype-external-links';
import 'remark-gfm';
import 'rehype-sort-attribute-values';
import 'rehype-sort-attributes';
import 'rehype-raw';
import 'gray-matter';
import 'flat';
import 'unified';
import 'remark-parse';
import 'remark-rehype';
import 'scule';
import 'unist-util-visit';
import 'stringify-entities';
import 'parse-entities';
import 'micromark-util-character';
import 'micromark-factory-space';
import 'micromark-factory-whitespace';
import 'micromark-core-commonmark';
import 'mdast-util-to-hast';
import 'detab';
import 'unist-builder';
import 'mdurl';
import 'unist-util-position';
import 'html-tags';
import 'destr';
import 'slugify';
import 'unenv/runtime/polyfill/fetch.node';
import 'http';
import 'https';
import 'ohmyfetch';
import 'radix3';
import 'unenv/runtime/fetch/index';
import 'hookable';
import 'ohash';
import 'unstorage';
import 'unstorage/drivers/overlay';
import 'unstorage/drivers/memory';

const releases = defineCachedEventHandler(async ({ req }) => {
  const { releases: releasesConfig } = useRuntimeConfig().github;
  const query = useQuery(req);
  let releases = await fetchGitHubReleases(query, releasesConfig);
  if (releasesConfig.parse && typeof parse === "function") {
    releases = await Promise.all(releases.map(async (release) => ({
      ...release,
      ...await parse(`${release.name}.md`, release.body)
    })));
  }
  releases.sort((a, b) => a.v !== b.v ? b.v - a.v : a.date - b.date);
  return releases;
}, {
  maxAge: 60
});
const normalizeReleaseName = (name) => {
  name = name.replace("Release ", "");
  if (!name.match(/^[a-zA-Z]/)) {
    name = `v${name}`;
  }
  return name;
};
async function fetchGitHubReleases(query, { api: configApi, repo: configRepo, token: configToken }) {
  const api = query?.api || configApi;
  const token = query?.token || configToken;
  const repo = query?.repo || configRepo;
  const perPage = query?.per_page;
  const page = query?.page;
  const url = withQuery(`${api}/${repo}/releases`, { per_page: perPage, page });
  const rawReleases = await $fetch(url, {
    headers: {
      Authorization: token ? `token ${token}` : void 0
    }
  }).catch((err) => {
    console.warn(`Cannot fetch GitHub releases on ${url} [${err.response?.status}]`);
    console.info("If your repository is private, make sure to provide GITHUB_TOKEN environment in `.env`");
    if (err.response.status !== 403) {
      console.info("To disable fetching releases, set `github.releases` to `false` in `nuxt.config.ts`");
    }
    return [];
  });
  return rawReleases.filter((r) => !r.draft).map((release) => {
    return {
      name: normalizeReleaseName(release?.name || release?.tag_name),
      date: release?.published_at,
      body: release?.body,
      v: +normalizeReleaseName(release?.tag_name).substring(1, 2),
      url: release?.html_url,
      tarball: release?.tarball_url,
      zipball: release?.zipball_url,
      prerelease: release?.prerelease,
      reactions: release?.reactions,
      author: release?.author ? {
        name: release.author?.login,
        url: release.author?.html_url,
        avatar: release.author?.avatar_url
      } : false
    };
  });
}

export { releases as default, fetchGitHubReleases };
//# sourceMappingURL=releases.mjs.map
