---
import { site } from '../../../lib/siteConfig';
import { tagSlug } from '../../../lib/posts';

type Props = {
  post: any;
};

const { post } = Astro.props;
const { Content } = post;
const date = post.frontmatter.created_at ?? post.frontmatter.published_at ?? post.frontmatter.updated_at;

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

<main class="yar-page">
  <div class="yar-shell">
    <div class="yar-back-row">
      <a class="yar-back" href="/" data-history-back>← Back</a>
    </div>

    <article class="yar-article">
      <header class="yar-article-header">
        <div class="heti heti--sans">
          <h1 class="post-title">{post.frontmatter.title}</h1>
        </div>
        {date && (
          <time class="yar-date" datetime={date}>
            {formatPublishedAt(date)}
          </time>
        )}
        {post.frontmatter.tags?.length > 0 && (
          <ul class="yar-tags" aria-label="Tags">
            {post.frontmatter.tags.map((tag) => (
              <li>
                <a href={`/tags/${tagSlug(tag)}/`}>{tag}</a>
              </li>
            ))}
          </ul>
        )}
      </header>

      <div class="yar-post-content heti heti--sans">
        <Content />
      </div>
    </article>

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

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

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

  .yar-back-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1em;
    width: 50vw;
    margin: 0 auto;
  }

  .yar-back {
    color: #1d3557;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.86rem;
    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-back:hover {
    background: #f3f6f4;
    text-decoration-color: rgb(29 53 87 / 48%);
  }

  .heti,
  .heti--sans {
    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;
  }

  .yar-article {
    width: 50vw;
    margin: 0 auto;
    padding: 36px 0 0;
    color: #3d3d3f;
    font-size: 16px;
    line-height: 1.7em;
    letter-spacing: 1px;
  }

  .yar-article-header {
    text-align: center;
  }

  .yar-article-header .heti {
    max-width: none;
    text-align: center;
  }

  .yar-article-header .post-title {
    margin: 0 0 8px;
    font-size: 1.75em;
    letter-spacing: 0.06em;
    line-height: 1.2;
    text-align: center;
  }

  .yar-date {
    display: block;
    margin: 0.45em 0 2.4em;
    color: #7a8375;
    font-family:
      ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      sans-serif;
    font-size: 0.78rem;
    text-align: center;
    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-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-content :global(p) {
    color: #596254;
    line-height: 1.8;
  }

  .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-back-row,
    .yar-article,
    .yar-footer {
      width: calc(100% - 28px);
    }

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

<script>
  const backLink = document.querySelector('[data-history-back]');

  backLink?.addEventListener('click', (event) => {
    if (window.history.length <= 1) {
      return;
    }

    event.preventDefault();
    window.history.back();
  });
</script>
