---
import SiteNav from '../../SiteNav.astro';
import { site } from '../../../lib/siteConfig';
import { tagSlug, type Post } from '../../../lib/posts';

type Props = {
  posts: Post[];
  currentPage?: number;
  totalPages?: number;
};

const { posts, currentPage = 1, totalPages = 1 } = Astro.props;
const pages = Array.from({ length: totalPages }, (_, index) => index + 1);

function pageHref(page: number): string {
  return page === 1 ? '/' : `/page/${page}/`;
}

function formatPublishedAt(date: string): string {
  return new Date(date).toLocaleString(site.language, {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false
  });
}
---

<main class="yar-home">
  <div class="yar-shell">
    <header class="yar-header">
      <div class="yar-header-inner">
        <div class="yar-title-box">
          <h1 class="yar-title">{site.title}</h1>
        </div>
        <SiteNav />
      </div>
    </header>

    <section class="yar-section" aria-label="Posts">
      <div class="yar-posts">
        {posts.length === 0 ? (
          <p class="yar-empty">No published posts yet.</p>
        ) : (
          posts.map((post) => {
            const Content = post.Content;

            return (
              <article class="yar-post">
                <header class="yar-post-header">
                  <a href={post.url}>
                    <div class="heti heti--sans">
                      <h2 class="post-title">{post.title}</h2>
                    </div>
                  </a>
                  {post.date && (
                    <time class="yar-date" datetime={post.date}>
                      发布时间：{formatPublishedAt(post.date)}
                    </time>
                  )}
                </header>
                <div class="yar-post-content heti heti--sans">
                  <Content />
                </div>
                {post.tags.length > 0 && (
                  <ul class="yar-tags" aria-label="Tags">
                    {post.tags.map((tag) => (
                      <li>
                        <a href={`/tags/${tagSlug(tag)}/`}>{tag}</a>
                      </li>
                    ))}
                  </ul>
                )}
              </article>
            );
          })
        )}
      </div>
    </section>

    {totalPages > 1 && (
      <nav class="yar-pagination" aria-label="Pagination">
        {currentPage > 1 && (
          <a href={pageHref(currentPage - 1)} aria-label="上一页">上一页</a>
        )}
        <ol>
          {pages.map((page) => (
            <li>
              <a
                href={pageHref(page)}
                class:list={['yar-page-link', { 'is-current': page === currentPage }]}
                aria-current={page === currentPage ? 'page' : undefined}
              >
                {page}
              </a>
            </li>
          ))}
        </ol>
        {currentPage < totalPages && (
          <a href={pageHref(currentPage + 1)} aria-label="下一页">下一页</a>
        )}
      </nav>
    )}

    <footer class="yar-footer">
      <p>由 osaet 构建</p>
      <p>© 2026 Osaet. All rights reserved.</p>
    </footer>
  </div>
</main>

