import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.array.slice.js";
import * as React from 'react';
import { Carousel } from '@megafon/ui-core';
import { breakpoints, cnCreate, filterDataAttrs } from '@megafon/ui-helpers';
import PartnersItem from "./components/PartnersItem/PartnersItem";
import "./Partners.css";
var MAX_GRID_ITEMS_LENGTH = 8;
var slidesSettings = _defineProperty(_defineProperty(_defineProperty({}, breakpoints.MOBILE_SMALL_START, {
  slidesPerView: 2,
  spaceBetween: 16
}), breakpoints.MOBILE_MIDDLE_START, {
  slidesPerView: 3,
  spaceBetween: 20
}), breakpoints.DESKTOP_SMALL_START, {
  slidesPerView: 4,
  spaceBetween: 20
});
var testIdPrefix = 'Partners';
var cn = cnCreate('mfui-9-partners');
var Partners = function Partners(_ref) {
  var rootRef = _ref.rootRef,
    _ref$classes = _ref.classes,
    _ref$classes2 = _ref$classes === void 0 ? {} : _ref$classes,
    root = _ref$classes2.root,
    itemClass = _ref$classes2.itemClass,
    slide = _ref$classes2.slide,
    dataAttrs = _ref.dataAttrs,
    className = _ref.className,
    items = _ref.items,
    _ref$background = _ref.background,
    background = _ref$background === void 0 ? 'shadow' : _ref$background,
    onChange = _ref.onChange,
    onNextClick = _ref.onNextClick,
    onPrevClick = _ref.onPrevClick;
  var renderItem = React.useCallback(function (item, index) {
    if (!item) {
      return null;
    }
    var src = item.src,
      alt = item.alt,
      rel = item.rel,
      href = item.href;
    return /*#__PURE__*/React.createElement(PartnersItem, {
      className: itemClass,
      src: src,
      alt: alt,
      rel: rel,
      href: href,
      background: background,
      dataAttrs: {
        root: _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.item, index + 1))
      }
    });
  }, [background, dataAttrs, itemClass]);
  var renderGrid = React.useCallback(function () {
    return /*#__PURE__*/React.createElement("div", {
      className: cn('grid')
    }, items.map(function (item, i) {
      return /*#__PURE__*/React.createElement("div", {
        className: cn('grid-item'),
        key: i + item.src
      }, renderItem(item, i));
    }));
  }, [items, renderItem]);
  var renderCarousel = React.useCallback(function () {
    var halfItemsLength = Math.ceil(items.length / 2);
    var topRow = items.slice(0, halfItemsLength);
    var bottomRow = items.slice(halfItemsLength);
    return /*#__PURE__*/React.createElement(Carousel, {
      classes: {
        slide: cn('slide', [slide])
      },
      slidesSettings: slidesSettings,
      onChange: onChange,
      onNextClick: onNextClick,
      onPrevClick: onPrevClick,
      dataAttrs: {
        prev: {
          'data-testid': "".concat(testIdPrefix, "-prev")
        },
        next: {
          'data-testid': "".concat(testIdPrefix, "-next")
        }
      }
    }, topRow.map(function (item, i) {
      return /*#__PURE__*/React.createElement("div", {
        key: i + item.src,
        className: cn('slide')
      }, renderItem(item, i), renderItem(bottomRow[i], i));
    }));
  }, [items, slide, onChange, onNextClick, onPrevClick, renderItem]);
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: rootRef,
    className: cn([root, className])
  }, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.root)), items.length > MAX_GRID_ITEMS_LENGTH ? renderCarousel() : renderGrid());
};
export default Partners;