text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const tslib_1 = require("tslib");
  const chalk_1 = tslib_1.__importDefault(require("chalk"));
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
  const sideload = tslib_1.__importStar(require("../models/sideload"));
  const connect_1 = require("./connect");
  const setAppPackage_1 = require("./setAppPackage");
  function install(stores) {
      return (cli) => {
          const makeProgressCallback = (componentType) => {
              cli.ui.redraw(`Sideloading ${componentType}: starting...`);
              return (sent, total) => {
                  const percentComplete = Math.round((sent / total) * 100);
                  cli.ui.redraw(`Sideloading ${componentType}: ${percentComplete}% completed`);
              };
          };
          cli
              .command('install [packagePath]', 'Install an app package')
              .types({ string: ['packagePath'] })
              .action(async (args) => {
              const { appContext, hostConnections } = stores;
              const appPackage = await setAppPackage_1.setAppPackageAction(cli, appContext, args.packagePath);
              if (!appPackage)
                  return false;
              const hasApp = Object.keys(lodash_1.default.get(appPackage, 'components.device') || {}).length > 0;
              const hasCompanion = !!lodash_1.default.get(appPackage, 'components.companion');
              if (!hasApp) {
                  if (!hasCompanion) {
                      cli.activeCommand.log('Nothing to do: app contains neither a device nor a companion component');
                      return false;
                  }
                  cli.activeCommand.log(chalk_1.default.keyword('orange')('This app does not contain a device component'));
              }
              if (hasApp && (!hostConnections.appHost || hostConnections.appHost.host.rpc.ended)) {
                  cli.activeCommand.log('App requires a device, connecting...');
                  const result = await connect_1.connectAction(cli, 'device', hostConnections);
                  if (!result)
                      return false;
              }
              if (hasCompanion
                  && (!hostConnections.companionHost || hostConnections.companionHost.host.rpc.ended)) {
                  cli.activeCommand.log('App requires a phone, connecting...');
                  const result = await connect_1.connectAction(cli, 'phone', hostConnections);
                  if (!result)
                      return false;
              }
              const { appHost, companionHost } = hostConnections;
              try {
                  if (hasApp && appHost) {
                      await sideload.app(appHost.host, appPackage, makeProgressCallback('app'));
                      cli.ui.redraw.done();
                  }
                  if (hasCompanion && companionHost) {
                      await sideload.companion(companionHost.host, appPackage, makeProgressCallback('companion'));
                      cli.ui.redraw.done();
                  }
              }
              catch (ex) {
                  cli.activeCommand.log(`Install failed: ${ex.message}`);
                  return false;
              }
              if (hasApp && appHost) {
                  cli.activeCommand.log('Launching app');
                  await appHost.host.launchAppComponent({
                      uuid: appPackage.uuid,
                      component: 'app',
                  });
              }
              return true;
          });
      };
  }
  exports.default = install;
  