"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.slice.js");
var React = _interopRequireWildcard(require("react"));
var _uiCore = require("@megafon/ui-core");
var _uiHelpers = require("@megafon/ui-helpers");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var MAX_GRID_ITEMS_LENGTH = 8;
var slidesSettings = (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, _uiHelpers.breakpoints.MOBILE_SMALL_START, {
  slidesPerView: 2,
  spaceBetween: 16
}), _uiHelpers.breakpoints.MOBILE_MIDDLE_START, {
  slidesPerView: 3,
  spaceBetween: 20
}), _uiHelpers.breakpoints.DESKTOP_SMALL_START, {
  slidesPerView: 4,
  spaceBetween: 20
});
var testIdPrefix = 'Partners';
var cn = (0, _uiHelpers.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(_uiCore.Tile, {
      href: href,
      rel: rel,
      shadowLevel: "default",
      radius: radius,
      isInteractive: !!href,
      className: cn('tile'),
      dataAttrs: {
        root: (0, _extends2["default"])({}, (0, _uiHelpers.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(_uiCore.Grid, {
      guttersLeft: "medium",
      guttersBottom: "medium"
    }, items.map(function (item, i) {
      return /*#__PURE__*/React.createElement(_uiCore.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(_uiCore.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", (0, _extends2["default"])({
    ref: rootRef,
    className: cn([root, className])
  }, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.root)), items.length > MAX_GRID_ITEMS_LENGTH ? renderCarousel() : renderGrid());
};
var _default = exports["default"] = Partners;