"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StartCmd = void 0;
function _bitError() {
  const data = require("@teambit/bit-error");
  _bitError = function () {
    return data;
  };
  return data;
}
function _constants() {
  const data = require("@teambit/legacy/dist/constants");
  _constants = function () {
    return data;
  };
  return data;
}
function _uiFoundationCli() {
  const data = require("@teambit/ui-foundation.cli.ui-server-console");
  _uiFoundationCli = function () {
    return data;
  };
  return data;
}
function _react() {
  const data = _interopRequireDefault(require("react"));
  _react = function () {
    return data;
  };
  return data;
}
function _openBrowser() {
  const data = _interopRequireDefault(require("react-dev-utils/openBrowser"));
  _openBrowser = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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); }
class StartCmd {
  constructor(
  /**
   * access to the extension instance.
   */
  ui, logger) {
    this.ui = ui;
    this.logger = logger;
    _defineProperty(this, "name", 'start [component-pattern]');
    _defineProperty(this, "description", 'run the ui/development server');
    _defineProperty(this, "arguments", [{
      name: 'component-pattern',
      description: _constants().COMPONENT_PATTERN_HELP
    }]);
    _defineProperty(this, "alias", 'c');
    _defineProperty(this, "group", 'development');
    _defineProperty(this, "options", [['d', 'dev', 'start UI server in dev mode.'], ['p', 'port [port-number]', 'port of the UI server.'], ['r', 'rebuild', 'rebuild the UI (useful e.g. when updating the workspace UI - can use the dev flag for HMR in this case)'], ['', 'skip-ui-build', 'skip building UI'], ['v', 'verbose', 'show verbose output for inspection and prints stack trace'], ['n', 'no-browser', 'do not automatically open browser when ready'], ['', 'skip-compilation', 'skip the auto-compilation before starting the web-server'], ['u', 'ui-root-name [type]', 'name of the ui root to use, e.g. "teambit.scope/scope" or "teambit.workspace/workspace"']]);
  }

  // async report([uiRootName, userPattern]: StartArgs, { dev, port, rebuild, verbose }: StartFlags): Promise<string> {
  //   this.logger.off();
  //   const pattern = userPattern && userPattern.toString();

  //   const uiServer = await this.ui.createRuntime({
  //     uiRootName,
  //     pattern,
  //     dev,
  //     port: port ? parseInt(port) : undefined,
  //     rebuild,
  //     verbose,
  //   });

  //   return `Bit server has started on port ${uiServer.port}`;
  // }

  async render([userPattern], {
    dev,
    port,
    rebuild,
    verbose,
    noBrowser,
    skipCompilation,
    skipUiBuild,
    uiRootName: uiRootAspectIdOrName
  }) {
    this.logger.off();
    if (!this.ui.isHostAvailable()) {
      throw new (_bitError().BitError)(`bit start can only be run inside a bit workspace or a bit scope - please ensure you are running the command in the correct directory`);
    }
    const appName = this.ui.getUiName(uiRootAspectIdOrName);
    await this.ui.invokePreStart({
      skipCompilation
    });
    const uiServer = this.ui.createRuntime({
      uiRootAspectIdOrName,
      skipUiBuild,
      pattern: userPattern,
      dev,
      port: +port,
      rebuild,
      verbose
    });
    if (!noBrowser) {
      uiServer.then(async server => {
        if (!server.buildOptions?.launchBrowserOnStart) return undefined;
        await server.whenReady;
        return (0, _openBrowser().default)(this.ui.publicUrl || server.fullUrl);
      }).catch(error => this.logger.error(error));
    }

    // DO NOT CHANGE THIS - this meant to be an async hook.
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    this.ui.invokeOnStart();
    this.ui.clearConsole();
    return /*#__PURE__*/_react().default.createElement(_uiFoundationCli().UIServerConsole, {
      appName: appName,
      futureUiServer: uiServer,
      url: this.ui.publicUrl
    });
  }
}
exports.StartCmd = StartCmd;

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