"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCredentialMetadataAsync = getCredentialMetadataAsync;
exports.credentialsExistForPlatformAsync = credentialsExistForPlatformAsync;
exports.getEncryptedCredentialsForPlatformAsync = getEncryptedCredentialsForPlatformAsync;
exports.getCredentialsForPlatform = getCredentialsForPlatform;
exports.updateCredentialsForPlatform = updateCredentialsForPlatform;
exports.removeCredentialsForPlatform = removeCredentialsForPlatform;
exports.Ios = void 0;

function _config() {
  const data = require("@expo/config");

  _config = function () {
    return data;
  };

  return data;
}

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

  _Api = function () {
    return data;
  };

  return data;
}

function _User() {
  const data = _interopRequireDefault(require("../User"));

  _User = function () {
    return data;
  };

  return data;
}

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

  Ios = function () {
    return data;
  };

  return data;
}

Object.defineProperty(exports, "Ios", {
  enumerable: true,
  get: function () {
    return Ios();
  }
});

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 }; }

async function getCredentialMetadataAsync(projectRoot, platform) {
  const {
    exp
  } = await (0, _config().readConfigJsonAsync)(projectRoot);
  const user = await _User().default.ensureLoggedInAsync();
  let {
    username
  } = user;

  if (exp.owner) {
    username = exp.owner;
  }

  const bundleIdentifier = platform === 'ios' ? exp.ios.bundleIdentifier : undefined;
  return {
    username,
    experienceName: `@${username}/${exp.slug}`,
    bundleIdentifier,
    platform
  };
}

async function credentialsExistForPlatformAsync(metadata) {
  const credentials = await fetchCredentials(metadata, false);
  return !!credentials;
}

async function getEncryptedCredentialsForPlatformAsync(metadata) {
  return fetchCredentials(metadata, false);
}

async function getCredentialsForPlatform(metadata) {
  return fetchCredentials(metadata, true);
}

async function fetchCredentials({
  username,
  experienceName,
  bundleIdentifier,
  platform
}, decrypt) {
  // this doesn't hit our mac rpc channel, so it needs significantly less debugging
  const {
    err,
    credentials
  } = await _Api().default.callMethodAsync('getCredentials', [], 'post', {
    username,
    experienceName,
    bundleIdentifier,
    platform,
    decrypt
  });

  if (err) {
    throw new Error('Error fetching credentials.');
  }

  return credentials;
}

async function updateCredentialsForPlatform(platform, newCredentials, userCredentialsIds, metadata) {
  const {
    err,
    credentials
  } = await _Api().default.callMethodAsync('updateCredentials', [], 'post', {
    credentials: newCredentials,
    userCredentialsIds,
    platform,
    ...metadata
  });

  if (err || !credentials) {
    throw new Error('Error updating credentials.');
  }
}

async function removeCredentialsForPlatform(platform, metadata) {
  // doesn't go through mac rpc, no request id needed
  const {
    err
  } = await _Api().default.callMethodAsync('deleteCredentials', [], 'post', {
    platform,
    ...metadata
  });

  if (err) {
    throw new Error('Error deleting credentials.');
  }
}
//# sourceMappingURL=../__sourcemaps__/credentials/Credentials.js.map