System.register(["lang", "src/external/dexie3.js", "src/client/ContextJS/src/contextjs.js"], function (_export, _context) {
  "use strict";

  var Dexie, cop, __SystemJSRewritingHack, incrementExpectedAwaits, newPSD, decrementExpectedAwaits, global, globalZone, Zone;
  function exposeGlobal(key, value) {
    Object.defineProperty(global, key, {
      configurable: true,
      enumerable: true,
      value,
      writable: true
    });
  }

  /*MD
  ## ZONES
  
  - a **minimal interface** to zones intended to be as compliant as possible to Dart's [Zone concept](https://dart.dev/articles/archive/zones)
  - intended to be exchangeable in case of future standard or library advancements
  - <span style='background: red'>!only modify if you know what you are doing and understand the **long-term consequences**!</span>
  - currently implemented as a lightweight wrapper around [Dexie.Promise](https://dexie.org/docs/Promise/Promise.PSD)
    - Caution: we <span style='background: red'>monkey-patched Dexie</span> to access `incrementExpectedAwaits` and `decrementExpectedAwaits` to correctly detect native await on primitives
  
  MD*/

  // check if `incrementExpectedAwaits` and `decrementExpectedAwaits` are available

  function withZone(scopeFunc, zoneProps = {}) {
    let returnValue;
    try {
      incrementExpectedAwaits();
      newPSD(() => {
        returnValue = scopeFunc.call();
      }, zoneProps);
      return returnValue;
    } finally {
      if (returnValue && typeof returnValue.then === 'function') {
        (async () => {
          try {
            await returnValue;
          } catch (e) {
            // #TODO why are we catching errors here?
            console.warn("Error in withZone: ", e.toString(), e.stack ? e.stack.toString() : "no stack");
            throw e;
          } finally {
            decrementExpectedAwaits();
          }
        })();
      } else {
        decrementExpectedAwaits();
      }
    }
  }
  function runZoned(fn, {
    zoneValues
  } = {}) {
    return withZone(fn, zoneValues);
  }
  _export("withZone", withZone);
  return {
    setters: [function (_lang) {}, function (_srcExternalDexie3Js) {
      Dexie = _srcExternalDexie3Js.default;
    }, function (_srcClientContextJSSrcContextjsJs) {
      cop = _srcClientContextJSSrcContextjsJs;
    }],
    execute: function () {
      __SystemJSRewritingHack = {};
      _recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js = _recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js || {};
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "runZoned", {
        get() {
          return runZoned;
        },
        set(thisIsVererySecretVariableName) {
          runZoned = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "withZone", {
        get() {
          return withZone;
        },
        set(thisIsVererySecretVariableName) {
          _export("withZone", withZone = thisIsVererySecretVariableName);
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "exposeGlobal", {
        get() {
          return exposeGlobal;
        },
        set(thisIsVererySecretVariableName) {
          exposeGlobal = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "Dexie", {
        get() {
          return Dexie;
        },
        set(thisIsVererySecretVariableName) {
          Dexie = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "cop", {
        get() {
          return cop;
        },
        set(thisIsVererySecretVariableName) {
          cop = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      ({
        incrementExpectedAwaits,
        newPSD,
        decrementExpectedAwaits
      } = Dexie.Promise);
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "decrementExpectedAwaits", {
        get() {
          return decrementExpectedAwaits;
        },
        set(thisIsVererySecretVariableName) {
          decrementExpectedAwaits = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "newPSD", {
        get() {
          return newPSD;
        },
        set(thisIsVererySecretVariableName) {
          newPSD = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "incrementExpectedAwaits", {
        get() {
          return incrementExpectedAwaits;
        },
        set(thisIsVererySecretVariableName) {
          incrementExpectedAwaits = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      global = self;
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "global", {
        get() {
          return global;
        },
        set(thisIsVererySecretVariableName) {
          global = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      if (![incrementExpectedAwaits, decrementExpectedAwaits].every(func => typeof func === 'function')) {
        throw new Error('Dexie not monkey-patched to expose `incrementExpectedAwaits` and `decrementExpectedAwaits`');
      }
      globalZone = (() => {
        let zone = Dexie.Promise.PSD;
        while (!zone.global) {
          if (zone.parent && zone.parent !== zone) {
            zone = zone.parent;
          } else {
            throw new Error('could not initialize `zones`: no global zone found');
          }
        }
        return zone;
      })();
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "globalZone", {
        get() {
          return globalZone;
        },
        set(thisIsVererySecretVariableName) {
          globalZone = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      Zone = {
        get root() {
          return globalZone;
        },
        get current() {
          return Dexie.Promise.PSD;
        }
      };
      Object.defineProperty(_recorder_._https__lively_kernel_org_lively4_lively4_ansgar_src_client_lang_lang_zone_js, "Zone", {
        get() {
          return Zone;
        },
        set(thisIsVererySecretVariableName) {
          Zone = thisIsVererySecretVariableName;
          return true;
        },
        enumerable: true,
        configurable: true
      });
      exposeGlobal('Zone', Zone);
      exposeGlobal('runZoned', runZoned);

      // ensure zones in known native promise calls
      cop.layer(window, "ZonifyNativePromisesLayer").refineClass(Response, {
        text(...rest) {
          return Promise.resolve(cop.proceed(...rest));
        },
        json(...rest) {
          return Promise.resolve(cop.proceed(...rest));
        }
      }, {
        text: {
          location: {
            file: 'no_file_given',
            end: {
              column: 3,
              line: 102
            },
            start: {
              column: 2,
              line: 100
            },
            source: ''
          },
          code: "text(...rest) {\n    return Promise.resolve(cop.proceed(...rest))\n  }"
        },
        json: {
          location: {
            file: 'no_file_given',
            end: {
              column: 3,
              line: 106
            },
            start: {
              column: 2,
              line: 104
            },
            source: ''
          },
          code: "json(...rest) {\n    return Promise.resolve(cop.proceed(...rest))\n  }"
        }
      });
      ZonifyNativePromisesLayer.beGlobal();
    }
  };
});