import type { VideoHTMLAttributes } from 'react';

import { useResolvedAssetReadUrl } from '../hooks/useResolvedAssetReadUrl';

type ResolvedAssetVideoProps = Omit<
  VideoHTMLAttributes<HTMLVideoElement>,
  'src'
> & {
  src?: string | null;
  fallbackSrc?: string | null;
  refreshKey?: string | number | null;
};

export function ResolvedAssetVideo({
  src,
  fallbackSrc,
  refreshKey,
  ...rest
}: ResolvedAssetVideoProps) {
  const { resolvedUrl } = useResolvedAssetReadUrl(src, {
    refreshKey,
  });
  const finalSrc = resolvedUrl || fallbackSrc?.trim() || '';

  if (!finalSrc) {
    return null;
  }

  return <video {...rest} src={finalSrc} />;
}
