import type { ReactNode } from 'react';
import { cx } from '@/site/lib/cx';
import type { Media } from '@/site/types';
import { LazyVideo } from './lazy-video';
import { Picture } from './picture';

type MediaSlotProps = {
    /** The real photo or video, when the clinic has provided one. */
    media?: Media | null;
    /** Generative artwork shown until then. */
    fallback: ReactNode;
    /** First-screen media: load it right away (see Picture). */
    priority?: boolean;
    /** Displayed width of a photo, for its srcset (see Picture). */
    sizes?: string;
    /**
     * Frame of the real media: size or aspect ratio, rounding, position.
     * The media always covers the frame.
     */
    className?: string;
};

/**
 * A place for a photograph or a video. While the clinic's own material is
 * missing it shows the `fallback` artwork; as soon as a file is added to
 * the media library (App\Content\MediaLibrary) the real thing appears — no
 * code change needed.
 */
export function MediaSlot({
    media,
    fallback,
    priority,
    sizes,
    className,
}: MediaSlotProps) {
    if (!media) {
        return <>{fallback}</>;
    }

    return (
        <div className={cx('bg-stage-2 overflow-hidden', className)}>
            {media.kind === 'video' ? (
                <LazyVideo media={media} priority={priority} />
            ) : (
                <Picture media={media} priority={priority} sizes={sizes} />
            )}
        </div>
    );
}
