import odooApi from '@/core/api/odooApi';
import Image from 'next/image';
import { useQuery } from 'react-query';
import Link from '../Link/Link';
import SmoothRender from '~/components/ui/smooth-render';

const TopBanner = () => {
  const topBanner = useQuery({
    queryKey: 'topBanner',
    queryFn: async () => await odooApi('GET', '/api/v1/banner?type=top-banner'),
    refetchOnWindowFocus: false,
  });

  const backgroundColor = topBanner.data?.[0]?.backgroundColor || 'transparent';
  const hasData = topBanner.data?.length > 0;
  const data = topBanner.data?.[0] || null;

  return (
    <SmoothRender
      isLoaded={hasData}
      height={36}
      duration={700}
      delay={500}
      style={{ backgroundColor }}
    >
      <Link href={data?.url}>
        <Image
          src={data?.image}
          alt={data?.name}
          width={1440}
          height={40}
          className='object-cover object-center h-full mx-auto'
        />
      </Link>
    </SmoothRender>
  );
};

export default TopBanner;
