---
import { getCollection, render } from "astro:content";
import { getLatestRelease } from "@lib/github";

interface Props {
  status: "active" | "completed";
  showUnreleased?: boolean;
}

const { status, showUnreleased = false } = Astro.props;

const releaseData = await getLatestRelease("gruntwork-io", "terragrunt");
const latestVersion = (releaseData?.tag_name ?? "v0.0.0").replace(/^v/, "");

function isSinceReleased(since?: string): boolean {
  if (!since) return true;
  const target = since.replace(/^v/, "");
  return latestVersion.localeCompare(target, undefined, { numeric: true }) >= 0;
}

const allEntries = await getCollection("strictControls");

const entries = allEntries
  .filter((entry) => entry.data.status === status)
  .filter((entry) => showUnreleased || isSinceReleased(entry.data.since))
  .sort((a, b) => a.data.name.localeCompare(b.data.name));

const rendered = await Promise.all(
  entries.map(async (entry) => {
    const { Content } = await render(entry);
    return { entry, Content };
  })
);
---

{rendered.map(({ entry, Content }, i) => (
  <div>
    {i > 0 && <hr />}
    <h2 id={entry.data.name}>
      <code>{entry.data.name}</code>
      <a class="anchor-link" href={`#${entry.data.name}`}>
        <span aria-hidden="true" class="anchor-icon" />
      </a>
    </h2>
    <Content />
  </div>
))}
