'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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('./exceptions');
}

var _abstractBitJson;

function _load_abstractBitJson() {
  return _abstractBitJson = _interopRequireDefault(require('./abstract-bit-json'));
}

var _consumerBitJson;

function _load_consumerBitJson() {
  return _consumerBitJson = _interopRequireDefault(require('./consumer-bit-json'));
}

var _component;

function _load_component() {
  return _component = _interopRequireDefault(require('../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"); } }

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 ComponentBitJson = function (_AbstractBitJson) {
  _inherits(ComponentBitJson, _AbstractBitJson);

  function ComponentBitJson(_ref) {
    var impl = _ref.impl,
        spec = _ref.spec,
        compiler = _ref.compiler,
        tester = _ref.tester,
        dependencies = _ref.dependencies,
        devDependencies = _ref.devDependencies,
        packageDependencies = _ref.packageDependencies,
        devPackageDependencies = _ref.devPackageDependencies,
        peerPackageDependencies = _ref.peerPackageDependencies,
        lang = _ref.lang,
        bindingPrefix = _ref.bindingPrefix,
        extensions = _ref.extensions;

    _classCallCheck(this, ComponentBitJson);

    var _this = _possibleConstructorReturn(this, (ComponentBitJson.__proto__ || Object.getPrototypeOf(ComponentBitJson)).call(this, { impl, spec, compiler, tester, dependencies, devDependencies, lang, bindingPrefix, extensions }));

    _this.packageDependencies = packageDependencies || {};
    _this.devPackageDependencies = devPackageDependencies || {};
    _this.peerPackageDependencies = peerPackageDependencies || {};
    return _this;
  }

  _createClass(ComponentBitJson, [{
    key: 'toPlainObject',
    value: function toPlainObject() {
      var superObject = _get(ComponentBitJson.prototype.__proto__ || Object.getPrototypeOf(ComponentBitJson.prototype), 'toPlainObject', this).call(this);
      return (_ramda || _load_ramda()).default.merge(superObject, {
        packageDependencies: this.getPackageDependencies()
      });
    }
  }, {
    key: 'getPackageDependencies',
    value: function getPackageDependencies() {
      return this.packageDependencies;
    }
  }, {
    key: 'toJson',
    value: function toJson() {
      var readable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

      if (!readable) return JSON.stringify(this.toPlainObject());
      return JSON.stringify(this.toPlainObject(), null, 4);
    }
  }, {
    key: 'validate',
    value: function validate(bitJsonPath) {
      if (typeof this.getImplBasename() !== 'string' || typeof this.compiler !== 'object' || typeof this.tester !== 'object' || this.getDependencies() && typeof this.getDependencies() !== 'object' || this.extensions() && typeof this.extensions() !== 'object') {
        throw new (_exceptions || _load_exceptions()).InvalidBitJson(bitJsonPath);
      }
    }
  }, {
    key: 'mergeWithComponentData',
    value: function mergeWithComponentData(component) {
      this.bindingPrefix = component.bindingPrefix;
      this.lang = component.lang;
    }

    /**
     * Use the consumerBitJson as a base. Override values if exist in componentBitJson
     */

  }], [{
    key: 'fromPlainObject',
    value: function fromPlainObject(object) {
      var _object$sources = object.sources,
          sources = _object$sources === undefined ? {} : _object$sources,
          env = object.env,
          dependencies = object.dependencies,
          packageDependencies = object.packageDependencies,
          lang = object.lang,
          bindingPrefix = object.bindingPrefix,
          extensions = object.extensions;


      return new ComponentBitJson({
        impl: (_ramda || _load_ramda()).default.prop('impl', sources),
        spec: (_ramda || _load_ramda()).default.prop('spec', sources),
        compiler: (_ramda || _load_ramda()).default.prop('compiler', env),
        tester: (_ramda || _load_ramda()).default.prop('tester', env),
        dependencies,
        extensions,
        lang,
        bindingPrefix,
        packageDependencies
      });
    }
  }, {
    key: 'mergeWithProto',
    value: function mergeWithProto(json, protoBJ) {
      var plainProtoBJ = protoBJ ? protoBJ.toPlainObject() : {};
      delete plainProtoBJ.dependencies;
      return ComponentBitJson.fromPlainObject((_ramda || _load_ramda()).default.merge(plainProtoBJ, json));
    }
  }, {
    key: 'create',
    value: function create() {
      var json = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var protoBJ = arguments[1];

      return ComponentBitJson.mergeWithProto(json, protoBJ);
    }
  }, {
    key: 'load',
    value: function load(dirPath, protoBJ) {
      var _this2 = this;

      return new Promise(function (resolve, reject) {
        try {
          var result = _this2.loadSync(dirPath, protoBJ);
          return resolve(result);
        } catch (e) {
          return reject(dirPath);
        }
      });
    }
  }, {
    key: 'loadSync',
    value: function loadSync(dirPath, protoBJ) {
      var thisBJ = {};
      var bitJsonPath = '';
      if (dirPath) {
        bitJsonPath = (_abstractBitJson || _load_abstractBitJson()).default.composePath(dirPath);
        if ((_fsExtra || _load_fsExtra()).default.existsSync(bitJsonPath)) {
          try {
            thisBJ = (_fsExtra || _load_fsExtra()).default.readJsonSync(bitJsonPath);
          } catch (e) {
            throw new (_exceptions || _load_exceptions()).InvalidBitJson(bitJsonPath);
          }
        }
      }

      var componentBitJson = ComponentBitJson.mergeWithProto(thisBJ, protoBJ);
      componentBitJson.path = bitJsonPath;
      return componentBitJson;
    }
  }]);

  return ComponentBitJson;
}((_abstractBitJson || _load_abstractBitJson()).default);

exports.default = ComponentBitJson;