---
/**
 * Conditionally renders its children only when the latest Terragrunt release
 * is equal to or newer than the specified semver version.
 *
 * In dev mode, the version is always treated as released so that new content
 * is visible during local development.
 *
 * Usage in .mdx files:
 *   <Since version="1.1.0">
 *     Content shown only when the latest release is >= 1.1.0
 *   </Since>
 *
 * IMPORTANT: Do not use markdown headings (##, ###, etc.) inside <Since>.
 * Starlight extracts headings at build time before the component renders,
 * so hidden headings will still appear in the Table of Contents.
 */
import { getLatestRelease } from '@lib/github';

interface Props {
  version: string;
}

const { version } = Astro.props;

const isDev = import.meta.env.DEV;

let isReleased = true;

if (!isDev) {
  const releaseData = await getLatestRelease('gruntwork-io', 'terragrunt');
  const latestTag = releaseData?.tag_name ?? 'v0.0.0';

  // Strip leading 'v' for comparison
  const latest = latestTag.replace(/^v/, '');
  const target = version.replace(/^v/, '');

  // Compare using locale-aware numeric comparison, matching the pattern
  // used by ChangelogEntries.astro for semver ordering.
  isReleased = latest.localeCompare(target, undefined, { numeric: true }) >= 0;
}
---

{isReleased && <slot />}
