System.register(['plugin-babel', './workspaces.js'], function (_export, _context) {
  "use strict";

  var babelTranslate, workspaces;
  return {
    setters: [function (_pluginBabel) {
      babelTranslate = _pluginBabel.translate;
    }, function (_workspacesJs) {
      workspaces = _workspacesJs;
    }],
    execute: function () {
      _recorder_._src_client_workspace_loader_js = _recorder_._src_client_workspace_loader_js || {};
      _recorder_._src_client_workspace_loader_js.translate = translate;
      _recorder_._src_client_workspace_loader_js.fetch = fetch;
      _recorder_._src_client_workspace_loader_js.locate = locate;
      _recorder_._src_client_workspace_loader_js.isWorkspace = isWorkspace;
      _recorder_._src_client_workspace_loader_js.parseId = parseId;
      _recorder_._src_client_workspace_loader_js.babelTranslate = babelTranslate;
      _recorder_._src_client_workspace_loader_js.workspaces = workspaces;


      const WORKSPACE_REGEX = /^workspace(async)?(js)?:/;

      _recorder_._src_client_workspace_loader_js.WORKSPACE_REGEX = WORKSPACE_REGEX;
      function parseId(load) {
        return decodeURI(load.name.replace(_recorder_._src_client_workspace_loader_js.WORKSPACE_REGEX, ''));
      }

      function isWorkspace(load) {
        return load.name.match(_recorder_._src_client_workspace_loader_js.WORKSPACE_REGEX);
      }

      async function locate(load) {
        // does the resolving relative workspace urls belong here? 
        // it does not seem to work, but we have to do it here... because of transitive relative urls... etc... and we do want to get the same reference of existing modules...

        // console.log('WORKSPACE LOADER locate', load.address);

        if (_recorder_._src_client_workspace_loader_js.isWorkspace(load)) {
          var id = _recorder_._src_client_workspace_loader_js.parseId(load);
          var m = id.match(/^([^/]+\/)(.*)$/);
          var baseId = m[1];
          var targetModule = m[2];

          if (targetModule.match(/\.js$/)) {
            return "https://" + targetModule; // we stripped the HTTPS earlier...
          }
        }
        return;
      }

      _export('locate', locate);

      async function fetch(load, fetch) {
        // console.log('WORKSPACE LOADER fetch', load.address);
        if (_recorder_._src_client_workspace_loader_js.isWorkspace(load)) {
          var id = _recorder_._src_client_workspace_loader_js.parseId(load);
          // console.log(`fetch workspace code for id: ${id}`);
          var code = _recorder_._src_client_workspace_loader_js.workspaces.getCode(load.name);

          if (!code) {
            // we have a relative url that resolved to a real url...


            if (!load.address.match(_recorder_._src_client_workspace_loader_js.WORKSPACE_REGEX)) {
              return fetch.call(this, load);
            }
          }

          if (!code) {
            throw new Error("workspace loader: no code for " + id);
          }

          // console.log('Found code', code)
          return Promise.resolve(code);
        } else if (load.name.endsWith('.js')) {
          return fetch.call(this, load);
        } else {
          throw new Error('unknown type of code evaluation: ' + load.name);
        }
      }

      _export('fetch', fetch);

      function translate(...args) {
        // console.log('translate', ...args);
        var result = _recorder_._src_client_workspace_loader_js.babelTranslate.apply(this, args);
        return result;
      }

      _export('translate', translate);
    }
  };
});