import { getRenderableSectionBlocks, normalizeContent } from "@/admin/pages/sectionLayouts";
import type { PageSection } from "@/admin/types";
import { resolveBlockSlug } from "@/lib/blockSlug";

/** Landing Footer block — suppresses the global Site Settings footer. */
export const FOOTER_BLOCK_SLUG = "footer";

/** PBEL Guide Footer — separate flyer footer; also suppresses the global footer. */
export const PBEL_GUIDE_FOOTER_BLOCK_SLUG = "pbel-guide-footer";

/** Slugs that replace the global Site Settings footer when present on a page. */
export const PAGE_FOOTER_BLOCK_SLUGS = [FOOTER_BLOCK_SLUG, PBEL_GUIDE_FOOTER_BLOCK_SLUG] as const;

export function sectionsHaveSlug(sections: PageSection[], slug: string): boolean {
  for (const section of sections) {
    for (const block of getRenderableSectionBlocks(section)) {
      if (resolveBlockSlug(block) === slug) {
        return true;
      }
    }
  }
  return false;
}

/**
 * True when the page content includes a page-level footer block
 * (`footer` or `pbel-guide-footer`). Closing Vision is never treated as a footer.
 */
export function sectionsHaveFooterBlock(sections: PageSection[]): boolean {
  for (const section of sections) {
    for (const block of getRenderableSectionBlocks(section)) {
      const slug = resolveBlockSlug(block);
      if ((PAGE_FOOTER_BLOCK_SLUGS as readonly string[]).includes(slug)) {
        return true;
      }
    }
  }
  return false;
}

export function pageContentHasFooterBlock(content: unknown): boolean {
  if (!content) return false;
  const sections = normalizeContent(content);
  return sectionsHaveFooterBlock(sections);
}
