'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.pdf = exports.createElement = exports.StyleSheet = exports.Document = exports.Image = exports.Font = exports.Page = exports.Link = exports.Text = exports.View = exports.PDFRenderer = undefined;

var _renderer = require('./renderer');

var _renderer2 = _interopRequireDefault(_renderer);

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

var _stylesheet2 = _interopRequireDefault(_stylesheet);

var _elements = require('./elements');

var _font = require('./font');

var _font2 = _interopRequireDefault(_font);

var _blobStream = require('blob-stream');

var _blobStream2 = _interopRequireDefault(_blobStream);

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

var View = 'VIEW';
var Text = 'TEXT';
var Link = 'LINK';
var Page = 'PAGE';
var Image = 'IMAGE';
var Document = 'DOCUMENT';

var pdf = function pdf(input) {
  var parse = function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(input) {
      var document;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              document = input.document;
              _context.next = 3;
              return document.render();

            case 3:

              if (document.props.onRender) {
                document.props.onRender();
              }

              return _context.abrupt('return', input);

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

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

  var toBlob = function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var stream;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return parse(input);

            case 2:
              stream = input.pipe((0, _blobStream2.default)());
              return _context2.abrupt('return', new Promise(function (resolve, reject) {
                stream.on('finish', function () {
                  resolve(stream.toBlob('application/pdf'));
                });

                stream.on('error', reject);
              }));

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

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

  var toBuffer = function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return parse(input);

            case 2:
              return _context3.abrupt('return', _context3.sent);

            case 3:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, this);
    }));

    return function toBuffer() {
      return _ref3.apply(this, arguments);
    };
  }();

  var toString = function () {
    var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
      var result, render;
      return regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              result = '';
              _context4.next = 3;
              return parse(input);

            case 3:
              render = _context4.sent;
              return _context4.abrupt('return', new Promise(function (resolve) {
                render.on('data', function (buffer) {
                  result += buffer;
                });

                render.on('end', function () {
                  resolve(result);
                });
              }));

            case 5:
            case 'end':
              return _context4.stop();
          }
        }
      }, _callee4, this);
    }));

    return function toString() {
      return _ref4.apply(this, arguments);
    };
  }();

  return {
    toBuffer: toBuffer,
    toBlob: toBlob,
    toString: toString
  };
};

exports.PDFRenderer = _renderer2.default;
exports.View = View;
exports.Text = Text;
exports.Link = Link;
exports.Page = Page;
exports.Font = _font2.default;
exports.Image = Image;
exports.Document = Document;
exports.StyleSheet = _stylesheet2.default;
exports.createElement = _elements.createElement;
exports.pdf = pdf;