'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

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; };

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 _SubPage = require('./SubPage');

var _SubPage2 = _interopRequireDefault(_SubPage);

var _stylesheet = require('../stylesheet');

var _stylesheet2 = _interopRequireDefault(_stylesheet);

var _pageSizes = require('../utils/pageSizes');

var _pageSizes2 = _interopRequireDefault(_pageSizes);

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Page = function () {
  function Page(root, props) {
    _classCallCheck(this, Page);

    this.root = root;
    this.parent = null;
    this.props = _extends({}, Page.defaultProps, props);
    this.previousPage = null;
    this.children = [];
    this._size = null;

    this.addInitialSubpage();
  }

  _createClass(Page, [{
    key: 'applyProps',
    value: function applyProps() {
      this.style = _stylesheet2.default.resolve(this.props.style);

      for (var i = 0; i < this.children.length; i++) {
        this.children[i].applyProps();
      }
    }
  }, {
    key: 'addInitialSubpage',
    value: function addInitialSubpage() {
      var newSubpage = new _SubPage2.default(this.root, this.props, 1);
      newSubpage.parent = this;

      this.children.push(newSubpage);
    }
  }, {
    key: 'appendChild',
    value: function appendChild(child) {
      this.children[0].appendChild(child);
    }
  }, {
    key: 'removeChild',
    value: function removeChild(child) {
      this.children[0].removeChild(child);
    }
  }, {
    key: 'wrapPage',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
        var _style, paddingTop, paddingBottom, height, nextSubpage;

        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _style = this.style, paddingTop = _style.paddingTop, paddingBottom = _style.paddingBottom;
                height = this.size[1] - paddingTop - paddingBottom;
                nextSubpage = this.initialSubpage.wrap(height);


                while (this.props.wrap && !nextSubpage.isEmpty()) {
                  console.log(nextSubpage);
                  this.children.push(nextSubpage);
                  nextSubpage = nextSubpage.wrap(height);
                }

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

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

      return wrapPage;
    }()
  }, {
    key: 'render',
    value: function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
        var i;
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                i = 0;

              case 1:
                if (!(i < this.children.length)) {
                  _context2.next = 7;
                  break;
                }

                _context2.next = 4;
                return this.children[i].render(this);

              case 4:
                i++;
                _context2.next = 1;
                break;

              case 7:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

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

      return render;
    }()
  }, {
    key: 'document',
    get: function get() {
      return this.parent;
    }
  }, {
    key: 'orientation',
    get: function get() {
      return this.props.orientation;
    }
  }, {
    key: 'initialSubpage',
    get: function get() {
      return this.children[0];
    }
  }, {
    key: 'subpagesCount',
    get: function get() {
      return this.children.length;
    }
  }, {
    key: 'numberOffset',
    get: function get() {
      var result = 0;
      var page = this.previousPage;

      while (page) {
        result += page.subpagesCount;
        page = page.previousPage;
      }

      return result;
    }
  }, {
    key: 'size',
    get: function get() {
      if (this._size) {
        return this._size;
      }

      var size = this.props.size;


      if (typeof size === 'string') {
        this._size = _pageSizes2.default[size];
      } else if (Array.isArray(size)) {
        this._size = size;
      } else if ((typeof size === 'undefined' ? 'undefined' : _typeof(size)) === 'object' && size.width && size.height) {
        this._size = [size.width, size.height];
      } else {
        throw new Error('Invalid Page size: ' + size);
      }

      return this._size;
    }
  }]);

  return Page;
}();

Page.defaultProps = {
  size: 'A4',
  orientation: 'portrait',
  style: {},
  wrap: false
};
exports.default = Page;