import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/es.array.map.js";
import * as React from 'react';
import { Grid, GridColumn } from '@megafon/ui-core';
import { cnCreate, breakpoints, filterDataAttrs } from '@megafon/ui-helpers';
import throttle from 'lodash.throttle';
import throttleTime from "../../constants/throttleTime";
import BenefitsIconsTile from "./BenefitsIconsTile";
import getMultiColumnConfig, { getOneColumnConfig } from "./helpers";
import { IconPositionEnum, ItemsAlignEnum } from "./types";
import "./style/BenefitsIcons.css";
var testIdPrefix = 'BenefitsIcons';
var cn = cnCreate('mfui-benefits-icons');
var BenefitsIcons = function BenefitsIcons(_ref) {
  var dataAttrs = _ref.dataAttrs,
    rootRef = _ref.rootRef,
    _ref$iconPosition = _ref.iconPosition,
    iconPosition = _ref$iconPosition === void 0 ? 'left-top' : _ref$iconPosition,
    _ref$background = _ref.background,
    background = _ref$background === void 0 ? 'transparent' : _ref$background,
    _ref$inOneColumn = _ref.inOneColumn,
    inOneColumn = _ref$inOneColumn === void 0 ? false : _ref$inOneColumn,
    items = _ref.items,
    className = _ref.className,
    _ref$classes = _ref.classes,
    classes = _ref$classes === void 0 ? {} : _ref$classes;
  var _React$useState = React.useState(iconPosition === IconPositionEnum.CENTER_TOP ? 'center' : 'left'),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    itemsAlign = _React$useState2[0],
    setItemsAlign = _React$useState2[1];
  var _React$useState3 = React.useState(iconPosition),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    localIconPosition = _React$useState4[0],
    setLocalIconPosition = _React$useState4[1];
  var resizeHandler = React.useCallback(function () {
    if (window.innerWidth <= breakpoints.MOBILE_MIDDLE_END) {
      setItemsAlign(ItemsAlignEnum.CENTER);
      setLocalIconPosition(IconPositionEnum.CENTER_TOP);
    } else {
      setItemsAlign(ItemsAlignEnum.LEFT);
      setLocalIconPosition(iconPosition);
    }
  }, [iconPosition]);
  React.useEffect(function () {
    var resizeHandlerThrottled = throttle(resizeHandler, throttleTime.resize);
    if (inOneColumn) {
      resizeHandler();
      window.addEventListener('resize', resizeHandlerThrottled);
    }
    return function () {
      window.removeEventListener('resize', resizeHandlerThrottled);
    };
  }, [iconPosition, inOneColumn, resizeHandler]);
  return /*#__PURE__*/React.createElement("div", _extends({}, filterDataAttrs(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.root), {
    className: cn({
      'one-column': inOneColumn,
      background: background
    }, [className, classes.root]),
    ref: rootRef
  }), /*#__PURE__*/React.createElement("div", {
    className: cn('inner')
  }, /*#__PURE__*/React.createElement(Grid, {
    className: classes.grid,
    guttersLeft: "medium",
    hAlign: itemsAlign,
    dataAttrs: {
      root: {
        'data-testid': "".concat(testIdPrefix, "-grid-root")
      },
      container: {
        'data-testid': "".concat(testIdPrefix, "-grid-container")
      }
    }
  }, items.map(function (_ref2, i) {
    var title = _ref2.title,
      text = _ref2.text,
      icon = _ref2.icon,
      alt = _ref2.alt,
      itemClasses = _ref2.classes,
      itemDataAttrs = _ref2.dataAttrs;
    var columnConfig = inOneColumn ? getOneColumnConfig(iconPosition) : getMultiColumnConfig(iconPosition, items.length, i);
    return /*#__PURE__*/React.createElement(GridColumn, _extends({
      className: cn('column', [classes.gridColumn]),
      key: i,
      dataAttrs: {
        root: {
          'data-testid': "".concat(testIdPrefix, "-grid-column")
        }
      }
    }, columnConfig), /*#__PURE__*/React.createElement(BenefitsIconsTile, {
      className: cn('tile', [classes.item]),
      classes: itemClasses,
      dataAttrs: itemDataAttrs,
      title: title,
      text: text,
      icon: icon,
      background: background,
      iconPosition: localIconPosition,
      alt: alt
    }));
  }))));
};
export default BenefitsIcons;