System.register(["src/client/poid.js", "src/external/focalStorage.js", "src/client/literature.js", "src/external/lodash/lodash.js"], function (_export, _context) {
  "use strict";

  var Scheme, PolymorphicIdentifier, focalStorage, Author, Paper, MicrosoftAcademicEntities, _, SemanticScholarScheme, __SystemJSRewritingHack, semanticScholarSubscriptionKeyId;
  _export("default", void 0);
  return {
    setters: [function (_srcClientPoidJs) {
      Scheme = _srcClientPoidJs.Scheme;
      PolymorphicIdentifier = _srcClientPoidJs.default;
    }, function (_srcExternalFocalStorageJs) {
      focalStorage = _srcExternalFocalStorageJs.default;
    }, function (_srcClientLiteratureJs) {
      Author = _srcClientLiteratureJs.Author;
      Paper = _srcClientLiteratureJs.Paper;
      MicrosoftAcademicEntities = _srcClientLiteratureJs.MicrosoftAcademicEntities;
    }, function (_srcExternalLodashLodashJs) {
      _ = _srcExternalLodashLodashJs.default;
    }],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._src_client_protocols_scholar_js = _recorder_._src_client_protocols_scholar_js || {};
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "Scheme", {
        get() {
          return Scheme;
        },
        set(thisIsVererySecretVariableName) {
          Scheme = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "PolymorphicIdentifier", {
        get() {
          return PolymorphicIdentifier;
        },
        set(thisIsVererySecretVariableName) {
          PolymorphicIdentifier = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "focalStorage", {
        get() {
          return focalStorage;
        },
        set(thisIsVererySecretVariableName) {
          focalStorage = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "MicrosoftAcademicEntities", {
        get() {
          return MicrosoftAcademicEntities;
        },
        set(thisIsVererySecretVariableName) {
          MicrosoftAcademicEntities = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "Paper", {
        get() {
          return Paper;
        },
        set(thisIsVererySecretVariableName) {
          Paper = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "Author", {
        get() {
          return Author;
        },
        set(thisIsVererySecretVariableName) {
          Author = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      /*MD 
      # Semantic Scholar API 
      
      Examples: 
      ```
      fetch("scholar://paper/search?query=literature+graph").then(r => r.json())
      
      var fields = "externalIds,url,title,abstract,venue,year,referenceCount,citationCount,influentialCitationCount,isOpenAccess,fieldsOfStudy,s2FieldsOfStudy,authors"
      fetch(`scholar://paper/649def34f8be52c8b66281af98ae884c09aef38b?fields=${fields}`).then(r => r.json())
      
      
      fetch("scholar://data/paper/DOI:10.1145/2384592.2384611").then(r => r.json())
      
      fetch("scholar://data/paper/MAG:2087784813").then(r => r.json())
      ```
      <script>
        <div>
          <button click={() => {
           lively.openBrowser("scholar://data/paper/DOI:10.1145/2384592.2384611")     
              }}>DOI example</button>
          <button click={() => {
           lively.openBrowser("scholar://data/paper/MAG:2087784813")     
          }}>Microsoft Academic Graph example</button>
          <button click={() => {
           lively.openBrowser("scholar://browse/paper/MAG:2087784813")     
          }}>Browse example</button>
        </div>
      </script>
      
      MD*/
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "_", {
        get() {
          return _;
        },
        set(thisIsVererySecretVariableName) {
          _ = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      semanticScholarSubscriptionKeyId = "semantic-scholar-key";
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "semanticScholarSubscriptionKeyId", {
        get() {
          return semanticScholarSubscriptionKeyId;
        },
        set(thisIsVererySecretVariableName) {
          semanticScholarSubscriptionKeyId = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      _export("default", SemanticScholarScheme = class SemanticScholarScheme extends Scheme {
        get scheme() {
          return "scholar";
        }
        resolve() {
          return true;
        }
        static async setSubscriptionKey(key) {
          return focalStorage.setItem(semanticScholarSubscriptionKeyId, key);
        }
        static async getSubscriptionKey() {
          var key = await focalStorage.getItem(semanticScholarSubscriptionKeyId);
          return key;
        }
        static async ensureSubscriptionKey() {
          var key = await this.getSubscriptionKey();
          if (!key) {
            key = await lively.prompt(`Enter your Semantic Scholar key`, "");
            await this.setSubscriptionKey(key);
          }
          return key;
        }
        response(content, contentType = "text/html") {
          return new Response(content, {
            headers: {
              "content-type": contentType
            },
            status: 200
          });
        }
        notfound(content, contentType = "text/html") {
          return new Response(content, {
            headers: {
              "content-type": contentType
            },
            status: 303
          });
        }
        get baseURL() {
          return "https://api.semanticscholar.org/graph/v1/";
        }
        async GET(options) {
          var m = this.url.match(new RegExp(this.scheme + "\:\/\/([^/]*)/(.*)"));
          var mode = m[1];
          var query = m[2];
          if (query.length < 2) return this.response(`{"error": "query to short"}`);
          if (mode === "browse") {
            if (query.match(/author\/search\?query=/)) {
              let search = decodeURIComponent(query.replace(/.*\?query=/, ""));
              return this.response(`<literature-paper authorsearch="${search}"><literature-paper>`);
            } else if (query.match(/search\?query=/)) {
              let search = decodeURIComponent(query.replace(/.*\?query=/, ""));
              return this.response(`<literature-paper search="${search}"><literature-paper>`);
            } else if (query.match("paper/")) {
              let id = query.replace(/.*\?query=/, "").replace(/paper\//, "");
              return this.response(`<literature-paper scholarid="${id}"><literature-paper>`);
            } else if (query.match("author/")) {
              var authorId = query.replace(/.*author\//, "");
              return this.response(`<literature-paper authorid="${authorId}"><literature-paper>`);
            } else {
              return this.response(`query not supported: ` + query);
            }
          }
          var url = this.baseURL + query;
          var key = await SemanticScholarScheme.ensureSubscriptionKey(); // maybe only get... ?
          var headers = new Headers({});
          if (key) {
            headers.set("x-api-key", key);
          }
          var content = await fetch(url, {
            method: "GET",
            headers: headers
          }).then(r => r.text());
          return this.response(content);
        }

        /*MD ## Example
        ```javascript {.snippet}
        fetch("scholar://data/paper/batch?fields=referenceCount,citationCount,title", {
        method: "POST",
        body: JSON.stringify({"ids": ["649def34f8be52c8b66281af98ae884c09aef38b", "ARXIV:2106.15928"]})
        }).then(r => r.text())  
        
        ```
        
        
        MD*/
        async POST(options) {
          // #TODO get rid of duplication with GET
          var m = this.url.match(new RegExp(this.scheme + "\:\/\/([^/]*)/(.*)"));
          var mode = m[1];
          var query = m[2];
          if (query.length < 2) return this.response(`{"error": "query to short"}`);
          var url = this.baseURL + query;
          var key = await SemanticScholarScheme.ensureSubscriptionKey(); // maybe only get... ?
          var headers = new Headers({});
          if (key) {
            headers.set("x-api-key", key);
          }
          var content = await fetch(url, {
            method: "POST",
            headers: headers,
            body: options.body
          }).then(r => r.text());
          return this.response(content);
        }
        async OPTIONS(options) {
          var content = JSON.stringify({}, undefined, 2);
          return new Response(content, {
            headers: {
              "content-type": "application/json"
            },
            status: 200
          });
        }
      });
      Object.defineProperty(_recorder_._src_client_protocols_scholar_js, "SemanticScholarScheme", {
        get() {
          return SemanticScholarScheme;
        },
        set(thisIsVererySecretVariableName) {
          _export("default", SemanticScholarScheme = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
      PolymorphicIdentifier.register(SemanticScholarScheme);

      // import Tracing from "src/client/tracing.js"
      // Tracing.traceClass(Paper)
    }
  };
});