'use strict';

async function getAllPosts(adapter, params) {
  const query = params ?? {};
  const whereConditions = [];
  if (query.tagSlug) {
    const tag = await adapter.findOne({
      model: "tag",
      where: [{ field: "slug", value: query.tagSlug, operator: "eq" }]
    });
    if (!tag) {
      return { items: [], total: 0, limit: query.limit, offset: query.offset };
    }
    const postTags = await adapter.findMany({
      model: "postTag",
      where: [{ field: "tagId", value: tag.id, operator: "eq" }]
    });
    const taggedPostIds = postTags.map((pt) => pt.postId);
    if (taggedPostIds.length === 0) {
      return { items: [], total: 0, limit: query.limit, offset: query.offset };
    }
    whereConditions.push({
      field: "id",
      value: taggedPostIds,
      operator: "in"
    });
  }
  if (query.published !== void 0) {
    whereConditions.push({
      field: "published",
      value: query.published,
      operator: "eq"
    });
  }
  if (query.slug) {
    whereConditions.push({
      field: "slug",
      value: query.slug,
      operator: "eq"
    });
  }
  const needsInMemoryFilter = !!query.query;
  const dbWhere = whereConditions.length > 0 ? whereConditions : void 0;
  const dbTotal = !needsInMemoryFilter ? await adapter.count({ model: "post", where: dbWhere }) : void 0;
  const posts = await adapter.findMany({
    model: "post",
    limit: !needsInMemoryFilter ? query.limit : void 0,
    offset: !needsInMemoryFilter ? query.offset : void 0,
    where: dbWhere,
    sortBy: { field: "createdAt", direction: "desc" },
    join: { postTag: true }
  });
  const tagIds = /* @__PURE__ */ new Set();
  for (const post of posts) {
    if (post.postTag) {
      for (const pt of post.postTag) {
        tagIds.add(pt.tagId);
      }
    }
  }
  const tags = tagIds.size > 0 ? await adapter.findMany({
    model: "tag",
    where: [
      {
        field: "id",
        value: Array.from(tagIds),
        operator: "in"
      }
    ]
  }) : [];
  const tagMap = new Map(tags.map((t) => [t.id, t]));
  let result = posts.map((post) => {
    const postTags = (post.postTag || []).map((pt) => {
      const tag = tagMap.get(pt.tagId);
      return tag ? { ...tag } : void 0;
    }).filter((tag) => tag !== void 0);
    const { postTag: _, ...postWithoutJoin } = post;
    return { ...postWithoutJoin, tags: postTags };
  });
  if (query.query) {
    const searchLower = query.query.toLowerCase();
    result = result.filter(
      (post) => post.title?.toLowerCase().includes(searchLower) || post.content?.toLowerCase().includes(searchLower) || post.excerpt?.toLowerCase().includes(searchLower)
    );
  }
  if (needsInMemoryFilter) {
    const total = result.length;
    const offset = query.offset ?? 0;
    const limit = query.limit;
    result = result.slice(
      offset,
      limit !== void 0 ? offset + limit : void 0
    );
    return { items: result, total, limit: query.limit, offset: query.offset };
  }
  return {
    items: result,
    total: dbTotal ?? result.length,
    limit: query.limit,
    offset: query.offset
  };
}
async function getPostBySlug(adapter, slug) {
  const posts = await adapter.findMany({
    model: "post",
    where: [{ field: "slug", value: slug, operator: "eq" }],
    limit: 1,
    join: { postTag: true }
  });
  if (posts.length === 0) return null;
  const post = posts[0];
  const tagIds = (post.postTag || []).map((pt) => pt.tagId);
  const tags = tagIds.length > 0 ? await adapter.findMany({
    model: "tag",
    where: [{ field: "id", value: tagIds, operator: "in" }]
  }) : [];
  const tagMap = new Map(tags.map((t) => [t.id, t]));
  const resolvedTags = (post.postTag || []).map((pt) => tagMap.get(pt.tagId)).filter((t) => t !== void 0);
  const { postTag: _, ...postWithoutJoin } = post;
  return { ...postWithoutJoin, tags: resolvedTags };
}
async function getAllTags(adapter) {
  return adapter.findMany({
    model: "tag",
    sortBy: { field: "name", direction: "asc" }
  });
}

exports.getAllPosts = getAllPosts;
exports.getAllTags = getAllTags;
exports.getPostBySlug = getPostBySlug;
