"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var react_resize_detector_1 = require("react-resize-detector");
var DEFAULT_GAP = 32;
var Masonry = /** @class */ (function (_super) {
    tslib_1.__extends(Masonry, _super);
    function Masonry() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            containerWidth: _this.props.containerWidth
        };
        return _this;
    }
    Masonry.prototype.render = function () {
        var _this = this;
        var _a = this.props, _b = _a.gap, gap = _b === void 0 ? DEFAULT_GAP : _b, items = _a.items, minColumnWidth = _a.minColumnWidth;
        var containerWidth = this.state.containerWidth;
        var margin = gap / 2;
        var count = containerWidth !== undefined ? columnCount(containerWidth, gap, minColumnWidth) : null;
        var content = null;
        if (count !== null && items.length > 0) {
            var columns_1 = sort(count, items);
            content = (React.createElement("div", { style: { display: "flex", margin: -margin } }, columns_1.map(function (c, i) { return (React.createElement("div", { key: i, style: { flex: "1 1 " + 100 / columns_1.length + "%" } }, c.map(function (i) { return (React.createElement("div", { key: i.key, style: { padding: margin } }, i.node)); }))); })));
        }
        return (React.createElement(React.Fragment, null,
            content,
            process.env.NODE_ENV === "test" ? null : (React.createElement(react_resize_detector_1.default, { handleWidth: true, onResize: function (width) { return _this.setState({ containerWidth: width }); } }))));
    };
    return Masonry;
}(React.PureComponent));
exports.Masonry = Masonry;
function sort(count, items) {
    var columns = Array.apply([], Array(count));
    columns = columns.map(function () { return []; });
    var curr = 0;
    for (var i in items) {
        columns[curr].push(items[i]);
        curr = curr < count - 1 ? curr + 1 : 0;
    }
    return columns;
}
function columnCount(containerWidth, gap, minColumnWidth) {
    var columns = Math.floor((containerWidth + gap) / (minColumnWidth + gap));
    return Math.max(columns, 1);
}
//# sourceMappingURL=index.js.map