---
import { Icon } from 'astro-icon';
import { Picture } from '@astrojs/image/components';

interface Item {
  title: string;
  description?: string;
  icon?: string;
}

export interface Props {
  title?: string;
  subtitle?: string;
  highlight?: string;
  content?: string;
  items?: Array<Item>;
  image?: string | any; // TODO: find HTMLElementProps
  isReversed?: boolean;
  isAfterContent?: boolean;
}

const {
  title = await Astro.slots.render('title'),
  subtitle = await Astro.slots.render('subtitle'),
  highlight,
  content = await Astro.slots.render('content'),
  items = [],
  image = await Astro.slots.render('image'),
  isReversed = false,
  isAfterContent = false,
} = Astro.props;
---

<section class:list={[{ 'pt-0 md:pt-0': isAfterContent }, 'bg-blue-50 dark:bg-slate-800 py-16 md:py-20']}>
  <div class="max-w-xl sm:mx-auto lg:max-w-2xl">
    {
      (title || subtitle || highlight) && (
        <div class="mb-10 md:mx-auto text-center md:mb-12 max-w-3xl">
          {highlight && (
            <p
              class="text-base text-primary dark:text-blue-200 font-semibold tracking-wide uppercase"
              set:html={highlight}
            />
          )}
          {title && (
            <h2
              class="text-4xl md:text-5xl font-bold leading-tighter tracking-tighter mb-4 font-heading"
              set:html={title}
            />
          )}

          {subtitle && (
            <p class="max-w-3xl mx-auto sm:text-center text-xl text-muted dark:text-slate-400" set:html={subtitle} />
          )}
        </div>
      )
    }
  </div>
  <div class="mx-auto max-w-7xl p-4 md:px-8">
    <div class={`md:flex ${isReversed ? 'md:flex-row-reverse' : ''} md:gap-16`}>
      <div class="md:basis-1/2 self-center">
        {content && <div class="mb-12 text-lg dark:text-slate-400" set:html={content} />}

        {
          items && (
            <div class="space-y-8">
              {items.map(({ title: title2, description, icon }) => (
                <div class="flex">
                  <div class="flex-shrink-0">
                    <div class="flex h-7 w-7 items-center justify-center rounded-full bg-primary text-gray-50">
                      <Icon name={icon ? icon : 'tabler:check'} class="w-5 h-5" />
                    </div>
                  </div>
                  <div class="ml-4">
                    {title2 && <h3 class="text-lg font-medium leading-6 dark:text-white">{title2}</h3>}
                    {description && <p class="mt-2 text-muted dark:text-slate-400" set:html={description} />}
                  </div>
                </div>
              ))}
            </div>
          )
        }
      </div>
      <div aria-hidden="true" class="mt-10 md:mt-0 md:basis-1/2">
        {
          image && (
            <div class="relative m-auto max-w-4xl">
              {typeof image === 'string' ? (
                <Fragment set:html={image} />
              ) : (
                <Picture
                  class="mx-auto w-full rounded-lg bg-gray-500 shadow-lg"
                  width={500}
                  height={500}
                  widths={[400, 768]}
                  sizes="(max-width: 768px) 100vw, 432px"
                  aspectRatio="500:500"
                  {...image}
                />
              )}
            </div>
          )
        }
      </div>
    </div>
  </div>
</section>
