// jshint esversion: 6
'use strict';

import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import Row from 'react-bootstrap/lib/Row';
import { Banner } from './Banner';
var INDEX_NOT_SET = -1;
var PROPERTY_NOT_SET = null;
export var TiledGrid =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(TiledGrid, _React$Component);

  function TiledGrid(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    var initialIndex = INDEX_NOT_SET;

    if (props.currentItemIndex !== PROPERTY_NOT_SET) {
      initialIndex = props.currentItemIndex;
    }

    _this.state = {
      currentItemIndex: initialIndex
    };
    return _this;
  }

  var _proto = TiledGrid.prototype;

  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    if (nextProps.hasOwnProperty('currentItemIndex') && nextProps.currentItemIndex !== PROPERTY_NOT_SET) {
      this.setCurrentItemIndex(nextProps.currentItemIndex);
    }
  };

  _proto.setCurrentItemIndex = function setCurrentItemIndex(index) {
    if (this.state.currentItemIndex !== index) {
      this.setState({
        currentItemIndex: index
      });

      if (this.props.onChange) {
        this.props.onChange(index);
      }
    }
  };

  _proto.getColSize = function getColSize() {
    var cols = this.props.cols;
    var GRID_CAPACITY = 12;
    var colSize = null;

    if (GRID_CAPACITY % cols === 0) {
      colSize = GRID_CAPACITY / cols;
    } else {
      throw new RangeError('Invalid cols parameter. Cols must be 1, 2, 3, 4, 6, 12');
    }

    return colSize;
  };

  _proto.getRowNumbers = function getRowNumbers() {
    var items = this.props.items;
    var cols = this.props.cols;
    return Math.ceil(items.length / cols);
  };

  _proto.getActiveRow = function getActiveRow() {
    var cols = this.props.cols;
    var currentItemIndex = this.state.currentItemIndex;

    if (currentItemIndex === INDEX_NOT_SET) {
      return null;
    }

    return Math.ceil((currentItemIndex + 1) / cols);
  };

  _proto.onCloseButtonClick = function onCloseButtonClick() {
    this.setCurrentItemIndex(INDEX_NOT_SET);
  };

  _proto.renderItem = function renderItem(item, index) {
    return null;
  };

  _proto.renderItemDetails = function renderItemDetails(item) {
    return null;
  };

  _proto.renderItems = function renderItems() {
    var _this$props = this.props,
        cols = _this$props.cols,
        items = _this$props.items,
        testAutomationId = _this$props.testAutomationId,
        dataTestAutomationId = _this$props['data-test-automation-id'];
    var renderedItems = items.map(this.renderItem.bind(this));
    var gridItems = [];

    for (var i = 1; i <= this.getRowNumbers(); i++) {
      var rowItems = [];

      for (var j = 0; j < cols; j++) {
        rowItems.push(renderedItems.shift());
      }

      gridItems.push(React.createElement(Row, {
        key: "row-0-" + i
      }, rowItems));

      if (this.getActiveRow() === i) {
        gridItems.push(React.createElement(Row, {
          className: "active",
          key: "row-1-" + i
        }, React.createElement(Banner, {
          closable: this.props.closable,
          closeLabel: this.props.closeLabel,
          onClose: this.onCloseButtonClick.bind(this),
          arrowColsOffset: this.state.currentItemIndex % cols * this.getColSize(),
          additionalOffset: this.props.bannerAdditionalOffset
        }, this.renderItemDetails(items[this.state.currentItemIndex]))));
      }
    }

    return React.createElement("div", {
      className: "rc-tiled-grid",
      "data-test-automation-id": testAutomationId || dataTestAutomationId
    }, gridItems);
  };

  _proto.render = function render() {
    return this.renderItems();
  };

  return TiledGrid;
}(React.Component);

_defineProperty(TiledGrid, "propTypes", {
  testAutomationId: PropTypes.string,
  currentItemIndex: PropTypes.number,
  cols: PropTypes.number,
  items: PropTypes.array.isRequired,
  bannerAdditionalOffset: PropTypes.number,
  closable: PropTypes.bool,
  closeLabel: PropTypes.string,
  onChange: PropTypes.func
});

_defineProperty(TiledGrid, "defaultProps", {
  cols: 2,
  bannerAdditionalOffset: 48,
  currentItemIndex: PROPERTY_NOT_SET,
  closable: true,
  closeLabel: 'TRANSLATE:CLOSE',
  testAutomationId: 'tiledGrid',
  onChange: null
});