"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.actionLegacy = actionLegacy;
exports.reportLegacy = reportLegacy;
function _chalk() {
  const data = _interopRequireDefault(require("chalk"));
  _chalk = function () {
    return data;
  };
  return data;
}
function _padRight() {
  const data = _interopRequireDefault(require("pad-right"));
  _padRight = function () {
    return data;
  };
  return data;
}
function _table() {
  const data = require("table");
  _table = function () {
    return data;
  };
  return data;
}
function _legacy() {
  const data = require("@teambit/legacy.component-diff");
  _legacy = function () {
    return data;
  };
  return data;
}
function _legacyShow() {
  const data = require("./legacy-show");
  _legacyShow = function () {
    return data;
  };
  return data;
}
function _docsTemplate() {
  const data = _interopRequireDefault(require("./docs-template"));
  _docsTemplate = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function actionLegacy([id], {
  json,
  remote = false,
  compare = false
}) {
  return (0, _legacyShow().show)({
    id,
    // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
    json,
    remote,
    compare
  });
}
function reportLegacy({
  component,
  componentModel,
  json
}) {
  if (component.componentFromModel) {
    component.scopesList = component.componentFromModel.scopesList;
  }
  if (json) {
    const makeComponentReadable = comp => {
      if (!comp) return comp;
      const componentObj = comp.toObject();
      componentObj.files = comp.files.map(file => file.toReadableString());
      if (comp.componentMap) {
        componentObj.componentDir = comp.componentMap.getComponentDir();
      }
      return componentObj;
    };
    const componentFromFileSystem = makeComponentReadable(component);
    if (component.scopesList) {
      // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
      componentFromFileSystem.scopesList = component.scopesList;
    }
    const componentFromModel = componentModel ? makeComponentReadable(componentModel) : undefined;
    const jsonObject = componentFromModel ? {
      componentFromFileSystem,
      componentFromModel
    } : componentFromFileSystem;
    return JSON.stringify(jsonObject, null, '  ');
  }
  return paintComponent(component, componentModel);
}
const COLUMN_WIDTH = 50;
const tableColumnConfig = {
  columns: {
    1: {
      alignment: 'left',
      width: COLUMN_WIDTH
    },
    2: {
      alignment: 'left',
      width: COLUMN_WIDTH
    }
  }
};
function paintComponent(component, componentModel) {
  return componentModel ? paintWithCompare() : paintWithoutCompare();
  function paintWithoutCompare() {
    const printableComponent = (0, _legacy().componentToPrintableForDiff)(component);
    // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
    printableComponent.scopesList = (component.scopesList || []).map(s => s.name).join('\n');
    const rows = getFields().map(field => {
      const arr = [];
      const title = (0, _legacy().prettifyFieldName)(field);
      if (!printableComponent[field]) return null;

      // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
      arr.push(_chalk().default.cyan(title));
      if (!printableComponent[field]) return null;
      if (printableComponent[field]) {
        if (printableComponent[field] instanceof Array) {
          arr.push(
          // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
          printableComponent[field].map(str => calculatePadRightLength(str, COLUMN_WIDTH)).join(' ').trim());
          // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
        } else arr.push(printableComponent[field]);
      }
      return arr;
    }).filter(x => x);
    const componentTable = (0, _table().table)(rows, tableColumnConfig);
    return componentTable + (0, _docsTemplate().default)(component.docs);
  }
  function paintWithCompare() {
    if (!componentModel) throw new Error('paintWithCompare, componentModel must be defined');
    const printableOriginalComponent = (0, _legacy().componentToPrintableForDiff)(component);
    // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
    printableOriginalComponent.id += ' [file system]';
    const printableComponentToCompare = (0, _legacy().componentToPrintableForDiff)(componentModel);
    const componentsDiffs = (0, _legacy().getDiffBetweenObjects)(printableOriginalComponent, printableComponentToCompare);
    const rows = getFields().map(field => {
      const arr = [];
      if (!printableOriginalComponent[field] && !printableComponentToCompare[field]) return null;
      const title = `${field[0].toUpperCase()}${field.slice(1)}`.replace(/([A-Z])/g, ' $1').trim();
      // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
      arr.push(field in componentsDiffs && field !== 'id' ? _chalk().default.red(title) : _chalk().default.cyan(title));
      if (printableComponentToCompare[field] instanceof Array) {
        arr.push(
        // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
        printableComponentToCompare[field].map(str => calculatePadRightLength(str, COLUMN_WIDTH)).join(' ').trim());
      } else {
        // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
        arr.push(printableComponentToCompare[field]);
      }
      if (printableOriginalComponent[field] instanceof Array) {
        arr.push(
        // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
        printableOriginalComponent[field].map(str => calculatePadRightLength(str, COLUMN_WIDTH)).join(' ').trim());
      } else {
        // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
        arr.push(printableOriginalComponent[field]);
      }
      return arr;
    }).filter(x => x);
    const componentTable = (0, _table().table)(rows, tableColumnConfig);
    const dependenciesTableStr = generateDependenciesTable();
    return componentTable + dependenciesTableStr;
  }
  function getFields() {
    const fields = ['id', 'compiler', 'tester', 'language', 'mainFile', 'dependencies', 'devDependencies', 'packages', 'devPackages', 'peerDependencies', 'files', 'specs', 'deprecated'];
    return fields;
  }
  function generateDependenciesTable() {
    if (!component.hasDependencies()) {
      return '';
    }
    const dependencyHeader = [];
    // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
    dependencyHeader.push(['Dependencies']);
    const getDependenciesRows = (dependencies, title) => {
      const dependencyRows = [];
      dependencies.forEach(dependency => {
        let dependencyId = dependency.id.toString();
        dependencyId = title ? `${dependencyId} (${title})` : dependencyId;
        const row = [dependencyId];
        // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
        dependencyRows.push(row);
      });
      return dependencyRows;
    };
    const dependenciesRows = getDependenciesRows(component.dependencies.get());
    const devDependenciesRows = getDependenciesRows(component.devDependencies.get(), 'dev');
    const allDependenciesRows = [...dependenciesRows, ...devDependenciesRows];
    const dependenciesTable = (0, _table().table)(dependencyHeader.concat(allDependenciesRows));
    return dependenciesTable;
  }
  function calculatePadRightLength(str, columnWidth) {
    if (!str) return '';
    const padRightCount = Math.ceil(str.length / columnWidth) * columnWidth;
    return str.length > columnWidth ? (0, _padRight().default)(str, padRightCount, ' ') : (0, _padRight().default)(str, columnWidth, ' ');
  }
}

//# sourceMappingURL=show-legacy-cmd.js.map