import { query } from "../db.js";
class PatternsResolverService {
  /**
   * Get multiple patterns by IDs (batch fetch)
   *
   * Used for resolving pattern references in public pages.
   * Only fetches PUBLISHED patterns (drafts are not shown publicly).
   *
   * @param ids - Array of pattern IDs
   * @returns Array of published patterns
   *
   * @example
   * const patternRefs = [{ ref: 'uuid-1' }, { ref: 'uuid-2' }]
   * const patterns = await PatternsResolverService.getByIds(
   *   patternRefs.map(p => p.ref)
   * )
   */
  static async getByIds(ids) {
    try {
      if (!Array.isArray(ids) || ids.length === 0) {
        return [];
      }
      const validIds = ids.filter((id) => id && id.trim());
      if (validIds.length === 0) {
        return [];
      }
      const placeholders = validIds.map((_, i) => `$${i + 1}`).join(", ");
      const result = await query(
        `
        SELECT
          id,
          "userId",
          "teamId",
          title,
          slug,
          blocks,
          status,
          description,
          "createdAt",
          "updatedAt"
        FROM "patterns"
        WHERE id IN (${placeholders})
          AND status = 'published'
        `,
        validIds
      );
      return result.rows.map((pattern) => ({
        id: pattern.id,
        userId: pattern.userId,
        teamId: pattern.teamId,
        title: pattern.title,
        slug: pattern.slug,
        blocks: Array.isArray(pattern.blocks) ? pattern.blocks : [],
        status: pattern.status,
        description: pattern.description ?? void 0,
        createdAt: pattern.createdAt,
        updatedAt: pattern.updatedAt
      }));
    } catch (error) {
      console.error("[PatternsResolverService.getByIds] Error:", error);
      return [];
    }
  }
}
export {
  PatternsResolverService
};
