import { getValueFromContactConfig } from "@/utils/contactConfig"

import type { FooterRefDataRaw } from "@/types/components/footer/footer"
import type { System } from "@/types/requests/system"
import type { Edges } from "@/types/requests/utils/edges"
import type { NodeRefs } from "@/types/requests/utils/refs"
import type { HeaderRefs } from "@/types/trpc/routers/contentstack/header"
import type {
  AlertOutput,
  GetSiteConfigRefData,
} from "@/types/trpc/routers/contentstack/siteConfig"
import type { ContactConfig } from "./output"

export function getConnections({ header }: HeaderRefs) {
  const connections: System["system"][] = [header.system]

  if (header.top_link) {
    if (header.top_link.logged_in?.link) {
      connections.push(header.top_link.logged_in.link)
    }
    if (header.top_link.logged_out?.link) {
      connections.push(header.top_link.logged_out.link)
    }
  }

  if (header.menu_items.length) {
    header.menu_items.forEach((menuItem) => {
      if (menuItem.card) {
        connections.push(...menuItem.card)
      }
      if (menuItem.link) {
        connections.push(menuItem.link)
      }
      if (menuItem.see_all_link?.link) {
        connections.push(menuItem.see_all_link.link)
      }
      if (menuItem.submenu.length) {
        menuItem.submenu.forEach((subMenuItem) => {
          if (subMenuItem.links.length) {
            subMenuItem.links.forEach((link) => {
              if (link?.link) {
                connections.push(link.link)
              }
            })
          }
        })
      }
    })
  }

  return connections
}

export function getFooterConnections(refs: FooterRefDataRaw) {
  const connections: Edges<NodeRefs>[] = []
  const footerData = refs.all_footer.items[0]
  const mainLinks = footerData.main_links
  const secondaryLinks = footerData.secondary_links
  const tertiaryLinks = footerData.tertiary_links
  if (mainLinks) {
    mainLinks.forEach(({ pageConnection }) => {
      connections.push(pageConnection)
    })
  }
  secondaryLinks?.forEach(({ links }) => {
    if (links) {
      links.forEach(({ pageConnection }) => {
        connections.push(pageConnection)
      })
    }
  })
  if (tertiaryLinks) {
    tertiaryLinks.forEach(({ pageConnection }) => {
      connections.push(pageConnection)
    })
  }

  return connections
}

export function getSiteConfigConnections(refs: GetSiteConfigRefData) {
  const siteConfigData = refs.all_site_config.items[0]
  const connections: System["system"][] = []

  if (!siteConfigData) return connections

  const alertConnection = siteConfigData.sitewide_alert.alertConnection

  alertConnection.edges.forEach(({ node }) => {
    connections.push(node.system)

    const link = node.link.link
    if (link) {
      connections.push(link)
    }
    node.sidepeek_content.content.embedded_itemsConnection.edges.forEach(
      ({ node }) => {
        connections.push(node.system)
      }
    )
  })

  return connections
}

export function getAlertPhoneContactData(
  alert: AlertOutput,
  contactConfig: ContactConfig
) {
  if (alert.phoneContact) {
    const { displayText, phoneNumber, footnote } = alert.phoneContact

    return {
      displayText,
      phoneNumber: getValueFromContactConfig(phoneNumber, contactConfig),
      footnote: footnote
        ? getValueFromContactConfig(footnote, contactConfig)
        : null,
    }
  }
  return null
}
