'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _Base2 = require('./Base');

var _Base3 = _interopRequireDefault(_Base2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ORPHAN_THRESHOLD = 15;

var View = function (_Base) {
  _inherits(View, _Base);

  function View() {
    _classCallCheck(this, View);

    return _possibleConstructorReturn(this, (View.__proto__ || Object.getPrototypeOf(View)).apply(this, arguments));
  }

  _createClass(View, [{
    key: 'isEmpty',
    value: function isEmpty() {
      if (this.children.length === 0) {
        return false;
      }

      return this.children.every(function (child) {
        return child.isEmpty();
      });
    }
  }, {
    key: 'splice',
    value: function splice(wrapHeight, pageHeight) {
      var buffer = [];
      var result = this.clone();

      for (var i = 0; i < this.children.length; i++) {
        var child = this.children[i];
        var _child$props = child.props,
            fixed = _child$props.fixed,
            wrap = _child$props.wrap,
            orphan = _child$props.orphan;


        var childBottom = child.top + child.height;
        var isElementOutside = wrapHeight < child.top;
        var shouldElementSplit = wrapHeight < child.top + child.height;
        var orphanThreshold = child.props.orphanThreshold || ORPHAN_THRESHOLD;
        var isElementOrphan = !orphan && pageHeight - childBottom < orphanThreshold;

        if (isElementOutside || isElementOrphan) {
          buffer.push(child);
        } else if (fixed) {
          var fixedElement = child.clone();
          fixedElement.children = child.children;
          result.appendChild(fixedElement);
        } else if (child.props.break) {
          child.props.break = false;
          buffer.push.apply(buffer, _toConsumableArray(this.children.slice(i)));
          break;
        } else if (shouldElementSplit) {
          var remainingHeight = wrapHeight - child.top - this.marginTop;

          if (!wrap) {
            buffer.push(child);
          } else {
            result.appendChild(child.splice(remainingHeight, pageHeight));
          }
        }
      }

      buffer.forEach(function (child) {
        return child.moveTo(result);
      });

      // If the View has fixed height, we calculate the new element heights.
      // If not, we set it up as NaN and use Yoga calculated heights as fallback.
      var h = this.style.height ? wrapHeight : NaN;

      result.marginTop = 0;
      result.paddingTop = 0;
      result.height = this.height - h;
      this.marginBottom = 0;
      this.paddingBottom = 0;
      this.height = h;

      return result;
    }
  }, {
    key: 'render',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(page) {
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                this.drawBackgroundColor();
                this.drawBorders();

                if (this.props.debug) {
                  this.debug();
                }

                _context.next = 5;
                return this.renderChildren(page);

              case 5:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function render(_x) {
        return _ref.apply(this, arguments);
      }

      return render;
    }()
  }]);

  return View;
}(_Base3.default);

View.defaultProps = {
  style: {},
  wrap: true
};
exports.default = View;