"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.promise.js");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StartCmd = void 0;
function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  _defineProperty2 = function () {
    return data;
  };
  return data;
}
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;
}
class StartCmd {
  constructor(
  /**
   * access to the extension instance.
   */
  ui, logger) {
    this.ui = ui;
    this.logger = logger;
    (0, _defineProperty2().default)(this, "name", 'start [component-pattern]');
    (0, _defineProperty2().default)(this, "description", 'run the ui/development server');
    (0, _defineProperty2().default)(this, "arguments", [{
      name: 'component-pattern',
      description: _constants().COMPONENT_PATTERN_HELP
    }]);
    (0, _defineProperty2().default)(this, "alias", 'c');
    (0, _defineProperty2().default)(this, "group", 'development');
    (0, _defineProperty2().default)(this, "options", [['d', 'dev', 'start UI server in dev mode.'], ['p', 'port [port-number]', 'port of the UI server.'], ['r', 'rebuild', 'rebuild the UI'], ['', '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
  }) {
    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(uiRootName);
    await this.ui.invokePreStart({
      skipCompilation
    });
    const uiServer = this.ui.createRuntime({
      uiRootName,
      skipUiBuild,
      pattern: userPattern,
      dev,
      port: +port,
      rebuild,
      verbose
    });
    if (!noBrowser) {
      uiServer.then(async server => {
        var _server$buildOptions;
        if (!((_server$buildOptions = server.buildOptions) !== null && _server$buildOptions !== void 0 && _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