import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "core-js/modules/es.array.map.js";
import * as React from 'react';
import { Carousel } from '@megafon/ui-core';
import { breakpoints, cnCreate, filterDataAttrs } from '@megafon/ui-helpers';
import useResolutions from "../../hooks/useResolutions";
import CardOnBackground from "./components/CardOnBackground";
import useUniformHeights from "./useUniformHeights";
import "./CardsOnBackground.css";
var getSlidesSettings = function getSlidesSettings(count) {
  return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, breakpoints.MOBILE_SMALL_START, {
    slidesPerView: 1,
    spaceBetween: 16
  }), breakpoints.MOBILE_BIG_START, {
    slidesPerView: 2,
    spaceBetween: 20
  }), breakpoints.DESKTOP_MIDDLE_START, {
    slidesPerView: count < 3 ? count : 3,
    spaceBetween: 20
  }), breakpoints.DESKTOP_BIG_START, {
    slidesPerView: count < 4 ? count : 4,
    spaceBetween: 20
  });
};
var cn = cnCreate('mfui-9-cards-on-background');
var CardsOnBackground = function CardsOnBackground(_ref2) {
  var items = _ref2.items,
    _ref2$background = _ref2.background,
    background = _ref2$background === void 0 ? 'gray' : _ref2$background,
    _ref2$hasCarousel = _ref2.hasCarousel,
    hasCarousel = _ref2$hasCarousel === void 0 ? false : _ref2$hasCarousel,
    _ref2$singleCardAlign = _ref2.singleCardAlign,
    singleCardAlign = _ref2$singleCardAlign === void 0 ? 'left' : _ref2$singleCardAlign,
    className = _ref2.className,
    _ref2$classes = _ref2.classes,
    classes = _ref2$classes === void 0 ? {} : _ref2$classes,
    dataAttrs = _ref2.dataAttrs,
    onCarouselChange = _ref2.onCarouselChange,
    rootRef = _ref2.rootRef;
  var cardsCount = items.length;
  var _useResolutions = useResolutions(),
    isMobile = _useResolutions.isMobile;
  var _useUniformHeights = useUniformHeights(cardsCount, {
      disabled: isMobile
    }),
    maxHeight = _useUniformHeights.maxHeight,
    getRef = _useUniformHeights.getRef;
  var renderCard = function renderCard(item, index) {
    return /*#__PURE__*/React.createElement(CardOnBackground, _extends({}, item, !!maxHeight && {
      headerHeight: maxHeight
    }, {
      headerRef: getRef(index),
      classes: {
        root: classes.item,
        title: classes.itemTitle,
        button: classes.itemButton,
        extraButton: classes.itemExtraButton
      },
      dataAttrs: {
        root: _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.item, index + 1))
      }
    }));
  };
  var renderContent = function renderContent() {
    if (hasCarousel) {
      return /*#__PURE__*/React.createElement(Carousel, {
        slidesSettings: getSlidesSettings(cardsCount),
        arrowPosition: "inside",
        onChange: onCarouselChange
      }, items.map(function (item, i) {
        return /*#__PURE__*/React.createElement(React.Fragment, {
          key: item.title + i
        }, renderCard(item, i));
      }));
    }
    return /*#__PURE__*/React.createElement("div", _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.grid), {
      className: cn('grid', {
        count: "".concat(cardsCount),
        'single-align': singleCardAlign
      })
    }), items.map(function (item, i) {
      return /*#__PURE__*/React.createElement("div", {
        className: cn('grid-item'),
        key: item.title + i
      }, renderCard(item, i));
    }));
  };
  return /*#__PURE__*/React.createElement("div", _extends({
    className: cn({
      background: background,
      carousel: hasCarousel
    }, [className, classes.root])
  }, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.root), {
    ref: rootRef
  }), renderContent());
};
export default CardsOnBackground;