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

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

var _repository;

function _load_repository() {
  return _repository = _interopRequireDefault(require('./objects/repository'));
}

var _utils;

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

var _objectRegistrar;

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

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('./models/component'));
}

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',
    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);
        })
      };
    }
  }], [{
    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');
}