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

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

var _bitId;

function _load_bitId() {
  return _bitId = require('../bit-id');
}

var _utils;

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

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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

function _extendableBuiltin(cls) {
  function ExtendableBuiltin() {
    var instance = Reflect.construct(cls, Array.from(arguments));
    Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
    return instance;
  }

  ExtendableBuiltin.prototype = Object.create(cls.prototype, {
    constructor: {
      value: cls,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });

  if (Object.setPrototypeOf) {
    Object.setPrototypeOf(ExtendableBuiltin, cls);
  } else {
    ExtendableBuiltin.__proto__ = cls;
  }

  return ExtendableBuiltin;
}

var BitIds = function (_extendableBuiltin2) {
  _inherits(BitIds, _extendableBuiltin2);

  function BitIds() {
    _classCallCheck(this, BitIds);

    return _possibleConstructorReturn(this, (BitIds.__proto__ || Object.getPrototypeOf(BitIds)).apply(this, arguments));
  }

  _createClass(BitIds, [{
    key: 'serialize',
    value: function serialize() {
      return this.map(function (bitId) {
        return bitId.toString();
      });
    }
  }, {
    key: 'toObject',
    value: function toObject() {
      return (_ramda || _load_ramda()).default.mergeAll(this.map(function (bitId) {
        return bitId.toObject();
      }));
    }

    /**
     * Resolve an id with latest to specific version
     * This used to get the real version from the flatten deps by the deps ids
     *
     * @param {BitId} idWithLatest - A bit id object with latest version
     * @returns {BitId} - The bit id found in the array (with actual version)
     * @memberof BitIds
     */

  }, {
    key: 'resolveVersion',
    value: function resolveVersion(idWithLatest) {
      // $FlowFixMe
      return (0, (_utils || _load_utils()).getLatestVersionNumber)(this, idWithLatest);
    }
  }, {
    key: 'has',
    value: function has(bitId) {
      return Boolean(this.search(bitId));
    }
  }, {
    key: 'hasWithoutVersion',
    value: function hasWithoutVersion(bitId) {
      return Boolean(this.searchWithoutVersion(bitId));
    }
  }, {
    key: 'search',
    value: function search(bitId) {
      return this.find(function (id) {
        return id.hasSameName(bitId) && id.hasSameScope(bitId) && id.hasSameVersion(bitId);
      });
    }
  }, {
    key: 'searchWithoutVersion',
    value: function searchWithoutVersion(bitId) {
      return this.find(function (id) {
        return id.hasSameName(bitId) && id.hasSameScope(bitId);
      });
    }
  }, {
    key: 'searchWithoutScopeAndVersion',
    value: function searchWithoutScopeAndVersion(bitId) {
      return this.find(function (id) {
        return id.hasSameName(bitId);
      });
    }
  }, {
    key: 'getUniq',
    value: function getUniq() {
      return BitIds.fromArray((_ramda || _load_ramda()).default.uniqBy(JSON.stringify, this));
    }
  }, {
    key: 'removeIfExistWithoutVersion',
    value: function removeIfExistWithoutVersion(bitId) {
      return BitIds.fromArray(this.filter(function (id) {
        return !id.isEqualWithoutVersion(bitId);
      }));
    }

    /**
     * make sure to pass only bit ids you know they have scope, otherwise, you'll get invalid bit ids.
     * this is mainly useful for remote commands where it is impossible to have a component without scope.
     */

  }, {
    key: 'toString',
    value: function toString() {
      return this.map(function (id) {
        return id.toString();
      }).join(', ');
    }
  }, {
    key: 'clone',
    value: function clone() {
      var cloneIds = this.map(function (id) {
        return id.clone();
      });
      return new (Function.prototype.bind.apply(BitIds, [null].concat(_toConsumableArray(cloneIds))))();
    }
  }], [{
    key: 'deserialize',
    value: function deserialize() {
      var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

      return new (Function.prototype.bind.apply(BitIds, [null].concat(_toConsumableArray(array.map(function (id) {
        return (_bitId || _load_bitId()).BitId.parse(id, true);
      })))))();
    }
  }, {
    key: 'fromObject',
    value: function fromObject(dependencies) {
      var array = [];

      (0, (_utils || _load_utils()).forEach)(dependencies, function (version, id) {
        array.push((_bitId || _load_bitId()).BitId.parse(id, true, version)); // bit.json has only imported dependencies, they all have scope
      });

      return new (Function.prototype.bind.apply(BitIds, [null].concat(array)))();
    }
  }, {
    key: 'fromArray',
    value: function fromArray(bitIds) {
      return new (Function.prototype.bind.apply(BitIds, [null].concat(_toConsumableArray(bitIds))))();
    }
  }]);

  return BitIds;
}(_extendableBuiltin(Array));

exports.default = BitIds;