"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
require("core-js/modules/es.array.map.js");
var React = _interopRequireWildcard(require("react"));
var _uiCore = require("@megafon/ui-core");
var _uiHelpers = require("@megafon/ui-helpers");
var _lodash = _interopRequireDefault(require("lodash.throttle"));
var _throttleTime = _interopRequireDefault(require("../../constants/throttleTime"));
var _BenefitsIconsTile = _interopRequireDefault(require("./BenefitsIconsTile"));
var _helpers = _interopRequireWildcard(require("./helpers"));
var _types = require("./types");
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 testIdPrefix = 'BenefitsIcons';
var cn = (0, _uiHelpers.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 === _types.IconPositionEnum.CENTER_TOP ? 'center' : 'left'),
    _React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
    itemsAlign = _React$useState2[0],
    setItemsAlign = _React$useState2[1];
  var _React$useState3 = React.useState(iconPosition),
    _React$useState4 = (0, _slicedToArray2["default"])(_React$useState3, 2),
    localIconPosition = _React$useState4[0],
    setLocalIconPosition = _React$useState4[1];
  var resizeHandler = React.useCallback(function () {
    if (window.innerWidth <= _uiHelpers.breakpoints.MOBILE_MIDDLE_END) {
      setItemsAlign(_types.ItemsAlignEnum.CENTER);
      setLocalIconPosition(_types.IconPositionEnum.CENTER_TOP);
    } else {
      setItemsAlign(_types.ItemsAlignEnum.LEFT);
      setLocalIconPosition(iconPosition);
    }
  }, [iconPosition]);
  React.useEffect(function () {
    var resizeHandlerThrottled = (0, _lodash["default"])(resizeHandler, _throttleTime["default"].resize);
    if (inOneColumn) {
      resizeHandler();
      window.addEventListener('resize', resizeHandlerThrottled);
    }
    return function () {
      window.removeEventListener('resize', resizeHandlerThrottled);
    };
  }, [iconPosition, inOneColumn, resizeHandler]);
  return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({}, (0, _uiHelpers.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(_uiCore.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 ? (0, _helpers.getOneColumnConfig)(iconPosition) : (0, _helpers["default"])(iconPosition, items.length, i);
    return /*#__PURE__*/React.createElement(_uiCore.GridColumn, (0, _extends2["default"])({
      className: cn('column', [classes.gridColumn]),
      key: i,
      dataAttrs: {
        root: {
          'data-testid': "".concat(testIdPrefix, "-grid-column")
        }
      }
    }, columnConfig), /*#__PURE__*/React.createElement(_BenefitsIconsTile["default"], {
      className: cn('tile', [classes.item]),
      classes: itemClasses,
      dataAttrs: itemDataAttrs,
      title: title,
      text: text,
      icon: icon,
      background: background,
      iconPosition: localIconPosition,
      alt: alt
    }));
  }))));
};
var _default = exports["default"] = BenefitsIcons;