"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 _xdl() {
  const data = require("@expo/xdl");

  _xdl = function () {
    return data;
  };

  return data;
}

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

  _chalk = function () {
    return data;
  };

  return data;
}

function _utils() {
  const data = require("./utils");

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

class BaseUploader {
  constructor(platform, projectDir, options) {
    this.platform = platform;
    this.projectDir = projectDir;
    this.options = options; // it has to happen in constructor because we don't want to load this module on a different platform than darwin

    this.fastlane = require('@expo/traveling-fastlane-darwin')();
  }

  async upload() {
    await this._getProjectConfig();
    const buildPath = await this._getBinaryFilePath();
    const platformData = await this._getPlatformSpecificOptions();
    await this._uploadToTheStore(platformData, buildPath);
    await this._removeBuildFileIfDownloaded(buildPath);
    (0, _log().default)(`Please also see our docs (${_chalk().default.underline('https://docs.expo.io/versions/latest/distribution/uploading-apps/')}) to learn more about the upload process.`);
  }

  async _getProjectConfig() {
    const {
      exp
    } = await _xdl().ProjectUtils.readConfigJsonAsync(this.projectDir);

    if (!exp) {
      throw new Error(`Couldn't read project config file in ${this.projectDir}.`);
    }

    this._ensureExperienceIsValid(exp);

    this._exp = exp;
  }

  async _getBinaryFilePath() {
    const {
      path,
      id
    } = this.options;

    if (path) {
      return path;
    } else if (id) {
      return this._downloadBuildById(id);
    } else {
      return this._downloadLastestBuild();
    }
  }

  async _downloadBuildById(id) {
    const {
      platform
    } = this;
    const {
      slug
    } = this._exp;
    const build = await _xdl().StandaloneBuild.getStandaloneBuilds({
      id,
      slug,
      platform
    });

    if (!build) {
      throw new Error(`We couldn't find build with id ${id}`);
    }

    return this._downloadBuild(build.artifacts.url);
  }

  async _downloadLastestBuild() {
    const {
      platform
    } = this;
    const {
      slug
    } = this._exp;
    const build = await _xdl().StandaloneBuild.getStandaloneBuilds({
      slug,
      platform,
      limit: 1
    });

    if (!build) {
      throw new Error(`There are no builds on the Expo servers, please run 'expo build:${platform}' first`);
    }

    return this._downloadBuild(build.artifacts.url);
  }

  async _downloadBuild(urlOrPath) {
    const filename = _path().default.basename(urlOrPath);

    const destinationPath = `/tmp/${filename}`;

    if (await _fsExtra().default.exists(destinationPath)) {
      await _fsExtra().default.remove(destinationPath);
    }

    if (urlOrPath.startsWith('/')) {
      await _fsExtra().default.copy(urlOrPath, destinationPath);
      return destinationPath;
    } else {
      (0, _log().default)(`Downloading build from ${urlOrPath}`);
      return await (0, _utils().downloadFile)(urlOrPath, destinationPath);
    }
  }

  async _removeBuildFileIfDownloaded(buildPath) {
    if (!this.options.path) {
      await _fsExtra().default.remove(buildPath);
    }
  }

  _ensureExperienceIsValid() {
    throw new Error('Not implemented');
  }

  _getPlatformSpecificOptions() {
    throw new Error('Not implemented');
  }

  _uploadToTheStore(platformData, buildPath) {
    throw new Error('Not implemented');
  }

}

exports.default = BaseUploader;
//# sourceMappingURL=../../__sourcemaps__/commands/upload/BaseUploader.js.map