"use strict";

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

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

  _xdl = function () {
    return data;
  };

  return data;
}

function _findIndex() {
  const data = _interopRequireDefault(require("lodash/findIndex"));

  _findIndex = function () {
    return data;
  };

  return data;
}

function _omit() {
  const data = _interopRequireDefault(require("lodash/omit"));

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class IosApi {
  constructor(user) {
    _defineProperty(this, "shouldRefetch", true);

    this.api = _xdl().ApiV2.clientForUser(user);
    this.credentials = {
      appCredentials: [],
      userCredentials: []
    };
  }

  async getAllCredentials() {
    if (this.shouldRefetch) {
      (0, _log().default)('Fetching available credentials');
      this.credentials = await this.api.getAsync('credentials/ios');
      this.shouldRefetch = false;
    }

    return this.credentials;
  }

  async createDistCert(credentials) {
    const {
      id
    } = await this.api.postAsync('credentials/ios/dist', {
      credentials
    });
    const newDistCert = { ...credentials,
      id,
      type: 'dist-cert'
    };
    this.credentials.userCredentials.push(newDistCert);
    return newDistCert;
  }

  async updateDistCert(credentialsId, credentials) {
    const {
      id
    } = await this.api.putAsync(`credentials/ios/dist/${credentialsId}`, {
      credentials
    });
    const updatedDistCert = { ...credentials,
      id,
      type: 'dist-cert'
    };
    const credIndex = (0, _findIndex().default)(this.credentials.userCredentials, ({
      id
    }) => id === credentialsId);
    this.credentials.userCredentials[credIndex] = updatedDistCert;
    return updatedDistCert;
  }

  async deleteDistCert(credentialsId) {
    await this.api.deleteAsync(`credentials/ios/dist/${credentialsId}`);
    this.credentials.userCredentials = this.credentials.userCredentials.filter(({
      id
    }) => id !== credentialsId);
    this.credentials.appCredentials = this.credentials.appCredentials.map(record => {
      if (record.distCredentialsId === credentialsId) {
        return (0, _omit().default)(record, 'distCredentialsId');
      }

      return record;
    });
  }

  async useDistCert(experienceName, bundleIdentifier, userCredentialsId) {
    await this.api.postAsync('credentials/ios/use/dist', {
      experienceName,
      bundleIdentifier,
      userCredentialsId
    });

    this._ensureAppCredentials(experienceName, bundleIdentifier);

    const credIndex = (0, _findIndex().default)(this.credentials.appCredentials, app => app.experienceName === experienceName && app.bundleIdentifier === bundleIdentifier);
    this.credentials.appCredentials[credIndex].distCredentialsId = userCredentialsId;
  }

  async createPushKey(credentials) {
    const {
      id
    } = await this.api.postAsync('credentials/ios/push', {
      credentials
    });
    const newPushKey = { ...credentials,
      id,
      type: 'push-key'
    };
    this.credentials.userCredentials.push(newPushKey);
    return newPushKey;
  }

  async updatePushKey(credentialsId, credentials) {
    const {
      id
    } = await this.api.putAsync(`credentials/ios/push/${credentialsId}`, {
      credentials
    });
    const updatedPushKey = { ...credentials,
      id,
      type: 'push-key'
    };
    const credIndex = (0, _findIndex().default)(this.credentials.userCredentials, ({
      id
    }) => id === credentialsId);
    this.credentials.userCredentials[credIndex] = updatedPushKey;
    return updatedPushKey;
  }

  async deletePushKey(credentialsId) {
    await this.api.deleteAsync(`credentials/ios/push/${credentialsId}`);
    this.credentials.userCredentials = this.credentials.userCredentials.filter(({
      id
    }) => id !== credentialsId);
    this.credentials.appCredentials = this.credentials.appCredentials.map(record => {
      if (record.pushCredentialsId === credentialsId) {
        return (0, _omit().default)(record, 'pushCredentialsId');
      }

      return record;
    });
  }

  async usePushKey(experienceName, bundleIdentifier, userCredentialsId) {
    await this.api.postAsync('credentials/ios/use/push', {
      experienceName,
      bundleIdentifier,
      userCredentialsId
    });

    this._ensureAppCredentials(experienceName, bundleIdentifier);

    const credIndex = (0, _findIndex().default)(this.credentials.appCredentials, app => app.experienceName === experienceName && app.bundleIdentifier === bundleIdentifier);
    this.credentials.appCredentials[credIndex].pushCredentialsId = userCredentialsId;
  }

  async deletePushCert(experienceName, bundleIdentifier) {
    await this.api.postAsync(`credentials/ios/pushCert/delete`, {
      experienceName,
      bundleIdentifier
    });
    const credIndex = (0, _findIndex().default)(this.credentials.appCredentials, app => app.experienceName === experienceName && app.bundleIdentifier === bundleIdentifier);
    this.credentials.appCredentials[credIndex].credentials = (0, _omit().default)(this.credentials.appCredentials[credIndex].credentials, ['pushId', 'pushP12', 'pushPassword']);
  }

  async deleteProvisioningProfile(experienceName, bundleIdentifier) {
    await this.api.postAsync(`credentials/ios/provisioningProfile/delete`, {
      experienceName,
      bundleIdentifier
    });
    const credIndex = (0, _findIndex().default)(this.credentials.appCredentials, app => app.experienceName === experienceName && app.bundleIdentifier === bundleIdentifier);
    this.credentials.appCredentials[credIndex].credentials = (0, _omit().default)(this.credentials.appCredentials[credIndex].credentials, ['provisioningProfile', 'provisioningProfileId']);
  }

  _ensureAppCredentials(experienceName, bundleIdentifier) {
    const exists = this.credentials.appCredentials.filter(i => i.experienceName === experienceName && i.bundleIdentifier === bundleIdentifier).length !== 0;

    if (!exists) {
      this.credentials.appCredentials.push({
        experienceName,
        bundleIdentifier,
        credentials: {}
      });
    }
  }

}

exports.IosApi = IosApi;
//# sourceMappingURL=../__sourcemaps__/credentials/api.js.map