import React, { useCallback } from "react";

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

import SearchItemCard from "./SearchItemCard/SearchItemCard";
import css from "./SearchItemResults.module.less";

const LIST_ITEM_CONF = {
  ITEM_WIDTH: 561 * 2,
  ITEM_HEIGHT: 150 * 2,
  SPACING: 30 * 2,
};

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

export default function SearchItemResults({ itemDatas, itemCount, ...rest }) {
  const renderItem = useCallback(
    ({ index, ...rest }) => {
      const itemData = itemDatas[index];

      return (
        <SearchItemCard {...rest} key={itemDatas.contentId} {...itemData} />
      );
    },
    [itemDatas]
  );

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