text: "use strict";
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.getCustomConfig = exports.getConfig = exports.getDCLInfo = exports.loadConfig = exports.writeDCLInfo = exports.createDCLInfo = void 0;
  const path_1 = __importDefault(require("path"));
  const filesystem_1 = require("./utils/filesystem");
  const utils_1 = require("./utils");
  const moduleHelpers_1 = require("./utils/moduleHelpers");
  const env_1 = require("./utils/env");
  let networkFlag;
  let config;
  /**
   * Returns the path to the `.dclinfo` file located in the local HOME folder
   */
  function getDCLInfoPath() {
      return path_1.default.resolve((0, filesystem_1.getUserHome)(), '.dclinfo');
  }
  /**
   * Reads the contents of the `.dclinfo` file
   */
  async function readDCLInfo() {
      const filePath = getDCLInfoPath();
      try {
          const file = await (0, filesystem_1.readJSON)(filePath);
          return file;
      }
      catch (e) {
          return null;
      }
  }
  /**
   * Creates the `.dclinfo` file in the HOME directory
   */
  function createDCLInfo(dclInfo) {
      config = dclInfo;
      return (0, filesystem_1.writeJSON)(getDCLInfoPath(), dclInfo);
  }
  exports.createDCLInfo = createDCLInfo;
  /**
   * Add new configuration to `.dclinfo` file
   */
  async function writeDCLInfo(newInfo) {
      return (0, filesystem_1.writeJSON)(getDCLInfoPath(), Object.assign(Object.assign({}, config), { newInfo }));
  }
  exports.writeDCLInfo = writeDCLInfo;
  /**
   * Reads `.dclinfo` file and loads it in-memory to be sync-obtained with `getDCLInfo()` function
   */
  async function loadConfig(network) {
      networkFlag = network;
      config = (await readDCLInfo());
      return config;
  }
  exports.loadConfig = loadConfig;
  /**
   * Returns the contents of the `.dclinfo` file. It needs to be loaded first with `loadConfig()` function
   */
  function getDCLInfo() {
      return config;
  }
  exports.getDCLInfo = getDCLInfo;
  function getConfig(network = networkFlag) {
      const envConfig = getEnvConfig();
      const dclInfoConfig = getDclInfoConfig();
      const defaultConfig = getDefaultConfig(network);
      const config = Object.assign(Object.assign(Object.assign({}, defaultConfig), dclInfoConfig), envConfig);
      return config;
  }
  exports.getConfig = getConfig;
  function getCustomConfig() {
      const envConfig = getEnvConfig();
      const dclInfoConfig = getDclInfoConfig();
      return Object.assign(Object.assign({}, dclInfoConfig), envConfig);
  }
  exports.getCustomConfig = getCustomConfig;
  function getDefaultConfig(network) {
      const isMainnet = network === 'mainnet';
      return {
          userId: '',
          trackStats: false,
          provider: (0, env_1.isDevelopment)()
              ? 'https://ropsten.infura.io/'
              : 'https://mainnet.infura.io/',
          MANAToken: isMainnet
              ? '0x0f5d2fb29fb7d3cfee444a200298f468908cc942'
              : '0x2a8fd99c19271f4f04b1b7b9c4f7cf264b626edb',
          LANDRegistry: isMainnet
              ? '0xf87e31492faf9a91b02ee0deaad50d51d56d5d4d'
              : '0x7a73483784ab79257bb11b96fd62a2c3ae4fb75b',
          EstateRegistry: isMainnet
              ? '0x959e104e1a4db6317fa58f8295f586e1a978c297'
              : '0x124bf28a423b2ca80b3846c3aa0eb944fe7ebb95',
          catalystUrl: isMainnet
              ? 'https://peer.decentraland.org'
              : 'https://peer.decentraland.zone',
          dclApiUrl: isMainnet
              ? 'https://api.thegraph.com/subgraphs/name/decentraland/land-manager'
              : 'https://api.thegraph.com/subgraphs/name/decentraland/land-manager-ropsten',
          segmentKey: (0, moduleHelpers_1.isStableVersion)() && !(0, env_1.isDevelopment)()
              ? 'sFdziRVDJo0taOnGzTZwafEL9nLIANZ3'
              : 'mjCV5Dc4VAKXLJAH5g7LyHyW1jrIR3to'
      };
  }
  function getDclInfoConfig() {
      const dclInfo = getDCLInfo();
      const fileExists = !!dclInfo;
      if (!fileExists) {
          return { fileExists };
      }
      const dclInfoConfig = {
          fileExists,
          userId: dclInfo.userId,
          trackStats: !!dclInfo.trackStats,
          MANAToken: dclInfo.MANAToken,
          LANDRegistry: dclInfo.LANDRegistry,
          EstateRegistry: dclInfo.EstateRegistry,
          catalystUrl: dclInfo.catalystUrl,
          segmentKey: dclInfo.segmentKey
      };
      return (0, utils_1.removeEmptyKeys)(dclInfoConfig);
  }
  function getEnvConfig() {
      const { RPC_URL, MANA_TOKEN, LAND_REGISTRY, ESTATE_REGISTRY, CONTENT_URL, SEGMENT_KEY } = process.env;
      const envConfig = {
          provider: RPC_URL,
          MANAToken: MANA_TOKEN,
          LANDRegistry: LAND_REGISTRY,
          EstateRegistry: ESTATE_REGISTRY,
          contentUrl: CONTENT_URL,
          segmentKey: SEGMENT_KEY
      };
      return (0, utils_1.removeEmptyKeys)(envConfig);
  }
  //# sourceMappingURL=config.js.map