import _extends from "@babel/runtime/helpers/extends";
import "core-js/modules/es.array.concat.js";
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.array.slice.js";
import * as React from 'react';
import { Button } from '@megafon/ui-core';
import { cnCreate, filterDataAttrs } from '@megafon/ui-helpers';
import useResolutions from "../../hooks/useResolutions";
import BlogBoxTile from "./components/BlogBoxTile";
import "./BlogBox.css";
var maxItemsCount = 4;
var cn = cnCreate('mfui-blog-box');
var BlogBox = function BlogBox(_ref) {
  var items = _ref.items,
    button = _ref.button,
    _ref$hasAlignLeft = _ref.hasAlignLeft,
    hasAlignLeft = _ref$hasAlignLeft === void 0 ? false : _ref$hasAlignLeft,
    className = _ref.className,
    classes = _ref.classes,
    dataAttrs = _ref.dataAttrs,
    rootRef = _ref.rootRef;
  var _useResolutions = useResolutions(),
    isDesktop = _useResolutions.isDesktop;
  var visibleItems = items.slice(0, maxItemsCount);
  var itemsCount = visibleItems.length;
  var isRow = isDesktop && itemsCount < maxItemsCount;
  var isAlignLeft = isRow && hasAlignLeft;
  var getView = function getView(isCompact, i) {
    if (isCompact) {
      return 'compact';
    }
    if (i === 0) {
      return 'full';
    }
    return 'thumbnail';
  };
  return /*#__PURE__*/React.createElement("div", _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.root), {
    className: cn({
      'align-left': isAlignLeft
    }, [className, classes === null || classes === void 0 ? void 0 : classes.root]),
    ref: rootRef
  }), /*#__PURE__*/React.createElement("div", _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.list), {
    className: cn('list', {
      row: isRow,
      'row-size': isRow && "".concat(itemsCount)
    })
  }), visibleItems.map(function (item, i) {
    var view = getView(isRow, i);
    return /*#__PURE__*/React.createElement("div", _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.item, i + 1), {
      className: cn('item', {
        view: view
      }, [classes === null || classes === void 0 ? void 0 : classes.item]),
      key: "".concat(item.title, "_").concat(i)
    }), /*#__PURE__*/React.createElement(BlogBoxTile, _extends({}, item, {
      view: view,
      classes: {
        title: classes === null || classes === void 0 ? void 0 : classes.itemTitle
      }
    })));
  })), (button === null || button === void 0 ? void 0 : button.title) && /*#__PURE__*/React.createElement("div", {
    className: cn('button-wrapper')
  }, /*#__PURE__*/React.createElement(Button, {
    dataAttrs: {
      root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.button
    },
    className: cn('button', [classes === null || classes === void 0 ? void 0 : classes.button]),
    href: button.href,
    target: button.target,
    rel: button.rel
  }, button.title)));
};
export default BlogBox;