/*
 * Copyright 2015 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.TextResource;

import jsinterop.annotations.JsMethod;

/**
 * GWT compatible replacement for {@code ResourceLoader}
 *
 */
final class ResourceLoader {
  static interface Libraries extends ClientBundle {
    Libraries INSTANCE = GWT.create(Libraries.class);

    // This is a generated file containing all the text resources we want to package
    // as a single JSON string mapping (relative) filename to file content strings.
    @Source("resources.json")
    TextResource resources();
  }

  private static final JsObject RESOURCES = parse(Libraries.INSTANCE.resources().getText());

  static String loadTextResource(Class<?> clazz, String path) {
    String content = RESOURCES.get(path);
    if (content != null) {
      return content;
    }
    throw new RuntimeException("Resource not found " + path);
  }

  static boolean resourceExists(Class<?> clazz, String path) {
    // TODO(sdh): this is supposed to be relative to the given class, but
    // GWT can't handle that - probably better to remove the class argument
    // and just require that paths be relative to c.g.javascript.jscomp.
    return RESOURCES.get(path) != null;
  }

  public static class JsObject {
    public JsObject() {}

    public final native String get(String key) /*-{
      return this[key];
    }-*/;
  }

  @JsMethod(namespace = "JSON")
  private static native JsObject parse(String json);
}

@@ -1449,16 +1449,20 @@
 ntent = 
+get(
 RESOURCE
@@ -1458,29 +1458,26 @@
 et(RESOURCES
-.get(
+, 
 path);%0A    i
@@ -1890,16 +1890,20 @@
  return 
+get(
 RESOURCE
@@ -1907,13 +1907,10 @@
 RCES
-.get(
+, 
 path
@@ -1987,25 +1987,29 @@
  %7B%7D%0A
-%0A
   
-  public final
+%7D%0A%0A  private static
  nat
@@ -2023,16 +2023,30 @@
 ing get(
+JsObject obj, 
 String k
@@ -2062,41 +2062,32 @@
     
-  
 return 
-this
+obj
 %5Bkey%5D;%0A
-  
   %7D-*/;
-%0A  %7D
 %0A%0A  

Fix GWT version of transpiler to use quoted keys always.