'use strict';

class EnvironmentConfigService {
  constructor() {
    // Private constructor, singleton
    this.init();
  }
  static getInstance() {
    if (!EnvironmentConfigService.instance) {
      EnvironmentConfigService.instance = new EnvironmentConfigService();
    }
    return EnvironmentConfigService.instance;
  }
  init() {
    if (!window) {
      return;
    }
    const win = window;
    const Sparkle = win.Sparkle;
    console.log('getting info');
    this.m = new Map(Object.entries(Sparkle.config));
  }
  get(key, fallback) {
    const value = this.m.get(key);
    return value !== undefined ? value : fallback;
  }
}

class AssetsService {
  constructor() {
    this.assetsList = undefined;
  }
  static getInstance() {
    if (!AssetsService.instance) {
      AssetsService.instance = new AssetsService();
    }
    return AssetsService.instance;
  }
  async assets() {
    if (this.assetsList === undefined) {
      this.assetsList = await this.init();
    }
    return this.assetsList;
  }
  async init() {
    try {
      const config = EnvironmentConfigService.getInstance().get('sparkle');
      const res = await fetch(`${config.globalAssetsUrl}/assets.json`);
      if (!res) {
        return undefined;
      }
      const assets = await res.json();
      return assets;
    }
    catch (err) {
      return undefined;
    }
  }
  async loadJsonFile(path) {
    try {
      const res = await fetch(`${path}`);
      if (!res) {
        return undefined;
      }
      const data = await res.json();
      return data;
    }
    catch (err) {
      return undefined;
    }
  }
}

exports.AssetsService = AssetsService;
exports.EnvironmentConfigService = EnvironmentConfigService;
