import React, { useCallback } from "react";

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

import SectionTitle from "../../../components/SectionTitle/SectionTitle";
import { scaleH, scaleW } from "../../../utils/helperMethods";
import OnSaleProductItem from "../OnSaleProductList/OnSaleProductItem/OnSaleProductItem";
import css from "./OnSaleProductList.module.less";

const LIST_ITEM_CONF = {
  ITEM_WIDTH: 324,
  ITEM_HEIGHT: 570,
  SAPCING: 18,
};

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

export default function OnSaleProductList({
  saleName,
  saleProductInfos,
  ...rest
}) {
  const renderItem = useCallback(
    ({ index, ...rest }) => {
      return (
        <OnSaleProductItem
          index={index}
          saleProductInfos={saleProductInfos}
          {...rest}
        />
      );
    },
    [saleProductInfos]
  );

  return (
    <Container className={css.container} {...rest}>
      {saleName && <SectionTitle title={saleName} />}

      {saleProductInfos && (
        <VirtualGridList
          className={css.virtualGridList}
          dataSize={saleProductInfos.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.SAPCING)}
        />
      )}
    </Container>
  );
}
