"use strict";

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

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

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

  _chalk = function () {
    return data;
  };

  return data;
}

const 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)'
};
exports.componentIssuesLabels = componentIssuesLabels;

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]" or "bit untrack [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().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().default.underline(_chalk().default.cyan(missingComponent.id.toString()))}\n${formatMissing(missingComponent)}`;
  }

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

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

  const missingStr = Object.keys(componentIssuesLabels).map(key => formatMissingStr(missingComponent.issues[key], componentIssuesLabels[key])).join('');
  return `${missingStr}\n`;
}