import React, { useCallback } from "react";

import { useSelector } from "react-redux";

import { VirtualGridList } from "@enact/sandstone/VirtualList";
import SpotlightContainerDecorator from "@enact/spotlight/SpotlightContainerDecorator";

import TItemCard from "../../../../../components/TItemCard/TItemCard";
import { scaleH, scaleW } from "../../../../../utils/helperMethods";
import css from "./ShowProductContents.module.less";

const LIST_ITEM_CONF = {
  ITEM_WIDTH: 324,
  ITEM_HEIGHT: 438,
  SPACING: 18,
};

const Container = SpotlightContainerDecorator(
  { enterTo: "last-focused" },
  "div"
);

export default function ShowProductContents() {
  const showProductContentDatas = useSelector(
    (state) => state.main.subCategoryData?.topShowInfo
  );
  const { productInfos } = showProductContentDatas || {};

  const renderItem = useCallback(
    ({ index, ...rest }) => {
      const productInfo = productInfos[index];
      const { imgUrl, prdtId, prdtNm, priceInfo } = productInfo;

      return (
        <TItemCard
          {...rest}
          key={prdtId}
          imageAlt={prdtId}
          imageSource={imgUrl}
          priceInfo={priceInfo}
          productName={prdtNm}
        />
      );
    },
    [productInfos]
  );

  return (
    <Container className={css.container}>
      {productInfos && productInfos.length > 0 && (
        <VirtualGridList
          classname={css.grid}
          dataSize={productInfos.length}
          direction="horizontal"
          horizontalScrollbar="hidden"
          itemRenderer={renderItem}
          itemSize={{
            minWidth: scaleW(LIST_ITEM_CONF.ITEM_WIDTH),
            minHeight: scaleH(LIST_ITEM_CONF.ITEM_HEIGHT),
          }}
          noScrollByWheel
          scrollMode="translate"
          spacing={scaleW(LIST_ITEM_CONF.SPACING)}
        />
      )}
    </Container>
  );
}
