"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ShowCmd = void 0;
function _open() {
  const data = _interopRequireDefault(require("open"));
  _open = function () {
    return data;
  };
  return data;
}
function _lodash() {
  const data = require("lodash");
  _lodash = function () {
    return data;
  };
  return data;
}
function _cliTable() {
  const data = require("@teambit/cli-table");
  _cliTable = function () {
    return data;
  };
  return data;
}
function _legacy() {
  const data = require("@teambit/legacy.bit-map");
  _legacy = function () {
    return data;
  };
  return data;
}
function _componentId() {
  const data = require("@teambit/component-id");
  _componentId = function () {
    return data;
  };
  return data;
}
function _showLegacyCmd() {
  const data = require("./legacy-show/show-legacy-cmd");
  _showLegacyCmd = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // import { Logger } from '@teambit/logger';
// import chalk from 'chalk';
class ShowCmd {
  constructor(component, logger) {
    this.component = component;
    this.logger = logger;
    _defineProperty(this, "name", 'show <component-name>');
    _defineProperty(this, "description", "display the component's essential information");
    _defineProperty(this, "alias", '');
    _defineProperty(this, "group", 'info');
    _defineProperty(this, "arguments", [{
      name: 'component-name',
      description: 'component name or component id'
    }]);
    _defineProperty(this, "options", [['j', 'json', 'return the component data in json format'], ['l', 'legacy', 'use the legacy bit show.'], ['r', 'remote', 'show data for a remote component'], ['b', 'browser', 'open the component page in the browser'], ['c', 'compare', 'legacy-only. compare current file system component to its latest tagged version [default=latest]']]);
  }
  async getComponent(idStr, remote) {
    if (remote) {
      const id = _componentId().ComponentID.fromString(idStr); // user used --remote so we know it has a scope
      const host = this.component.getHostIfExist('teambit.scope/scope');
      if (!host) throw new Error(`error: the current directory is not a workspace nor a scope. the full "bit show" is not supported.
to see the legacy bit show, please use "--legacy" flag`);
      if (!host.getRemoteComponent) {
        throw new Error('Component Host does not implement getRemoteComponent()');
      }
      const component = await host.getRemoteComponent(id);
      return component;
    }
    const host = this.component.getHost();
    const id = await host.resolveComponentId(idStr);
    const component = await host.get(id);
    if (!component) throw new (_legacy().MissingBitMapComponent)(idStr);
    return component;
  }
  async useLegacy(id, json = false, remote = false, compare = false) {
    const showData = await (0, _showLegacyCmd().actionLegacy)([id], {
      json,
      remote,
      compare
    });
    return (0, _showLegacyCmd().reportLegacy)(showData);
  }
  async report([idStr], {
    legacy,
    remote,
    compare,
    browser
  }) {
    if (legacy) return this.useLegacy(idStr, false, remote, compare);
    const component = await this.getComponent(idStr, remote);
    const fragments = this.component.getShowFragments();
    const rows = await Promise.all(fragments.map(async fragment => {
      const row = await fragment.renderRow(component);
      if (!row.content) return null;
      return [row.title, row.content];
    }));
    const table = new (_cliTable().CLITable)([], (0, _lodash().compact)(rows));
    const renderedTable = table.render();
    if (browser) {
      const isExported = component.isExported();
      if (!isExported) {
        this.logger.consoleWarning(`unable to open the browser, the component "${idStr}" has not been exported yet`);
        return renderedTable;
      }
      const homepageUrl = component.homepage;
      if (!homepageUrl || homepageUrl === '') {
        this.logger.consoleWarning(`unable to open browser, the component ${idStr} does not have a homepage`);
        return renderedTable;
      }
      (0, _open().default)(homepageUrl).catch(err => {
        this.logger.error(`failed to open browser for component ${idStr}, err: ${err}`);
      });
    }
    return renderedTable;
  }
  async json([idStr], {
    remote,
    legacy
  }) {
    if (legacy) return JSON.parse(await this.useLegacy(idStr, true, remote));
    const component = await this.getComponent(idStr, remote);
    const fragments = this.component.getShowFragments();
    const rows = await Promise.all(fragments.map(async fragment => {
      return fragment.json ? fragment.json(component) : undefined;
    }));
    return rows.filter(row => !!row);
  }
}
exports.ShowCmd = ShowCmd;

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