import type { Setting } from '@/payload-types'
import { whatsapp } from '@/lib/site'

/** WhatsApp's own glyph, drawn inline so it needs no request and takes the colour around it. */
export function WhatsAppIcon({ size = 20 }: { size?: number }) {
  return (
    <svg viewBox="0 0 24 24" width={size} height={size} fill="currentColor" aria-hidden="true" focusable="false">
      <path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.46 1.32 4.96L2 22l5.25-1.38a9.87 9.87 0 0 0 4.79 1.22h.01c5.46 0 9.91-4.45 9.91-9.91 0-2.65-1.03-5.14-2.9-7.01A9.82 9.82 0 0 0 12.04 2Zm0 1.83c2.16 0 4.19.84 5.72 2.37a8.03 8.03 0 0 1 2.37 5.72c0 4.46-3.63 8.08-8.09 8.08a8.06 8.06 0 0 1-4.11-1.13l-.29-.17-3.05.8.81-2.98-.19-.31a8.02 8.02 0 0 1-1.23-4.29c0-4.46 3.63-8.09 8.06-8.09Zm-2.5 4.3c-.17 0-.44.06-.67.31-.23.25-.88.86-.88 2.1s.9 2.44 1.03 2.6c.13.17 1.76 2.68 4.27 3.76.6.26 1.06.41 1.42.53.6.19 1.14.16 1.57.1.48-.07 1.48-.6 1.69-1.19.21-.58.21-1.08.15-1.19-.06-.1-.23-.17-.48-.29-.25-.13-1.48-.73-1.71-.81-.23-.09-.4-.13-.56.12-.17.25-.64.81-.79.98-.15.17-.29.19-.54.06-.25-.13-1.05-.39-2-1.23-.74-.66-1.24-1.47-1.38-1.72-.15-.25-.02-.38.11-.51.11-.11.25-.29.38-.44.12-.15.16-.25.25-.42.08-.17.04-.31-.02-.44-.06-.13-.56-1.36-.77-1.86-.2-.48-.41-.42-.56-.43-.14-.01-.31-.01-.48-.01Z" />
    </svg>
  )
}

/** An envelope, drawn to the same weight and size as the WhatsApp glyph so the pair reads as a pair. */
export function MailIcon({ size = 20 }: { size?: number }) {
  return (
    <svg
      viewBox="0 0 24 24"
      width={size}
      height={size}
      fill="none"
      stroke="currentColor"
      strokeWidth={1.9}
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <rect x="2.5" y="5" width="19" height="14" rx="3" />
      <path d="m3.2 7.2 8.8 5.6 8.8-5.6" />
    </svg>
  )
}

/**
 * The ways to reach the academy, in one block: WhatsApp first (a tap opens a chat with the first line already
 * typed), then the support address, then the channel. Anything the CMS leaves empty simply does not appear.
 *
 * WhatsApp carried a glyph and the address carried none, so the two read as a link and a note rather than as
 * two ways of reaching the same people. They now carry one each, at the same size and weight.
 */
export function ContactBlock({ settings: s, className = '' }: { settings: Setting; className?: string }) {
  const wa = whatsapp(s.contact?.whatsapp, s.contact?.waText || undefined)
  const email = s.contact?.email
  const youtube = s.contact?.youtube
  if (!wa && !email && !youtube) return null
  return (
    <div className={`contact ${className}`.trim()}>
      {s.contact?.lead ? <p className="contact-lead">{s.contact.lead}</p> : null}
      <ul className="contact-ways">
        {wa ? (
          <li>
            <a className="contact-wa" href={wa.href} rel="noopener" target="_blank">
              <WhatsAppIcon />
              <span className="ltr">{wa.label}</span>
            </a>
          </li>
        ) : null}
        {email ? (
          <li>
            <a className="contact-mail" href={`mailto:${email}`}>
              <MailIcon />
              <span className="ltr">{email}</span>
            </a>
          </li>
        ) : null}
        {youtube ? (
          <li>
            <a href={youtube} rel="noopener">
              {s.cta?.youtube}
            </a>
          </li>
        ) : null}
      </ul>
    </div>
  )
}
