text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.authenticate = authenticate;
  
  function _chalk() {
    const data = _interopRequireDefault(require("chalk"));
  
    _chalk = function () {
      return data;
    };
  
    return data;
  }
  
  function _terminalLink() {
    const data = _interopRequireDefault(require("terminal-link"));
  
    _terminalLink = function () {
      return data;
    };
  
    return data;
  }
  
  function _wordwrap() {
    const data = _interopRequireDefault(require("wordwrap"));
  
    _wordwrap = function () {
      return data;
    };
  
    return data;
  }
  
  function _fastlane() {
    const data = require("./fastlane");
  
    _fastlane = function () {
      return data;
    };
  
    return data;
  }
  
  function _validators() {
    const data = require("../validators");
  
    _validators = function () {
      return data;
    };
  
    return data;
  }
  
  function _log() {
    const data = _interopRequireDefault(require("../log"));
  
    _log = function () {
      return data;
    };
  
    return data;
  }
  
  function _prompt() {
    const data = _interopRequireDefault(require("../prompt"));
  
    _prompt = function () {
      return data;
    };
  
    return data;
  }
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  const APPLE_IN_HOUSE_TEAM_TYPE = 'in-house';
  
  async function authenticate(options = {}) {
    const {
      appleId,
      appleIdPassword
    } = await _requestAppleIdCreds(options);
    (0, _log().default)(`Authenticating to Apple Developer Portal...`); // use log instead of spinner in case we need to prompt user for 2fa
  
    try {
      const {
        teams,
        fastlaneSession
      } = await (0, _fastlane().runAction)(_fastlane().travelingFastlane.authenticate, [appleId, appleIdPassword], {
        pipeStdout: true
      });
      (0, _log().default)(_chalk().default.green('Authenticated with Apple Developer Portal successfully!'));
      const team = await _chooseTeam(teams, options.teamId);
      return {
        appleId,
        appleIdPassword,
        team,
        fastlaneSession
      };
    } catch (err) {
      (0, _log().default)(_chalk().default.red('Authentication with Apple Developer Portal failed!'));
      throw err;
    }
  }
  
  async function _requestAppleIdCreds(options) {
    return _getAppleIdFromParams(options) || (await _promptForAppleId());
  }
  
  function _getAppleIdFromParams({
    appleId,
    appleIdPassword
  }) {
    const passedAppleIdPassword = appleIdPassword || process.env.EXPO_APPLE_PASSWORD; // none of the apple id params were set, assume user has no intention of passing it in
  
    if (!appleId && !passedAppleIdPassword) {
      return null;
    } // partial apple id params were set, assume user has intention of passing it in
  
  
    if (!(appleId && passedAppleIdPassword)) {
      throw new Error('In order to provide your Apple ID credentials, you must set the --apple-id flag and set the EXPO_APPLE_PASSWORD environment variable.');
    }
  
    return {
      appleId: appleId,
      appleIdPassword: passedAppleIdPassword
    };
  }
  
  async function _promptForAppleId() {
    const wrap = (0, _wordwrap().default)(process.stdout.columns || 80);
    (0, _log().default)(wrap('Please enter your Apple Developer Program account credentials. ' + 'These credentials are needed to manage certificates, keys and provisioning profiles ' + `in your Apple Developer account.`)); // https://docs.expo.io/distribution/security/#apple-developer-account-credentials
  
    const here = (0, _terminalLink().default)('here', 'https://bit.ly/2VtGWhU');
    (0, _log().default)(wrap(_chalk().default.bold(`The password is only used to authenticate with Apple and never stored`)));
    (0, _log().default)(wrap(_chalk().default.grey(`Learn more ${here}`)));
    const {
      appleId: promptAppleId
    } = await (0, _prompt().default)({
      type: 'input',
      name: 'appleId',
      message: `Apple ID:`,
      validate: _validators().nonEmptyInput
    }, {
      nonInteractiveHelp: 'Pass your Apple ID using the --apple-id flag.'
    });
    const {
      appleIdPassword
    } = await (0, _prompt().default)({
      type: 'password',
      name: 'appleIdPassword',
      message: answer => `Password (for ${promptAppleId}):`,
      validate: _validators().nonEmptyInput
    }, {
      nonInteractiveHelp: 'Pass your Apple ID password using the EXPO_APPLE_PASSWORD environment variable'
    });
    return {
      appleId: promptAppleId,
      appleIdPassword
    };
  }
  
  async function _chooseTeam(teams, userProvidedTeamId) {
    if (teams.length === 0) {
      throw new Error(`You have no team associated with your Apple account, cannot proceed.
  (Do you have a paid Apple Developer account?)`);
    }
  
    if (userProvidedTeamId) {
      const foundTeam = teams.find(({
        teamId
      }) => teamId === userProvidedTeamId);
  
      if (foundTeam) {
        (0, _log().default)(`Using Apple Team with ID: ${userProvidedTeamId}`);
        return _formatTeam(foundTeam);
      } else {
        _log().default.warn(`Your account is not associated with Apple Team with ID: ${userProvidedTeamId}`);
      }
    }
  
    if (teams.length === 1) {
      const [team] = teams;
      (0, _log().default)(`Only 1 team associated with your account, using Apple Team with ID: ${team.teamId}`);
      return _formatTeam(team);
    } else {
      (0, _log().default)(`You have ${teams.length} teams associated with your account`);
      const choices = teams.map((team, i) => ({
        name: `${i + 1}) ${team.teamId} "${team.name}" (${team.type})`,
        value: team
      }));
      const {
        team
      } = await (0, _prompt().default)({
        type: 'list',
        name: 'team',
        message: 'Which team would you like to use?',
        choices
      }, {
        nonInteractiveHelp: 'Pass in your Apple Team ID using the --team-id flag.'
      });
      return _formatTeam(team);
    }
  }
  
  function _formatTeam({
    teamId,
    name,
    type
  }) {
    return {
      id: teamId,
      name: `${name} (${type})`,
      inHouse: type.toLowerCase() === APPLE_IN_HOUSE_TEAM_TYPE
    };
  }
  //# sourceMappingURL=authenticate.js.map