"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _semver() {
  const data = _interopRequireDefault(require("semver"));

  _semver = function () {
    return data;
  };

  return data;
}

function _ttyTable() {
  const data = _interopRequireDefault(require("tty-table"));

  _ttyTable = function () {
    return data;
  };

  return data;
}

function _utils() {
  const data = require("../../utils");

  _utils = function () {
    return data;
  };

  return data;
}

var _default = (listScopeResults, json, showRemoteVersion) => {
  const 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'
    });
  }

  const opts = {
    align: 'left'
  };

  function tabulateComponent(listScopeResult) {
    const id = listScopeResult.id.toStringWithoutVersion();
    let version = listScopeResult.id.version;

    if (!json && showRemoteVersion) {
      const color = listScopeResult.remoteVersion && _semver().default.gt(listScopeResult.remoteVersion, version) ? 'red' : null;
      version = color ? _chalk().default[color](version) : version;
    }

    const data = {
      id: _chalk().default.white(`${id}${listScopeResult.deprecated ? ' [Deprecated]' : ''}`),
      localVersion: version,
      currentVersion: listScopeResult.currentlyUsedVersion || 'N/A'
    };

    if (showRemoteVersion) {
      let remoteVersion = listScopeResult.remoteVersion || 'N/A';
      const color = listScopeResult.remoteVersion && _semver().default.gt(listScopeResult.id.version, listScopeResult.remoteVersion) ? 'red' : null;
      remoteVersion = color ? _chalk().default[color](remoteVersion) : remoteVersion;
      data.remoteVersion = remoteVersion;
    }

    return data;
  }

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

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

  const rows = listScopeResults.map(tabulateComponent);
  const table = new (_ttyTable().default)(header, rows.map(row => _ramda().default.values(row)), opts);
  return table.render();
};

exports.default = _default;