"use strict";

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

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

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../api/consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _componentTemplate() {
  const data = _interopRequireDefault(require("../../templates/component-template"));

  _componentTemplate = function () {
    return data;
  };

  return data;
}

function _component() {
  const data = _interopRequireDefault(require("../../../consumer/component"));

  _component = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

class Show extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'show <id>');
    (0, _defineProperty2().default)(this, "description", `show component overview.\n https://${_constants().BASE_DOCS_DOMAIN}/docs/cli-show.html`);
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['j', 'json', 'return a json version of the component'], ['r', 'remote', 'show a remote component'], ['v', 'versions', 'return a json of all the versions of the component'], ['o', 'outdated', 'show latest version from the remote scope (if exists)'], ['c', 'compare [boolean]', 'compare current file system component to latest tagged component [default=latest]'], ['d', 'detailed', 'show more details'], ['', 'dependents', 'EXPERIMENTAL. show all dependents recursively'], ['', 'dependencies', 'EXPERIMENTAL. show all dependencies recursively']]);
    (0, _defineProperty2().default)(this, "loader", true);
    (0, _defineProperty2().default)(this, "migration", true);
  }

  action([id], {
    json,
    versions,
    remote = false,
    outdated = false,
    compare = false,
    detailed = false,
    dependents = false,
    dependencies = false
  }) {
    if (versions && (compare || outdated)) {
      throw new (_generalError().default)('the [--compare] or [--outdated] flag cannot be used along with --versions');
    }

    if (versions && remote) {
      throw new (_generalError().default)('the [--versions] and [--remote] flags cannot be used together');
    }

    if (compare && outdated) {
      throw new (_generalError().default)('please make sure to use either [--compare] or [--outdated], alone');
    }

    return (0, _consumer().show)({
      id,
      json,
      versions,
      remote,
      outdated,
      compare,
      detailed,
      dependents,
      dependencies
    });
  }

  report({
    component,
    componentModel,
    dependenciesInfo,
    dependentsInfo,
    json,
    versions,
    components,
    outdated,
    detailed
  }) {
    if (versions) {
      // $FlowFixMe
      return JSON.stringify(components.map(c => c.toObject()), null, '  ');
    }

    if (json) {
      const makeEnvFilesReadable = env => {
        if (!env) return undefined;

        if (env.files && env.files.length) {
          const readableFiles = env.files.map(file => file.toReadableString());
          return readableFiles;
        }

        return [];
      };

      const makeComponentReadable = comp => {
        if (!comp) return comp;
        const componentObj = comp.toObject();
        componentObj.files = comp.files.map(file => file.toReadableString());
        componentObj.dists = componentObj.dists.getAsReadable();

        if (comp.compiler) {
          componentObj.compiler.files = makeEnvFilesReadable(comp.compiler);
        }

        if (comp.tester) {
          componentObj.tester.files = makeEnvFilesReadable(comp.tester);
        }

        return componentObj;
      };

      const componentFromFileSystem = makeComponentReadable(component);

      if (dependenciesInfo) {
        componentFromFileSystem.dependenciesInfo = dependenciesInfo;
      }

      if (dependentsInfo) {
        componentFromFileSystem.dependentsInfo = dependentsInfo;
      }

      const componentFromModel = makeComponentReadable(componentModel);
      const jsonObject = componentFromModel ? {
        componentFromFileSystem,
        componentFromModel
      } : componentFromFileSystem;
      return JSON.stringify(jsonObject, null, '  ');
    }

    return (0, _componentTemplate().default)(component, componentModel, outdated, detailed, dependenciesInfo, dependentsInfo);
  }

}

exports.default = Show;