"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 _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);

  try {
    (0, _log().default)('Trying to authenticate with Apple Developer Portal...');
    const {
      teams,
      fastlaneSession
    } = await (0, _fastlane().runAction)(_fastlane().travelingFastlane.authenticate, [appleId, appleIdPassword], {
      pipeStdout: true
    });
    (0, _log().default)('Authenticated with Apple Developer Portal successfully!');
    const team = await _chooseTeam(teams, options.teamId);
    return {
      appleId,
      appleIdPassword,
      team,
      fastlaneSession
    };
  } catch (err) {
    (0, _log().default)('Authentication with Apple Developer Portal failed!');
    throw err;
  }
}

async function _requestAppleIdCreds(options) {
  return _getAppleIdFromParams(options) || (await _promptForAppleId(options));
}

function _getAppleIdFromParams({
  appleId
}) {
  const appleIdPassword = process.env.EXPO_APPLE_PASSWORD;

  if (appleId && appleIdPassword) {
    return {
      appleId,
      appleIdPassword
    };
  } else {
    return null;
  }
}

async function _promptForAppleId({
  appleId
}) {
  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.'));
  (0, _log().default)(wrap(_chalk().default.bold('The password is only used to authenticate with Apple and never stored.')));
  const {
    appleId: promptAppleId
  } = await (0, _prompt().default)({
    type: 'input',
    name: 'appleId',
    message: `Apple ID:`,
    validate: _validators().nonEmptyInput,
    when: !appleId
  }, {
    nonInteractiveHelp: 'Pass your Apple ID using the --apple-id flag.'
  });
  const {
    appleIdPassword
  } = await (0, _prompt().default)({
    type: 'password',
    name: 'appleIdPassword',
    message: answer => `Password (for ${appleId || promptAppleId}):`,
    validate: _validators().nonEmptyInput
  }, {
    nonInteractiveHelp: 'Pass your Apple ID password using the EXPO_APPLE_PASSWORD environment variable'
  });
  return {
    appleId: 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
    });
    return _formatTeam(team);
  }
}

function _formatTeam({
  teamId,
  name,
  type
}) {
  return {
    id: teamId,
    name: `${name} (${type})`,
    inHouse: type.toLowerCase() === APPLE_IN_HOUSE_TEAM_TYPE
  };
}
//# sourceMappingURL=../__sourcemaps__/appleApi/authenticate.js.map