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 { Grid, GridColumn, Tile, Carousel } from '@megafon/ui-core';
import { breakpoints, cnCreate, filterDataAttrs } from '@megafon/ui-helpers';
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-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$radius = _ref.radius,
    radius = _ref$radius === void 0 ? 'default' : _ref$radius,
    onChange = _ref.onChange,
    onNextClick = _ref.onNextClick,
    onPrevClick = _ref.onPrevClick;
  var renderItem = React.useCallback(function (item) {
    var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    if (!item) {
      return null;
    }
    var src = item.src,
      href = item.href,
      alt = item.alt,
      rel = item.rel;
    return /*#__PURE__*/React.createElement(Tile, {
      href: href,
      rel: rel,
      shadowLevel: "default",
      radius: radius,
      isInteractive: !!href,
      className: cn('tile'),
      dataAttrs: {
        root: _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.item, index + 1))
      }
    }, /*#__PURE__*/React.createElement("div", {
      className: cn('tile-inner', [itemClass]),
      "data-testid": "".concat(testIdPrefix, "-tile-inner")
    }, /*#__PURE__*/React.createElement("div", {
      className: cn('img-wrapper')
    }, /*#__PURE__*/React.createElement("img", {
      src: src,
      alt: alt,
      className: cn('tile-img')
    }))));
  }, [radius, dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.item, itemClass]);
  var renderGrid = React.useCallback(function () {
    return /*#__PURE__*/React.createElement(Grid, {
      guttersLeft: "medium",
      guttersBottom: "medium"
    }, items.map(function (item, i) {
      return /*#__PURE__*/React.createElement(GridColumn, {
        key: i + item.src,
        all: "3",
        mobile: "6"
      }, 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;