text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  
  /**
   * Copyright (c) 2015-present, Zippy Technologies
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE file in the root directory of this source tree.
   */
  
  /**
   * Adjusts the sizes of the boxes taking into account their offset.
   * @param {[type]} boxes [description]
   */
  
  /**
   * Based on a region and a set of boxes (notifications).
   * Determines their position based on a stacking from
   * top to bottom and left to right.
   *
   * There are two cases horizontal and vertical dominant.
   * Using these two cases the positions can be translated to
   * any other direction.
  
     Case 1.
     Vertical dominant.
  
     Boxes go from top to bottom and then to left to right.
  
     +----------------------------------------->
     | +-----+ +------+
     | |     | |      |
     | +-----+ +------+
     | +-----+
     | |     |
     | +-----+
     | +-----+
     | |     |
     | +-----+
     |
     v Dominant
  
       Case 2.
       Horizontal Dominant
  
     Boxes go from left to right then down.
  
                  Dominant
     +----------------------------------------->
     | +-----+ +------+ +-----+ +-----+ +-----+
     | |     | |      | |     | |     | |     |
     | +-----+ +------+ +-----+ +-----+ +-----+
     | +-----+
     | |     |
     | +-----+
     |
     |
     |
     |
     v
  */
  
  function getEmptyColumn() {
    var overwrites = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  
    var newColumn = {
      columnBoundry: 0,
      left: 0,
      top: 0,
      height: 0,
      positions: [],
      width: 0
    };
  
    if (overwrites) {
      newColumn = _extends({}, newColumn, overwrites);
    }
  
    return newColumn;
  }
  
  function getWrapPositions(_ref) {
    var boxes = _ref.boxes,
        isVertical = _ref.isVertical,
        height = _ref.height,
        width = _ref.width;
  
    var positions = void 0;
  
    positions = boxes.reduce(function (acc, box) {
      var currentColumn = acc[acc.length - 1];
  
      // check if there is enough room
      var isColumnFull = isVertical ? box.height + currentColumn.height > height : box.width + currentColumn.width > width;
  
      if (isColumnFull) {
        // begin a new column
        var newColumn = void 0;
        if (isVertical) {
          newColumn = getEmptyColumn({
            left: currentColumn.width + currentColumn.left
          });
        } else {
          newColumn = getEmptyColumn({
            top: currentColumn.height + currentColumn.top
          });
        }
  
        acc.push(newColumn);
        currentColumn = newColumn;
      }
  
      var position = void 0;
      if (isVertical) {
        position = {
          top: currentColumn.height,
          left: currentColumn.left
        };
      } else {
        position = {
          top: currentColumn.top,
          left: currentColumn.width
        };
      }
  
      // const newBox = assign({}, box, position)
      currentColumn.positions.push(_extends({}, box, position)); // push new box
  
      if (isVertical) {
        currentColumn.height = box.height + currentColumn.height; // increment box height
      } else {
        currentColumn.width = box.width + currentColumn.width; // increment box width
      }
  
      // biggerst box gives the width of the column, this will be the basis for the
      // next column left
      if (isVertical) {
        if (box.width > currentColumn.width) {
          currentColumn.width = box.width;
        }
      } else if (box.height > currentColumn.height) {
        currentColumn.height = box.height;
      }
      return acc;
    }, [getEmptyColumn()]);
  
    // normalize to an array of positioned boxes
    positions = positions.reduce(function (acc, column) {
      acc = acc.concat(column.positions);
      return acc;
    }, []);
  
    return positions;
  }
  
  /**
   * Creates a single column, simplified algoritm
   */
  function getNoWrapPositions(_ref2) {
    var boxes = _ref2.boxes,
        isVertical = _ref2.isVertical;
  
    return boxes.reduce(function (acc, box, index, list) {
      var position = void 0;
      if (isVertical) {
        var top = index !== 0 ? acc[index - 1].top + list[index - 1].height : 0;
        position = {
          top: top,
          left: 0
        };
      } else {
        var left = index !== 0 ? acc[index - 1].left + list[index - 1].width : 0;
        position = {
          left: left,
          top: 0
        };
      }
      acc.push(_extends({}, box, position));
      return acc;
    }, []);
  }
  
  /*
   * @param {
   *  boxes: { width: number, height: number }[],
   *  width: number,
   *  height: number,
   *  stacking: string['vertical'|'horizontal']
   * }
  */
  function getPositionsRelativeToTopLeft(_ref3) {
    var _ref3$boxes = _ref3.boxes,
        boxes = _ref3$boxes === undefined ? [] : _ref3$boxes,
        width = _ref3.width,
        height = _ref3.height,
        isVertical = _ref3.isVertical,
        _ref3$stackingWrap = _ref3.stackingWrap,
        stackingWrap = _ref3$stackingWrap === undefined ? true : _ref3$stackingWrap;
  
    var positions = void 0;
  
    // just stacks on first direction
    if (!stackingWrap) {
      positions = getNoWrapPositions({ boxes: boxes, isVertical: isVertical });
    } else {
      // stacking in two directions
      positions = getWrapPositions({ boxes: boxes, isVertical: isVertical, height: height, width: width });
    }
  
    return positions;
  }
  
  exports.default = getPositionsRelativeToTopLeft;