import type { CSSProperties, ReactNode, SVGProps } from 'react';
import { WHATSAPP_GLYPH } from '@/site/lib/whatsapp-glyph';

/**
 * Inline icons of the public site (no icon library is shipped). Stroke
 * icons on a 24×24 grid that inherit `currentColor`; decorative by default.
 *
 * The strokes of the icons that mark content (tick, shield, warning, the
 * contact details) declare a length of 1 and carry the class `sk`: inside
 * a <Reveal> they draw themselves as it comes into view (site.css →
 * "Parts that enter one after another"); anywhere else nothing changes.
 */
const second = { '--i': 4 } as CSSProperties;

type IconProps = SVGProps<SVGSVGElement> & { title?: string };

function Icon({
    title,
    children,
    ...props
}: IconProps & { children: ReactNode }) {
    return (
        <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 24 24"
            width="1em"
            height="1em"
            fill="none"
            stroke="currentColor"
            strokeWidth="1.5"
            strokeLinecap="round"
            strokeLinejoin="round"
            aria-hidden={title ? undefined : true}
            role={title ? 'img' : undefined}
            {...props}
        >
            {title ? <title>{title}</title> : null}
            {children}
        </svg>
    );
}

export const ArrowRight = (props: IconProps) => (
    <Icon {...props}>
        <path d="M4 12h16M14 6l6 6-6 6" />
    </Icon>
);

export const ArrowUpRight = (props: IconProps) => (
    <Icon {...props}>
        <path d="M7 17 17 7M8 7h9v9" />
    </Icon>
);

export const ArrowDown = (props: IconProps) => (
    <Icon {...props}>
        <path d="M12 4v16M6 14l6 6 6-6" />
    </Icon>
);

export const ChevronDown = (props: IconProps) => (
    <Icon {...props}>
        <path d="m6 9 6 6 6-6" />
    </Icon>
);

export const ChevronRight = (props: IconProps) => (
    <Icon {...props}>
        <path d="m9 6 6 6-6 6" />
    </Icon>
);

export const Plus = (props: IconProps) => (
    <Icon {...props}>
        <path d="M12 5v14M5 12h14" />
    </Icon>
);

export const Star = (props: IconProps) => (
    <Icon {...props}>
        <path
            d="m12 3.5 2.6 5.4 5.9.8-4.3 4.1 1 5.9L12 16.9l-5.2 2.8 1-5.9-4.3-4.1 5.9-.8L12 3.5Z"
            fill="currentColor"
            stroke="none"
        />
    </Icon>
);

export const Check = (props: IconProps) => (
    <Icon {...props}>
        <path pathLength={1} className="sk" d="m5 12.5 4.5 4.5L19 7.5" />
    </Icon>
);

export const Search = (props: IconProps) => (
    <Icon {...props}>
        <circle cx="11" cy="11" r="6.5" />
        <path d="m16 16 4.5 4.5" />
    </Icon>
);

export const Menu = (props: IconProps) => (
    <Icon {...props}>
        <path d="M4 8h16M4 16h16" />
    </Icon>
);

export const Close = (props: IconProps) => (
    <Icon {...props}>
        <path d="M6 6l12 12M18 6 6 18" />
    </Icon>
);

export const Phone = (props: IconProps) => (
    <Icon {...props}>
        <path
            pathLength={1}
            className="sk"
            d="M5 4h3.5l1.7 4.3-2.1 1.5a12 12 0 0 0 6.1 6.1l1.5-2.1L20 15.5V19a1.5 1.5 0 0 1-1.6 1.5A16 16 0 0 1 3.5 5.6 1.5 1.5 0 0 1 5 4Z"
        />
    </Icon>
);

export const Mail = (props: IconProps) => (
    <Icon {...props}>
        <rect
            pathLength={1}
            className="sk"
            x="3"
            y="5"
            width="18"
            height="14"
            rx="2.5"
        />
        <path pathLength={1} className="sk" style={second} d="m4 7.5 8 6 8-6" />
    </Icon>
);

export const MapPin = (props: IconProps) => (
    <Icon {...props}>
        <path
            pathLength={1}
            className="sk"
            d="M12 21s7-6.2 7-11.5a7 7 0 1 0-14 0C5 14.8 12 21 12 21Z"
        />
        <circle
            pathLength={1}
            className="sk"
            style={second}
            cx="12"
            cy="9.5"
            r="2.5"
        />
    </Icon>
);

export const Clock = (props: IconProps) => (
    <Icon {...props}>
        <circle pathLength={1} className="sk" cx="12" cy="12" r="8.5" />
        <path pathLength={1} className="sk" style={second} d="M12 7.5V12l3 2" />
    </Icon>
);

export const Alert = (props: IconProps) => (
    <Icon {...props}>
        <path pathLength={1} className="sk" d="M12 4 2.8 19.5h18.4L12 4Z" />
        <path
            pathLength={1}
            className="sk"
            style={second}
            d="M12 10v4.5M12 17.2v.1"
        />
    </Icon>
);

export const Shield = (props: IconProps) => (
    <Icon {...props}>
        <path
            pathLength={1}
            className="sk"
            d="M12 3 4.5 6v5.5c0 4.6 3.1 8 7.5 9.5 4.4-1.5 7.5-4.9 7.5-9.5V6L12 3Z"
        />
        <path
            pathLength={1}
            className="sk"
            style={second}
            d="m9 12 2.2 2.2L15.2 10"
        />
    </Icon>
);

/** WhatsApp's mark: filled, unlike the stroke icons, so it reads at a glance. */
export const WhatsApp = (props: IconProps) => (
    <Icon fill="currentColor" stroke="none" {...props}>
        <path fillRule="evenodd" clipRule="evenodd" d={WHATSAPP_GLYPH} />
    </Icon>
);

/** Social network glyphs, keyed by the names in App\Settings\SiteSettings::SOCIAL. */
const socialPaths: Record<string, ReactNode> = {
    instagram: (
        <>
            <rect x="4" y="4" width="16" height="16" rx="4.5" />
            <circle cx="12" cy="12" r="3.6" />
            <path d="M16.8 7.2v.1" />
        </>
    ),
    facebook: (
        <path d="M14 8.5V7c0-.8.4-1.3 1.4-1.3H17V3h-2.3C12.3 3 11 4.5 11 6.8v1.7H8.5v2.9H11V21h3v-9.6h2.5l.4-2.9H14Z" />
    ),
    youtube: (
        <>
            <rect x="3" y="6" width="18" height="12" rx="4" />
            <path d="m10.5 9.5 4 2.5-4 2.5v-5Z" />
        </>
    ),
    linkedin: (
        <>
            <rect x="4" y="4" width="16" height="16" rx="2.5" />
            <path d="M8 10.5v6M8 7.6v.1M11.5 16.5v-6M11.5 13c0-1.5 1-2.5 2.3-2.5s2.2.9 2.2 2.5v3.5" />
        </>
    ),
    tiktok: (
        <path d="M14 4v10.5a3.5 3.5 0 1 1-3.5-3.5M14 4c.3 2.5 1.9 4.2 4.5 4.5" />
    ),
    x: <path d="M5 4l14 16M19 4 5 20" />,
};

export function SocialIcon({
    network,
    ...props
}: IconProps & { network: string }) {
    return <Icon {...props}>{socialPaths[network] ?? socialPaths.x}</Icon>;
}
