import odooApi from '@/core/api/odooApi';
import Image from 'next/image';
import { useQuery } from 'react-query';
import clsxm from '~/libs/clsxm';
import Link from '../Link/Link';

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;

  return (
    <div
      style={{ backgroundColor }}
      className={clsxm('overflow-y-hidden transition-all duration-700', {
        'h-[40px]': hasData,
        'h-0': topBanner.isLoading,
      })}
    >
      {hasData && (
        <Link href={topBanner.data[0]?.url}>
          <Image
            src={topBanner.data[0].image}
            alt={topBanner.data[0].name}
            width={1440}
            height={40}
            className='object-cover object-center h-full mx-auto'
          />
        </Link>
      )}
    </div>
  );
};

export default TopBanner;
