"use strict";

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

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

  _path = function () {
    return data;
  };

  return data;
}

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

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _xdl() {
  const data = require("@expo/xdl");

  _xdl = function () {
    return data;
  };

  return data;
}

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

  _log = function () {
    return data;
  };

  return data;
}

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

async function fetchIosCerts(projectDir) {
  const {
    args: {
      remotePackageName
    }
  } = await _xdl().Exp.getPublishInfoAsync(projectDir);

  const inProjectDir = filename => _path().default.resolve(projectDir, filename);

  const credentialMetadata = await _xdl().Credentials.getCredentialMetadataAsync(projectDir, 'ios');
  const {
    experienceName
  } = credentialMetadata;
  (0, _log().default)(`Retrieving iOS credentials for ${credentialMetadata.experienceName}`);

  try {
    const {
      certP12,
      certPassword,
      certPrivateSigningKey,
      apnsKeyId,
      apnsKeyP8,
      pushP12,
      pushPassword,
      pushPrivateSigningKey,
      provisioningProfile,
      teamId
    } = await _xdl().Credentials.getCredentialsForPlatform(credentialMetadata);

    if (teamId !== undefined) {
      (0, _log().default)(`These credentials are associated with Apple Team ID: ${teamId}`);
    }

    if (certP12) {
      const distPath = inProjectDir(`${remotePackageName}_dist.p12`);
      await _fsExtra().default.writeFile(distPath, Buffer.from(certP12, 'base64'));
    }

    if (certPrivateSigningKey) {
      const distPrivateKeyPath = inProjectDir(`${remotePackageName}_dist_cert_private.key`);
      await _fsExtra().default.writeFile(distPrivateKeyPath, certPrivateSigningKey);
    }

    if (certP12 || certPrivateSigningKey) {
      (0, _log().default)('Wrote distribution cert credentials to disk.');
    }

    if (apnsKeyP8) {
      const apnsKeyP8Path = inProjectDir(`${remotePackageName}_apns_key.p8`);
      await _fsExtra().default.writeFile(apnsKeyP8Path, apnsKeyP8);
      (0, _log().default)('Wrote push key credentials to disk.');
    }

    if (pushP12) {
      const pushPath = inProjectDir(`${remotePackageName}_push.p12`);
      await _fsExtra().default.writeFile(pushPath, Buffer.from(pushP12, 'base64'));
    }

    if (pushPrivateSigningKey) {
      const pushPrivateKeyPath = inProjectDir(`${remotePackageName}_push_cert_private.key`);
      await _fsExtra().default.writeFile(pushPrivateKeyPath, pushPrivateSigningKey);
    }

    if (pushP12 || pushPrivateSigningKey) {
      (0, _log().default)('Wrote push cert credentials to disk.');
    }

    if (provisioningProfile) {
      const provisioningProfilePath = _path().default.resolve(projectDir, `${remotePackageName}.mobileprovision`);

      await _fsExtra().default.writeFile(provisioningProfilePath, Buffer.from(provisioningProfile, 'base64'));
      (0, _log().default)('Wrote provisioning profile to disk');
    }

    (0, _log().default)(`Save these important values as well:

Distribution P12 password: ${certPassword ? _chalk().default.bold(certPassword) : _chalk().default.yellow('(not available)')}
Push Key ID:               ${apnsKeyId ? _chalk().default.bold(apnsKeyId) : _chalk().default.yellow('(not available)')}
Push P12 password:         ${pushPassword ? _chalk().default.bold(pushPassword) : _chalk().default.yellow('(not available)')}
`);
  } catch (e) {
    throw new Error('Unable to fetch credentials for this project. Are you sure they exist?');
  }

  (0, _log().default)('All done!');
}

var _default = fetchIosCerts;
exports.default = _default;
//# sourceMappingURL=../../__sourcemaps__/commands/fetch/ios.js.map