<style>
  .yar-home {
    min-height: 100vh;
    background: #ffffff;
    color: #1c211b;
  }

  .yar-shell {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
    width: 100%;
    padding: 40px 0 5vh;
  }

  .yar-header {
    border-bottom: 1px solid rgba(28, 33, 27, 0.18);
  }

  .yar-header-inner {
    display: flex;
    justify-content: space-between;
    gap: 24px;
    align-items: center;
    width: 50vw;
    margin: 0 auto;
    padding-bottom: 28px;
  }

  .yar-shell :global(.site-nav) {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25em;
    justify-content: flex-end;
    margin: 0;
    border-radius: 0.75em;
    background: #ffffff;
    box-shadow: 0 0em 0.2em rgb(29 53 87 / 13%);
    padding: 0.5em 1.4em;
    color: #53605a;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.86rem;
  }

  .yar-shell :global(.site-nav a) {
    border-radius: 0.65em;
    color: inherit;
    padding: 0.55em 0.8em;
    text-decoration: none;
  }

  .yar-shell :global(.site-nav a:hover) {
    background: #eef1ed;
    text-decoration: none;
  }

  .heti {
    max-width: 42em;
    font-family:
      "Heti Hei", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
      "Noto Sans CJK SC", "Source Han Sans SC", "Source Han Sans CN",
      sans-serif;
    font-size: 16px;
    line-height: 1.9;
    letter-spacing: 0;
    overflow-wrap: break-word;
    text-align: justify;
  }

  .heti h1,
  .heti h2,
  .heti h3,
  .heti h4,
  .heti h5,
  .heti h6 {
    font-family:
      "Heti Hei", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
      sans-serif;
    font-weight: 700;
    line-height: 1.35;
    text-align: start;
  }

  .heti--sans {
    font-family:
      "Heti Hei", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
      "Noto Sans CJK SC", "Source Han Sans SC", "Source Han Sans CN",
      sans-serif;
  }

  .heti a {
    text-decoration-thickness: 1px;
    text-underline-offset: 0.18em;
  }

  .heti img {
    max-width: 100%;
    height: auto;
  }

  .yar-title-box {
    border-radius: 0.7em;
    background: #1d3557;
    padding: 1em 1.4em;
  }

  .yar-title {
    margin: 0;
    color: #ffffff;
    font-family:
      "Heti Hei", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
      sans-serif;
    font-size: 1em;
    letter-spacing: 0.16em;
    line-height: 1.15;
  }

  .yar-section {
    width: 50vw;
    margin: 0 auto;
    padding: 36px 0 0;
  }

  .yar-posts {
    display: grid;
    gap: 0;
  }

  .yar-post {
    color: #3d3d3f;
    font-size: 16px;
    line-height: 1.7em;
    border-bottom: 2px dashed #eee;
    letter-spacing: 1px;
    padding-bottom: 1.5em;
    margin-bottom: 3.5em;
  }

  .yar-post + .yar-post {
    padding-top: 0;
  }

  .yar-post-header a {
    display: inline-block;
    color: inherit;
    text-decoration: none;
  }

  .post-title {
    margin: 0 0 8px;
    line-height: 1.2;
  }

  .yar-post-content {
    margin-top: 12px;
  }

  .yar-post-content :global(p:first-child) {
    margin-top: 0;
  }

  .yar-post-content :global(p:last-child) {
    margin-bottom: 0;
  }

  .yar-post-content :global(img) {
    display: block;
    border-radius: 0.7em;
    max-width: 100%;
    height: auto;
    margin: 1.2em auto;
  }

  .yar-post-content :global(hr) {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    border: 0;
    margin: 2em 0;
    color: #0003;
    font-size: 1.1em;
    text-align: center;
  }

  .yar-post-content :global(hr::before) {
    content: "* * *";
  }

  .yar-post-content :global(pre) {
    overflow-x: auto;
    border-radius: 0.8em;
    background: #f6f8fa;
    box-shadow: inset 0 0 0 1px rgb(29 53 87 / 8%);
    color: #263238;
    font-size: 0.92em;
    line-height: 1.7;
    margin: 1.4em 0;
    padding: 1em 1.2em;
  }

  .yar-post-content :global(pre code) {
    background: transparent;
    color: inherit;
    font-family:
      "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: inherit;
    line-height: inherit;
    padding: 0;
  }

  .yar-post-content :global(:not(pre) > code) {
    border-radius: 0.35em;
    background: #f3f6f4;
    color: #1d3557;
    font-family:
      "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.92em;
    padding: 0.15em 0.35em;
  }

  .yar-post-content :global(a) {
    color: #1d3557;
    font-weight: 600;
    text-decoration: underline;
    text-decoration-color: rgb(29 53 87 / 24%);
    text-decoration-thickness: 0.12em;
    text-underline-offset: 0.2em;
  }

  .yar-post-content :global(a:hover) {
    background: #f3f6f4;
    text-decoration-color: rgb(29 53 87 / 48%);
  }

  .yar-post p {
    margin: 0;
    color: #596254;
    line-height: 1.8;
  }

  .yar-date {
    display: block;
    margin-top: 0.45em;
    color: #7a8375;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.78rem;
    white-space: nowrap;
  }

  .yar-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.55em;
    padding: 1em 0 0;
    margin: 0;
    list-style: none;
  }

  .yar-tags li {
    display: inline-flex;
  }

  .yar-tags a {
    border-radius: 999em;
    background: #f3f6f4;
    box-shadow: inset 0 0 0 1px rgb(29 53 87 / 7%);
    color: #53605a;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.78rem;
    line-height: 1;
    padding: 0.55em 0.8em;
    text-decoration: none;
  }

  .yar-tags a:hover {
    background: #e8eee9;
  }

  .yar-empty {
    margin: 0;
    color: #596254;
  }

  .yar-pagination {
    display: flex;
    justify-content: center;
    gap: 0.7em;
    align-items: center;
    width: 50vw;
    margin: 0 auto 3.5em;
    color: #53605a;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.86rem;
  }

  .yar-pagination ol {
    display: flex;
    gap: 0.45em;
    padding: 0;
    margin: 0;
    list-style: none;
  }

  .yar-pagination a {
    border-radius: 0.7em;
    color: inherit;
    padding: 0.55em 0.8em;
    text-decoration: none;
  }

  .yar-pagination a:hover,
  .yar-pagination .is-current {
    background: #eef1ed;
  }

  .yar-footer {
    width: 50vw;
    margin: auto auto 0;
    color: #8a9089;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.78rem;
    line-height: 1.8;
    text-align: center;
  }

  .yar-footer p {
    margin: 0;
  }

  @media (max-width: 760px) {
    .yar-shell {
      padding-top: 24px;
    }

    .yar-header-inner,
    .yar-section,
    .yar-pagination,
    .yar-footer {
      width: calc(100% - 28px);
    }

    .yar-header-inner {
      flex-direction: column;
      align-items: flex-start;
      gap: 16px;
    }

    .yar-date {
      white-space: normal;
    }
  }
</style>
