async function initializeDisclaimer() {
  const sourceList = document.querySelector("#source-list");
  try {
    const response = await fetch("./data/index.json", { cache: "no-store" });
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }
    const dataset = await response.json();
    const versions = [...(dataset.versions || [])].sort((a, b) => {
      if (a.fiscalYear !== b.fiscalYear) {
        return b.fiscalYear - a.fiscalYear;
      }
      return b.versionDate.localeCompare(a.versionDate);
    });
    const groups = versions.reduce((acc, version) => {
      const key = String(version.fiscalYear);
      if (!acc[key]) {
        acc[key] = {
          fiscalYear: version.fiscalYear,
          pageLabel: version.sourcePageLabel || "掲載ページ",
          versions: [],
        };
      }
      acc[key].versions.push(version);
      return acc;
    }, {});

    sourceList.innerHTML = Object.values(groups)
      .sort((a, b) => b.fiscalYear - a.fiscalYear)
      .map((group) => {
        const dates = group.versions.map((version) => version.versionDate);
        const latest = group.versions[0];
        const totalCount = latest?.dpcCount || 0;
        return `
          <article class="source-item">
            <h3>令和${group.fiscalYear - 2018}年度</h3>
            <div class="meta-row">
              <span class="year-badge">令和${group.fiscalYear - 2018}年度</span>
              <span class="tag">${group.versions.length}版</span>
              <span class="tag">最新収録 ${dates[0]}</span>
              <span class="tag">${totalCount}件</span>
            </div>
            <p class="detail-copy">ページ名: ${group.pageLabel}</p>
            <p class="detail-copy">ファイル名: 診断群分類（DPC）電子点数表（正式版）</p>
            <p class="detail-copy">収録更新日: ${dates.join(" / ")}</p>
          </article>
        `;
      })
      .join("");
  } catch (error) {
    sourceList.innerHTML = `<div class="empty-state">出典一覧を読み込めませんでした。</div>`;
    console.error(error);
  }
}

initializeDisclaimer();
