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

  var Scheme, PolymorphicIdentifier, focalStorage, parseQuery, getDeepProperty, WikipediaScheme, __SystemJSRewritingHack;
  _export("WikipediaScheme", void 0);
  return {
    setters: [function (_srcClientPoidJs) {
      Scheme = _srcClientPoidJs.Scheme;
      PolymorphicIdentifier = _srcClientPoidJs.default;
    }, function (_srcExternalFocalStorageJs) {
      focalStorage = _srcExternalFocalStorageJs.default;
    }, function (_utils) {
      parseQuery = _utils.parseQuery;
      getDeepProperty = _utils.getDeepProperty;
    }],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._src_client_protocols_wikipedia_js = _recorder_._src_client_protocols_wikipedia_js || {};
      Object.defineProperty(_recorder_._src_client_protocols_wikipedia_js, "Scheme", {
        get() {
          return Scheme;
        },
        set(thisIsVererySecretVariableName) {
          Scheme = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_wikipedia_js, "PolymorphicIdentifier", {
        get() {
          return PolymorphicIdentifier;
        },
        set(thisIsVererySecretVariableName) {
          PolymorphicIdentifier = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_wikipedia_js, "focalStorage", {
        get() {
          return focalStorage;
        },
        set(thisIsVererySecretVariableName) {
          focalStorage = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      // #TODO conflict with src/client/poid.js#LivelyWikipedia
      Object.defineProperty(_recorder_._src_client_protocols_wikipedia_js, "getDeepProperty", {
        get() {
          return getDeepProperty;
        },
        set(thisIsVererySecretVariableName) {
          getDeepProperty = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_protocols_wikipedia_js, "parseQuery", {
        get() {
          return parseQuery;
        },
        set(thisIsVererySecretVariableName) {
          parseQuery = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      _export("WikipediaScheme", WikipediaScheme = class WikipediaScheme extends Scheme {
        get scheme() {
          return "wikipedia";
        }
        resolve() {
          return true;
        }
        async getContent() {
          let urlObj = new URL(this.url);
          var entry = urlObj.pathname.replace(/^\/*/, "");
          // window.open("https://www.wikiwand.com/" + entry)

          this.lang = entry.split("/")[0];
          var query = entry.split("/")[1];
          this.mode = "html";
          var content = await fetch(`https://${this.lang}.wikipedia.org/api/rest_v1/page/${this.mode}/${query}`).then(r => r.text());
          return content.replace(/rel="mw:WikiLink" href="\.\//g, `rel="mw:WikiLink" href="wikipedia://${this.lang}/`);
        }
        async GET(options) {
          var content = await this.getContent();
          return new Response(content, {
            status: 200,
            headers: {
              'content-type': "text/html"
            }
          });
          // var content = await fetch(`https://${lang}.wikipedia.org/api/rest_v1/page/${mode}/${query}`).then(r => r.text())
          /// return new Response(content, {status: 200})
        }

        async OPTIONS(options) {
          let urlObj = new URL(this.url);
          var content = await this.getContent();
          var roots = lively.html.parseHTML(content);
          var children = [{
            name: urlObj.pathname.replace(/.*\//, ""),
            href: this.url,
            type: "file"
          }];
          debugger;
          var navboxes = lively.html.allQuerySelectorAll(roots, ".navbox");
          var links = lively.html.allQuerySelectorAll(navboxes, "a");
          links.forEach(ea => {
            children.push({
              name: ea.textContent,
              href: ea.getAttribute("href")
            });
          });
          return new Response(JSON.stringify({
            name: urlObj.pathname,
            parent: this.url,
            // I am my own parent
            type: "file",
            contents: children
          }), {
            status: 200
          });
        }
      });
      Object.defineProperty(_recorder_._src_client_protocols_wikipedia_js, "WikipediaScheme", {
        get() {
          return WikipediaScheme;
        },
        set(thisIsVererySecretVariableName) {
          _export("WikipediaScheme", WikipediaScheme = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
      PolymorphicIdentifier.register(WikipediaScheme);
    }
  };
});