package com.airlock.iam.common.infrastructure.i18n;

import com.airlock.iam.common.infrastructure.json.ResourceBundleToJson;
import com.airlock.iam.core.misc.resources.MedusaResourceBundle;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Locale;
import java.util.Map;

public class ResourceBundlerBasedJsonLoader implements I18nJsonLoader {
   private ResourceBundleLoader bundleLoader = new MedusaResourceBundleLoader();
   private ResourceBundleToJson bundleToJson = new ResourceBundleToJson();

   @Override
   public Map<String, Object> loadJson(String name) {
      MedusaResourceBundle resourceBundle = this.getResourceBundle(name);
      return this.bundleToJson.toJsonObject(resourceBundle);
   }

   private MedusaResourceBundle getResourceBundle(String name) {
      return this.bundleLoader.getResourceBundle(name, this.getLocale(name));
   }

   private Locale getLocale(String name) {
      String language = name.substring(name.lastIndexOf(47) + 1, name.lastIndexOf(46));
      return Locale.of(language);
   }

   @SuppressFBWarnings(justification = "generated code")
   public ResourceBundlerBasedJsonLoader() {
   }

   @SuppressFBWarnings(justification = "generated code")
   public ResourceBundlerBasedJsonLoader(ResourceBundleLoader bundleLoader, ResourceBundleToJson bundleToJson) {
      this.bundleLoader = bundleLoader;
      this.bundleToJson = bundleToJson;
   }
}
