import dynamic from 'next/dynamic'
import Seo from '@/core/components/Seo'
import ImageSkeleton from '@/core/components/elements/Skeleton/ImageSkeleton'
import PopularProductSkeleton from '@/lib/home/components/Skeleton/PopularProductSkeleton'
import MobileView from '@/core/components/views/MobileView'
import DesktopView from '@/core/components/views/DesktopView'
import { useRef } from 'react'

const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'))

const HeroBanner = dynamic(() => import('@/lib/home/components/HeroBanner'), {
  loading: () => <ImageSkeleton />
})

const PreferredBrand = dynamic(() => import('@/lib/home/components/PreferredBrand'), {
  loading: () => <PopularProductSkeleton />
})

const PopularProduct = dynamic(() => import('@/lib/home/components/PopularProduct'), {
  loading: () => <PopularProductSkeleton />
})

const CategoryHomeId = dynamic(() => import('@/lib/home/components/CategoryHomeId'), {
  loading: () => <PopularProductSkeleton />
})

export default function Home() {
  const bannerRef = useRef(null)
  const wrapperRef = useRef(null)

  const handleOnLoad = () => {
    wrapperRef.current.style.height =
      bannerRef.current.querySelector(':first-child').clientHeight + 'px'
  }

  return (
    <BasicLayout>
      <Seo title='Beranda - Indoteknik' />
      <DesktopView>
        <div className='container mx-auto'>
          <div
            className='flex h-[360px]'
            ref={wrapperRef}
            onLoad={handleOnLoad}
          >
            <div className='w-3/12'></div>
            <div
              className='w-6/12 px-1'
              ref={bannerRef}
            >
              <HeroBanner />
            </div>
            <div className='w-3/12'>
              <PopularProduct />
            </div>
          </div>

          <div className='my-16 flex flex-col gap-y-10'>
            <PreferredBrand />
            <CategoryHomeId />
          </div>
        </div>
      </DesktopView>

      <MobileView>
        <HeroBanner />
        <div className='flex flex-col gap-y-6 my-6'>
          <PreferredBrand />
          <PopularProduct />
          <CategoryHomeId />
        </div>
      </MobileView>
    </BasicLayout>
  )
}
