---
import type { CollectionEntry } from "astro:content";

interface Props {
  post: CollectionEntry<"blog">;
  componentType: "short" | "long" | "full";
}

const { post, componentType } = Astro.props;

// default options for the post component
const deafultOptions = {
  showTags: false,
  shortSummary: false,
  longSummary: false,
  fullText: false,
  postFooter: false,
};

const options = { ...deafultOptions };
// Determine which options should be applied to post
switch (componentType) {
  case "full":
    options.postFooter = true;
    options.showTags = true;
    options.fullText = true;
    break;
  case "long":
    options.longSummary = true;
    options.showTags = true;
    break;
  case "short":
    options.shortSummary = true;
    break;
  default:
    break;
}

// Options should not mutated further
Object.freeze(options);

// Format date
const postDateFormatted = post.data.date.toLocaleDateString("tr-TR", {
  day: "numeric",
  month: "long",
  year: "numeric",
});

// Create post link
const postLink = `/${post.data.category}/${post.slug}`;

// Create post content as an astro component
const { Content } = await post.render();

const copyPost = post;

import { Image } from "astro:assets";
import questionMark from "@/images/questionMark.svg";
import calendar from "@/images/calendar.svg";
---

<style>
  .meta-list li {
    padding-top: 0.1rem;
    &:not(:last-child) {
      &:after {
        content: ",\00a0";
      }
    }
  }
</style>

<div class="flex flex-col gap-3">
  <div class="flex flex-col gap-2">
    <p class="tracking-wide text-slate-700">{post.data.subcategory}</p>
    <a class="no-underline text-inherit" href={postLink}>
      <h4 class="text-3xl font-normal">{post.data.title}</h4>
    </a>
    <div class="flex flex-row gap-2">
      {
        options.showTags && post.data.tags?.length && (
          <div class="flex items-center gap-1">
            <Image alt="question mark" src={questionMark} />
            <ul class="list-none flex pl-0 meta-list">
              {post.data.tags.map((tag) => (
                <li class="text-sm text-gray-500">{tag}</li>
              ))}
            </ul>
          </div>
        )
      }

      <div class="flex items-center gap-1">
        <Image alt="calendar" src={calendar} />
        <ul class="list-none flex pl-0 meta-list">
          <p class="text-sm text-gray-500">{postDateFormatted}</p>
        </ul>
      </div>
    </div>
  </div>
  {
    (options.shortSummary || options.longSummary) && (
      <p class="post-summary">{post.data.summary}</p>
    )
  }

  {
    options.fullText && (
      <div class="text-lg" id="full-text">
        <Content />
      </div>
    )
  }
  {
    options.postFooter && (
      <p class="text-gray-600 text-sm">{postDateFormatted}</p>
    )
  }
</div>
