'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

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

function _load_object() {
  return _object = _interopRequireDefault(require('./objects/object'));
}

var _utils;

function _load_utils() {
  return _utils = require('../utils');
}

var _objectRegistrar;

function _load_objectRegistrar() {
  return _objectRegistrar = require('./object-registrar');
}

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

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

// import logger from '../logger/logger';

var ComponentObjects = function () {
  function ComponentObjects(component, objects) {
    _classCallCheck(this, ComponentObjects);

    this.component = component;
    this.objects = objects;
  }

  _createClass(ComponentObjects, [{
    key: 'toString',
    value: function toString() {
      return JSON.stringify({
        component: (0, (_utils || _load_utils()).toBase64ArrayBuffer)(this.component),
        objects: this.objects.map((_utils || _load_utils()).toBase64ArrayBuffer)
      });
    }

    // Used mainly by server side hooks

  }, {
    key: 'getParsedComponent',
    value: function getParsedComponent() {
      var component = (_object || _load_object()).default.parseSync(this.component, (_objectRegistrar || _load_objectRegistrar()).typesObj);
      return component;
    }

    // @TODO optimize ASAP.

  }, {
    key: 'toObjects',


    /**
     * prefer using `this.toObjectsAsync()` if not must to be sync.
     */
    value: function toObjects(repo) {
      return {
        component: (_object || _load_object()).default.parseSync(this.component, repo.types),
        objects: this.objects.map(function (obj) {
          return (_object || _load_object()).default.parseSync(obj, repo.types);
        })
      };
    }
    /**
     * see `this.toObject()` for the sync version
     */

  }, {
    key: 'toObjectsAsync',
    value: function () {
      var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(repo) {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return (_object || _load_object()).default.parseObject(this.component, repo.types);

              case 2:
                _context.t0 = _context.sent;
                _context.next = 5;
                return Promise.all(this.objects.map(function (obj) {
                  return (_object || _load_object()).default.parseObject(obj, repo.types);
                }));

              case 5:
                _context.t1 = _context.sent;
                return _context.abrupt('return', {
                  component: _context.t0,
                  objects: _context.t1
                });

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

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

      return toObjectsAsync;
    }()
  }], [{
    key: 'fromString',
    value: function fromString(str) {
      return ComponentObjects.fromObject(JSON.parse(str));
    }
  }, {
    key: 'manyToString',
    value: function manyToString(componentsAndObjects) {
      var result = JSON.stringify(componentsAndObjects.map(function (componentAndObject) {
        return componentAndObject.toString();
      }));
      return result;
    }
  }, {
    key: 'manyFromString',
    value: function manyFromString(str) {
      return JSON.parse(str).map(function (componentObject) {
        return ComponentObjects.fromString(componentObject);
      });
    }
  }, {
    key: 'fromObject',
    value: function fromObject(object) {
      var component = object.component,
          objects = object.objects;


      return new ComponentObjects(_from64Buffer(component), objects.map(_from64Buffer));
    }
  }]);

  return ComponentObjects;
}();

exports.default = ComponentObjects;


function _from64Buffer(val) {
  return Buffer.from(val, 'base64');
}