"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 _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = 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;
}

function _loaderMessages() {
  const data = require("../../../cli/loader/loader-messages");

  _loaderMessages = function () {
    return data;
  };

  return data;
}

function _loader() {
  const data = _interopRequireDefault(require("../../../cli/loader"));

  _loader = 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']]);
    (0, _defineProperty2().default)(this, "loader", true);
    (0, _defineProperty2().default)(this, "migration", true);
  }

  action([id], {
    json,
    versions,
    remote = false,
    outdated = false,
    compare = false,
    detailed = false
  }) {
    function getBitComponent(allVersions) {
      if (remote) {
        _loader().default.start(_loaderMessages().BEFORE_SHOW_REMOTE);

        return (0, _consumer().getScopeComponent)({
          id,
          allVersions,
          showRemoteVersions: outdated
        }).then(component => ({
          component
        }));
      }

      return (0, _consumer().getConsumerComponent)({
        id,
        compare,
        allVersions,
        showRemoteVersions: outdated
      });
    }

    if (versions && (compare || outdated)) {
      throw new (_generalError().default)('the [--compare] or [--outdated] flag cannot be used along with --versions');
    }

    if (versions) {
      return getBitComponent(versions).then(components => ({
        components,
        versions
      }));
    }

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

    return getBitComponent().then(({
      component,
      componentModel
    }) => ({
      component,
      componentModel,
      json,
      outdated,
      detailed
    }));
  }

  report({
    component,
    componentModel,
    json,
    versions,
    components,
    outdated,
    detailed
  }) {
    if (versions) {
      if (_ramda().default.isNil(components) || _ramda().default.isEmpty(components)) {
        return 'could not find the requested component';
      } // $FlowFixMe


      return JSON.stringify(components.map(c => c.toObject()), null, '  ');
    }

    if (!component) return 'could not find the requested component';

    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);
      const componentFromModel = makeComponentReadable(componentModel);
      const jsonObject = componentFromModel ? {
        componentFromFileSystem,
        componentFromModel
      } : componentFromFileSystem;
      return JSON.stringify(jsonObject, null, '  ');
    }

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

}

exports.default = Show;