"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getExistingDistCerts = getExistingDistCerts;
exports.formatDistCerts = formatDistCerts;
exports.getExistingPushKeys = getExistingPushKeys;
exports.formatPushKeys = formatPushKeys;

function _Api() {
  const data = _interopRequireDefault(require("../Api"));

  _Api = function () {
    return data;
  };

  return data;
}

function _PKCS12Utils() {
  const data = require("../detach/PKCS12Utils");

  _PKCS12Utils = function () {
    return data;
  };

  return data;
}

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

async function getExistingDistCerts(username, appleTeamId, options = {}) {
  const distCerts = await getExistingUserCredentials(username, appleTeamId, 'dist-cert');
  return formatDistCerts(distCerts, options);
}

function formatDistCerts(distCerts, options) {
  return distCerts.map(({
    usedByApps,
    userCredentialsId,
    certId,
    certP12,
    certPassword
  }) => {
    let serialNumber;

    try {
      serialNumber = (0, _PKCS12Utils().findP12CertSerialNumber)(certP12, certPassword);
    } catch (error) {
      serialNumber = '------';
    }

    let name = `Serial number: ${serialNumber}`;

    if (certId) {
      name = `${name}, Certificate ID: ${certId}`;
    }

    if (usedByApps) {
      name = `Used in apps: ${usedByApps.join(', ')} (${name})`;
    }

    return {
      value: {
        distCertSerialNumber: serialNumber,
        ...(options.provideFullCertificate ? {
          certP12,
          certId,
          certPassword
        } : {
          userCredentialsId: String(userCredentialsId)
        })
      },
      name
    };
  });
}

async function getExistingPushKeys(username, appleTeamId, options = {}) {
  const pushKeys = await getExistingUserCredentials(username, appleTeamId, 'push-key');
  return formatPushKeys(pushKeys, options);
}

function formatPushKeys(pushKeys, options) {
  return pushKeys.map(({
    usedByApps,
    userCredentialsId,
    apnsKeyId,
    apnsKeyP8
  }) => {
    let name = `Key ID: ${apnsKeyId}`;

    if (usedByApps) {
      name = `Used in apps: ${usedByApps.join(', ')} (${name})`;
    }

    return {
      value: { ...(options.provideFullPushKey ? {
          apnsKeyId,
          apnsKeyP8
        } : {
          userCredentialsId: String(userCredentialsId)
        })
      },
      name,
      short: apnsKeyId
    };
  });
}

async function getExistingUserCredentials(username, appleTeamId, type) {
  const {
    err,
    certs
  } = await _Api().default.callMethodAsync('getExistingUserCredentials', [], 'post', {
    username,
    appleTeamId,
    type
  });

  if (err) {
    throw new Error('Error getting existing distribution certificates.');
  } else {
    return certs.map(({
      usedByApps,
      userCredentialsId,
      ...rest
    }) => ({
      usedByApps: usedByApps && usedByApps.split(';'),
      userCredentialsId: String(userCredentialsId),
      ...rest
    }));
  }
}
//# sourceMappingURL=../__sourcemaps__/credentials/IosCredentials.js.map