"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.askForUserProvided = askForUserProvided;

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _untildify() {
  const data = _interopRequireDefault(require("untildify"));

  _untildify = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _get() {
  const data = _interopRequireDefault(require("lodash/get"));

  _get = function () {
    return data;
  };

  return data;
}

function _once() {
  const data = _interopRequireDefault(require("lodash/once"));

  _once = function () {
    return data;
  };

  return data;
}

function _prompt() {
  const data = _interopRequireDefault(require("../../prompt"));

  _prompt = function () {
    return data;
  };

  return data;
}

function _log() {
  const data = _interopRequireDefault(require("../../log"));

  _log = function () {
    return data;
  };

  return data;
}

function validators() {
  const data = _interopRequireWildcard(require("../../validators"));

  validators = function () {
    return data;
  };

  return data;
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const EXPERT_PROMPT = (0, _once().default)(() => _log().default.warn(`
WARNING! In this mode, we won't be able to make sure that your crdentials are valid.
Please double check that you're uploading valid files for your app otherwise you may encounter strange errors!

When building for IOS make sure you've created your App ID on the Apple Developer Portal, that your App ID
is in app.json as \`bundleIdentifier\`, and that the provisioning profile you
upload matches that Team ID and App ID.
`));

async function askForUserProvided(schema) {
  if (await willUserProvideCredentialsType(schema.name)) {
    EXPERT_PROMPT();
    return await getCredentialsFromUser(schema);
  }

  return null;
}

async function getCredentialsFromUser(credentialType) {
  const results = {};

  for (const field of credentialType.required) {
    results[field] = await askQuestionAndProcessAnswer((0, _get().default)(credentialType, `questions.${field}`));
  }

  return results;
}

async function willUserProvideCredentialsType(name) {
  const {
    answer
  } = await (0, _prompt().default)({
    type: 'list',
    name: 'answer',
    message: `Will you provide your own ${name}?`,
    choices: [{
      name: 'Let Expo handle the process',
      value: false
    }, {
      name: 'I want to upload my own file',
      value: true
    }]
  });
  return answer;
}

async function askQuestionAndProcessAnswer(definition) {
  const questionObject = buildQuestionObject(definition);
  const {
    input
  } = await (0, _prompt().default)(questionObject);
  return await processAnswer(definition, input);
}

function buildQuestionObject({
  type,
  question
}) {
  switch (type) {
    case 'string':
      return {
        type: 'input',
        name: 'input',
        message: question
      };

    case 'file':
      return {
        type: 'input',
        name: 'input',
        message: question,
        filter: produceAbsolutePath,
        validate: validators().existingFile
      };

    case 'password':
      return {
        type: 'password',
        name: 'input',
        message: question,
        validate: validators().nonEmptyInput
      };
  }
}

async function processAnswer({
  type,
  base64Encode
}, input) {
  if (type === 'file') {
    return _fsExtra().default.readFile(input, base64Encode ? 'base64' : 'utf8');
  } else {
    return input;
  }
}

function produceAbsolutePath(filePath) {
  const untildified = (0, _untildify().default)(filePath.trim());
  return !_path().default.isAbsolute(untildified) ? _path().default.resolve(untildified) : untildified;
}
//# sourceMappingURL=../../__sourcemaps__/credentials/actions/promptForCredentials.js.map