"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.waitForEmulator = void 0;var _ora = _interopRequireDefault(require("ora"));
var _exec = require("../../systemManager/exec");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};}

var waitForEmulator = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c, cli, command, callback) {var attempts, maxAttempts, CHECK_INTEVAL, maxErrorLength, spinner;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            attempts = 0;
            maxAttempts = 30;
            CHECK_INTEVAL = 2000;
            maxErrorLength = c.program.maxErrorLength;
            spinner = (0, _ora["default"])('Waiting for emulator to boot...').start();return _context.abrupt("return",

            new Promise(function (resolve, reject) {
              var interval = setInterval(function () {
                (0, _exec.execCLI)(c, cli, command, {
                  silent: true,
                  timeout: 10000,
                  maxErrorLength: maxErrorLength }).

                then(function (resp) {
                  if (callback(resp)) {
                    clearInterval(interval);
                    spinner.succeed();
                    return resolve(true);
                  }
                  attempts++;
                  if (attempts === maxAttempts) {
                    clearInterval(interval);
                    spinner.fail(
                    "Can't connect to the running emulator. Try restarting it.");

                    return reject(
                    "Can't connect to the running emulator. Try restarting it.");

                  }
                })["catch"](
                function () {
                  attempts++;
                  if (attempts > maxAttempts) {
                    clearInterval(interval);
                    spinner.fail(
                    "Can't connect to the running emulator. Try restarting it.");

                    return reject(
                    "Can't connect to the running emulator. Try restarting it.");

                  }
                });
              }, CHECK_INTEVAL);
            }));case 6:case "end":return _context.stop();}}}, _callee);}));return function waitForEmulator(_x, _x2, _x3, _x4) {return _ref.apply(this, arguments);};}();exports.waitForEmulator = waitForEmulator;
//# sourceMappingURL=common.js.map