'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.componentIssuesLabels = undefined;
exports.getInvalidComponentLabel = getInvalidComponentLabel;
exports.componentIssueToString = componentIssueToString;
exports.default = componentIssuesTemplate;

var _chalk;

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

var _consumerComponent;

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

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

var componentIssuesLabels = exports.componentIssuesLabels = {
  missingPackagesDependenciesOnFs: 'missing packages dependencies (use your package manager to make sure all package dependencies are installed)',
  missingComponents: 'missing components (use "bit import" or your package manager to make sure all components are installed)',
  untrackedDependencies: 'untracked file dependencies (use "bit add <file>" to track untracked files as components)',
  missingDependenciesOnFs: 'non-existing dependency files (please make sure all files exists on your workspace)',
  missingLinks: 'missing links (use "bit link" to build missing component links)',
  missingCustomModuleResolutionLinks: 'missing links (use "bit link" to build missing component links)',
  relativeComponents: 'components with relative import statements (please use absolute paths for imported components)',
  parseErrors: 'error found while parsing the file (please edit the file and fix the parsing error)',
  resolveErrors: 'error found while resolving the file dependencies (see the log for the full error)'
};

function getInvalidComponentLabel(error) {
  switch (error.name) {
    case 'MainFileRemoved':
      return 'main-file was removed (use "bit add" with "--main" and "--id" flags to add a main file)';
    case 'MissingFilesFromComponent':
    case 'ComponentNotFoundInPath':
      return 'component files were deleted (use "bit remove [component_id]" to remove the component from your workspace)';
    case 'ExtensionFileNotFound':
      // $FlowFixMe error.path is set for ExtensionFileNotFound
      return `extension file is missing at ${(_chalk || _load_chalk()).default.bold(error.path)}`;
    default:
      return error.name;
  }
}

function componentIssueToString(value) {
  return Array.isArray(value) ? value.join(', ') : value;
}

function componentIssuesTemplate(components) {
  function format(missingComponent) {
    return `${(_chalk || _load_chalk()).default.underline((_chalk || _load_chalk()).default.cyan(missingComponent.id.toString()))}\n${formatMissing(missingComponent)}`;
  }

  var result = `\n${components.map(format).join('\n')}`;
  return result;
}

function formatMissing(missingComponent) {
  function formatMissingStr(value, label) {
    if (!value || value.length === 0) return '';
    return (_chalk || _load_chalk()).default.yellow(`${label}: \n`) + (_chalk || _load_chalk()).default.white(Object.keys(value).map(function (key) {
      return `     ${key} -> ${componentIssueToString(value[key])}`;
    }).join('\n'));
  }

  var missingStr = Object.keys(componentIssuesLabels).map(function (key) {
    return formatMissingStr(missingComponent.issues[key], componentIssuesLabels[key]);
  }).join('');

  return `${missingStr}\n`;
}