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

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

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

exports.default = function (listScopeResults, json, showRemoteVersion) {
  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 tabulateComponent(listScopeResult) {
    var id = listScopeResult.id.toStringWithoutVersion();
    var version = listScopeResult.id.version;
    if (!json && showRemoteVersion) {
      var color = listScopeResult.remoteVersion && (_semver || _load_semver()).default.gt(listScopeResult.remoteVersion, version) ? 'red' : null;
      version = color ? (_chalk || _load_chalk()).default[color](version) : version;
    }
    var data = {
      id: (_chalk || _load_chalk()).default.white(`${id}${listScopeResult.deprecated ? ' [Deprecated]' : ''}`),
      localVersion: version,
      currentVersion: listScopeResult.currentlyUsedVersion || 'N/A'
    };

    if (showRemoteVersion) {
      var _remoteVersion = listScopeResult.remoteVersion || 'N/A';
      var _color = listScopeResult.remoteVersion && (_semver || _load_semver()).default.gt(listScopeResult.id.version, listScopeResult.remoteVersion) ? 'red' : null;
      _remoteVersion = _color ? (_chalk || _load_chalk()).default[_color](_remoteVersion) : _remoteVersion;
      data.remoteVersion = _remoteVersion;
    }
    return data;
  }

  function toJsonComponent(listScopeResult) {
    var id = listScopeResult.id.toStringWithoutVersion();
    var version = listScopeResult.id.version;
    var data = {
      id,
      localVersion: version,
      deprecated: listScopeResult.deprecated,
      currentVersion: listScopeResult.currentlyUsedVersion || 'N/A',
      remoteVersion: listScopeResult.remoteVersion || 'N/A'
    };
    return data;
  }

  if (json) {
    var jsonResults = listScopeResults.map(toJsonComponent);
    return JSON.stringify(jsonResults, null, 2);
  }
  var rows = listScopeResults.map(tabulateComponent);

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