"use strict";

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

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

  _chalk = 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 _appleApi() {
  const data = require("../../appleApi");

  _appleApi = function () {
    return data;
  };

  return data;
}

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

class RemoveProvisioningProfile {
  constructor(shouldRevoke = false) {
    this.shouldRevoke = shouldRevoke;
  }

  async open(ctx) {
    const selected = await selectProfileFromList(ctx.ios.credentials);

    if (selected) {
      await this.removeSpecific(ctx, selected);
      (0, _log().default)(_chalk().default.green(`Successfully removed Provisioning Profile for ${selected.experienceName} (${selected.bundleIdentifier})`));
    }

    return null;
  }

  async removeSpecific(ctx, selected) {
    await ctx.ios.deleteProvisioningProfile(selected.experienceName, selected.bundleIdentifier);
    const {
      revoke
    } = await (0, _prompt().default)([{
      type: 'confirm',
      name: 'revoke',
      message: 'Do you also want to revoke it on Apple Developer Portal?',
      when: !this.shouldRevoke
    }]);

    if (revoke || this.shouldRevoke) {
      await ctx.ensureAppleCtx();
      const ppManager = new (_appleApi().ProvisioningProfileManager)(ctx.appleCtx);
      await ppManager.revoke(selected.bundleIdentifier);
    }
  }

}

exports.RemoveProvisioningProfile = RemoveProvisioningProfile;

async function selectProfileFromList(iosCredentials) {
  const profiles = iosCredentials.appCredentials.filter(({
    credentials
  }) => !!credentials.provisioningProfile && !!credentials.provisioningProfileId);

  if (profiles.length === 0) {
    _log().default.warn('There are no Provisioning Profiles available in your account');

    return null;
  }

  const getName = profile => {
    const id = _chalk().default.green(profile.credentials.provisioningProfileId || '-----');

    const teamId = profile.credentials.teamId || '------';
    return `Provisioning Profile (ID: ${id}, Team ID: ${teamId})`;
  };

  const question = {
    type: 'list',
    name: 'credentialsIndex',
    message: 'Select Provisioning Profile from the list.',
    choices: profiles.map((entry, index) => ({
      name: getName(entry),
      value: index
    }))
  };
  const {
    credentialsIndex
  } = await (0, _prompt().default)(question);
  return profiles[credentialsIndex];
}
//# sourceMappingURL=../../__sourcemaps__/credentials/views/IosProvisioningProfile.js.map