'use strict';

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

var _chalk;

function _load_chalk() {
  return _chalk = _interopRequireDefault(require('chalk'));
}

var _ramda;

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

var _semver;

function _load_semver() {
  return _semver = _interopRequireDefault(require('semver'));
}

var _ttyTable;

function _load_ttyTable() {
  return _ttyTable = _interopRequireDefault(require('tty-table'));
}

var _consumerComponent;

function _load_consumerComponent() {
  return _consumerComponent = _interopRequireDefault(require('../../consumer/component/consumer-component'));
}

var _bitId;

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

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

exports.default = function (components, json) {
  var showRemoteVersion = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

  var header = [{ value: 'component ID', width: 70, headerColor: 'cyan', headerAlign: 'left' }, {
    value: showRemoteVersion ? 'local version' : 'local version',
    width: 9,
    headerColor: 'cyan',
    headerAlign: 'left'
  }, { value: 'used version', width: 9, headerColor: 'cyan', headerAlign: 'left' }];
  if (showRemoteVersion) {
    header.push({ value: 'remote version', width: 9, headerColor: 'cyan', headerAlign: 'left' });
  }
  var opts = {
    align: 'left'
  };

  function tablizeComponent(component) {
    var id = component.id.toStringWithoutVersion();
    var data = { id: (_chalk || _load_chalk()).default.white(`${id}${component.deprecated ? ' [Deprecated]' : ''}`) }; // Add date, author
    var version = component.version;
    if (!json && showRemoteVersion) {
      var color = component.latest && (_semver || _load_semver()).default.gt(component.latest, component.version) ? 'red' : null;
      version = color ? (_chalk || _load_chalk()).default[color](version) : version;
    }
    var getCurrentlyUsedVersion = function getCurrentlyUsedVersion() {
      if (!component._currentlyUsedVersion) return 'N/A';
      var bitId = component._currentlyUsedVersion;
      if (!bitId.hasVersion()) return 'N/A';
      return bitId.version;
    };
    data.localVersion = version;
    data.currentVersion = getCurrentlyUsedVersion();
    if (showRemoteVersion) {
      var remoteVersion = component.latest || 'N/A';
      var _color = component.latest && (_semver || _load_semver()).default.gt(component.version, component.latest) ? 'red' : null;
      remoteVersion = _color ? (_chalk || _load_chalk()).default[_color](remoteVersion) : remoteVersion;
      data.remoteVersion = remoteVersion;
    }
    return data;
  }

  var rows = components.map(tablizeComponent);
  if (json) return JSON.stringify(rows);

  var table = new (_ttyTable || _load_ttyTable()).default(header, rows.map(function (row) {
    return (_ramda || _load_ramda()).default.values(row);
  }), opts);
  return table.render();
};