// jshint esversion: 6
'use strict';
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import React from 'react';
import PropTypes from 'prop-types';
import Row from 'react-bootstrap/lib/Row';
import { Banner } from './Banner';
import lang from '../../lang/common/index-en_US';
var INDEX_NOT_SET = -1;
var PROPERTY_NOT_SET = null;
var TiledGrid = /** @class */ (function (_super) {
    __extends(TiledGrid, _super);
    function TiledGrid(props, context) {
        var _this = _super.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;
    }
    TiledGrid.prototype.componentWillReceiveProps = function (nextProps) {
        if (nextProps.hasOwnProperty('currentItemIndex') &&
            nextProps.currentItemIndex !== PROPERTY_NOT_SET) {
            this.setCurrentItemIndex(nextProps.currentItemIndex);
        }
    };
    TiledGrid.prototype.setCurrentItemIndex = function (index) {
        if (this.state.currentItemIndex !== index) {
            this.setState({
                currentItemIndex: index
            });
            if (this.props.onChange) {
                this.props.onChange(index);
            }
        }
    };
    TiledGrid.prototype.getColSize = function () {
        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;
    };
    TiledGrid.prototype.getRowNumbers = function () {
        var items = this.props.items;
        var cols = this.props.cols;
        return Math.ceil(items.length / cols);
    };
    TiledGrid.prototype.getActiveRow = function () {
        var cols = this.props.cols;
        var currentItemIndex = this.state.currentItemIndex;
        if (currentItemIndex === INDEX_NOT_SET) {
            return null;
        }
        return Math.ceil((currentItemIndex + 1) / cols);
    };
    TiledGrid.prototype.onCloseButtonClick = function () {
        this.setCurrentItemIndex(INDEX_NOT_SET);
    };
    TiledGrid.prototype.renderItem = function (item, index) {
        return null;
    };
    TiledGrid.prototype.renderItemDetails = function (item) {
        return null;
    };
    TiledGrid.prototype.renderItems = function () {
        var _a = this.props, cols = _a.cols, items = _a.items, testAutomationId = _a.testAutomationId, dataTestAutomationId = _a["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));
    };
    TiledGrid.prototype.render = function () {
        return this.renderItems();
    };
    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
    };
    TiledGrid.defaultProps = {
        cols: 2,
        bannerAdditionalOffset: 48,
        currentItemIndex: PROPERTY_NOT_SET,
        closable: true,
        get closeLabel() {
            return lang.CLOSE;
        },
        testAutomationId: 'tiledGrid',
        onChange: null
    };
    return TiledGrid;
}(React.Component));
export { TiledGrid };
//# sourceMappingURL=TiledGrid.js.map