import type { ElementType, ReactNode } from 'react';
import { Reveal } from '@/site/components/motion/reveal';
import { cx } from '@/site/lib/cx';
import { Em } from './em';
import { Term } from './term';

/**
 * Small uppercase label above a heading. A plain text goes through Term,
 * so an English term in it is not upper-cased by Turkish rules.
 */
export function Eyebrow({
    children,
    className,
    as: Tag = 'p',
}: {
    children: ReactNode;
    className?: string;
    as?: ElementType;
}) {
    return (
        <Tag
            className={cx(
                'flex items-center gap-3 text-eyebrow font-semibold text-accent-fg uppercase',
                className,
            )}
        >
            <span
                aria-hidden="true"
                className="h-px w-8 bg-current opacity-60"
            />
            {typeof children === 'string' ? <Term text={children} /> : children}
        </Tag>
    );
}

type SectionHeadingProps = {
    eyebrow?: ReactNode;
    /** Heading text; *asterisks* mark the italic emphasis. */
    title: string;
    lead?: string;
    /** Heading level — keep the page outline in order (one h1 per page). */
    as?: 'h1' | 'h2' | 'h3';
    size?: 'display-2' | 'display-3' | 'display-4';
    align?: 'start' | 'center';
    className?: string;
    id?: string;
};

const sizes = {
    'display-2': 'text-display-2',
    'display-3': 'text-display-3',
    'display-4': 'text-display-4',
};

/** Eyebrow + display heading + optional lead, revealed on scroll. */
export function SectionHeading({
    eyebrow,
    title,
    lead,
    as: Tag = 'h2',
    size = 'display-3',
    align = 'start',
    className,
    id,
}: SectionHeadingProps) {
    return (
        <div
            className={cx(
                'flex max-w-3xl flex-col gap-6',
                align === 'center' && 'mx-auto items-center text-center',
                className,
            )}
        >
            {eyebrow ? (
                <Reveal>
                    <Eyebrow>{eyebrow}</Eyebrow>
                </Reveal>
            ) : null}
            <Reveal delay={60}>
                <Tag
                    id={id}
                    className={cx('display-em font-display', sizes[size])}
                >
                    <Em text={title} />
                </Tag>
            </Reveal>
            {lead ? (
                <Reveal delay={120}>
                    <p className="text-lead text-soft max-w-2xl">{lead}</p>
                </Reveal>
            ) : null}
        </div>
    );
}
