'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 = require('ramda');
}

var _bitId;

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

var _utils;

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

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',

    // TODO: use the static toStrings below
    value: function serialize() {
      return this.map(function (bitId) {
        return bitId.toString();
      });
    }
  }, {
    key: 'toObject',
    value: function toObject() {
      return (0, (_ramda || _load_ramda()).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) {
      return (0, (_utils || _load_utils()).getLatestVersionNumber)(this, idWithLatest);
    }

    /**
     * Get array of bitIds strings and transfer them to BitIds object
     * This function support also checking if the array contain strings or BitIds
     * @param {string | BitId} array - array of bit ids
     */

  }], [{
    key: 'deserialize',
    value: function deserialize() {
      var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

      if (array && array.length && typeof array[0] === 'string') {
        return new (Function.prototype.bind.apply(BitIds, [null].concat(_toConsumableArray(array.map(function (id) {
          return (_bitId || _load_bitId()).BitId.parse(id);
        })))))();
      }
      return new (Function.prototype.bind.apply(BitIds, [null].concat(_toConsumableArray(array))))();
    }

    /**
     * Get array of bitIds strings and transfer them to BitIds object
     * This function support also checking if the array contain strings or BitIds
     * @param {string | BitId} array - array of bit ids
     */

  }, {
    key: 'toStrings',
    value: function toStrings() {
      var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

      if (array && array.length && typeof array[0] === 'string') {
        return array;
      }
      return array.map(function (bitId) {
        return bitId.toString();
      });
    }
  }, {
    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, version));
      });

      return new (Function.prototype.bind.apply(BitIds, [null].concat(array)))();
    }
  }, {
    key: 'clone',
    value: function clone() {
      var bitIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

      var cloneIds = bitIds.map(function (bitId) {
        return bitId.clone();
      });
      return new (Function.prototype.bind.apply(BitIds, [null].concat(_toConsumableArray(cloneIds))))();
    }
  }]);

  return BitIds;
}(_extendableBuiltin(Array));

exports.default = BitIds;