System.register(['reactive-jsx', 'src/client/reactive/active-expression/ae-registry.js', 'src/client/reactive/components/basic/aexpr-debugging-utils.js', 'utils'], function (_export, _context) {
  "use strict";

  var _childSpread, _childExpression, _attributeStringLiteral, _childElement, _childText, _children, _attributes, _element, AExprRegistry, toValueString, pluralize;

  return {
    setters: [function (_reactiveJsx) {
      _childSpread = _reactiveJsx.childSpread;
      _childExpression = _reactiveJsx.childExpression;
      _attributeStringLiteral = _reactiveJsx.attributeStringLiteral;
      _childElement = _reactiveJsx.childElement;
      _childText = _reactiveJsx.childText;
      _children = _reactiveJsx.children;
      _attributes = _reactiveJsx.attributes;
      _element = _reactiveJsx.element;
    }, function (_srcClientReactiveActiveExpressionAeRegistryJs) {
      AExprRegistry = _srcClientReactiveActiveExpressionAeRegistryJs.AExprRegistry;
    }, function (_srcClientReactiveComponentsBasicAexprDebuggingUtilsJs) {
      toValueString = _srcClientReactiveComponentsBasicAexprDebuggingUtilsJs.toValueString;
    }, function (_utils) {
      pluralize = _utils.pluralize;
    }],
    execute: function () {
      const __SystemJSRewritingHack = {};
      _recorder_._src_client_reactive_active_expression_events_event_js = _recorder_._src_client_reactive_active_expression_events_event_js || {};
      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'humanizePosition', {
        get() {
          return humanizePosition;
        },

        set(thisIsVererySecretVariableName) {
          humanizePosition = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'fileNameString', {
        get() {
          return fileNameString;
        },

        set(thisIsVererySecretVariableName) {
          fileNameString = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'AExprRegistry', {
        get() {
          return AExprRegistry;
        },

        set(thisIsVererySecretVariableName) {
          AExprRegistry = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'toValueString', {
        get() {
          return toValueString;
        },

        set(thisIsVererySecretVariableName) {
          toValueString = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'pluralize', {
        get() {
          return pluralize;
        },

        set(thisIsVererySecretVariableName) {
          pluralize = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });


      let eventCounter = 0;

      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'eventCounter', {
        get() {
          return eventCounter;
        },

        set(thisIsVererySecretVariableName) {
          eventCounter = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      const EventTypes = {
        //AE
        CREATED: "created",
        DISPOSED: "disposed",
        CHANGED: "changed value",
        EVALFAIL: "evaluation failed",
        CBADDED: "callback added",
        CBREMOVED: "callback removed",
        DEPCHANGED: "dependencies changed",
        //ILA
        REFINE: "refine function",
        UNREFINE: "unrefine function",
        LAYERCREATED: "layer created",
        //CUSTON
        CUSTOM: "custom mark"
      };

      _export('EventTypes', EventTypes);

      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'EventTypes', {
        get() {
          return EventTypes;
        },

        set(thisIsVererySecretVariableName) {
          _export('EventTypes', EventTypes = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
      function fileNameString(file) {
        return file.substring(file.lastIndexOf('/') + 1);
      }

      function humanizePosition(file, line) {
        return _element('div', _attributes(), _children(_childText('in '), _childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#0000FF')), _children(_childExpression(fileNameString(file))), {
          file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
          end: {
            column: 74,
            line: 29
          },
          start: {
            column: 17,
            line: 29
          }
        })), _childText(' line '), _childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#0000FF')), _children(_childExpression(line)), {
          file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
          end: {
            column: 121,
            line: 29
          },
          start: {
            column: 80,
            line: 29
          }
        }))), {
          file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
          end: {
            column: 127,
            line: 29
          },
          start: {
            column: 9,
            line: 29
          }
        });
      }

      class Event {
        constructor(ae, value, type, overrideTimestamp = undefined) {
          this.timestamp = overrideTimestamp || new Date();
          this.overallID = eventCounter;
          eventCounter++;

          this.type = type;
          this.valuePromise = Promise.resolve(value).then(resolvedValue => {
            this.value = resolvedValue;
          });
          this.setAE(ae);
        }

        setAE(ae) {
          if (!ae) return;
          const eventIDInAE = ae.addEvent(this);

          this.id = ae.meta().get('id') + "-" + eventIDInAE;
          this.ae = ae;

          this.valuePromise.then(() => {
            AExprRegistry.eventListeners().forEach(listener => listener.callback(this.ae, this));
          });
        }

        async ensureResolved() {
          await this.valuePromise;
          return this;
        }

        typeName() {
          if (this.ae.isILA() && this.type === EventTypes.CHANGED) {
            return this.value && this.value.value ? "layer enabled" : "layer disabled";
          }
          return this.type;
        }

        getColor() {
          switch (this.type) {
            case EventTypes.CREATED:
              return 'green';
            case EventTypes.LAYERCREATED:
              return 'lightgreen';
            case EventTypes.DISPOSED:
            case EventTypes.EVALFAIL:
              return 'red';
            case EventTypes.CHANGED:
              return 'blue';
            case EventTypes.CBADDED:
            case EventTypes.REFINE:
              return 'yellow';
            case EventTypes.CBREMOVED:
            case EventTypes.UNREFINE:
              return 'purple';
            case EventTypes.DEPCHANGED:
              return 'orange';
            case EventTypes.CUSTOM:
              return 'gray';
            default:
              return 'black';
          }
        }

        valueString(value) {
          if (value === undefined) {
            if (this.value === undefined) {
              value = undefined;
            } else {
              value = this.value.value;
            }
          }
          if (this.ae.isILA()) return value ? "on" : "off";
          return toValueString(value);
        }

        extractLayererdFunctions() {
          if (this.ae.isILA()) {
            const result = new Map();
            for (const event of this.ae.events) {
              if (event === this) break;
              if (event.type === EventTypes.REFINE) {
                const functionMap = result.getOrCreate(event.value.obj, () => new Map());
                Object.keys(event.value.functions).forEach(key => {
                  functionMap.set(key, { function: event.value.functions[key], debugInfo: event.value.debugInfo[key] });
                });
              } else if (event.type === EventTypes.UNREFINE) {
                result.set(event.value.obj, new Map());
              }
            }
            //const layer = this.ae.getLayer();
            return result;
          }
          return undefined;
        }

        layeredFunctionsString() {
          const layeredFunctions = this.extractLayererdFunctions();
          if (!layeredFunctions) return "";
          return _element('div', _attributes(), _children(_childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#AA00AA')), _children(_childExpression(pluralize([...layeredFunctions.values()].reduce((p, c) => p += c.size, 0), "active method"))), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
            end: {
              column: 144,
              line: 131
            },
            start: {
              column: 16,
              line: 131
            }
          })), _childText(' in '), _childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#AA00AA')), _children(_childExpression(pluralize(layeredFunctions.size, "object"))), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
            end: {
              column: 227,
              line: 131
            },
            start: {
              column: 148,
              line: 131
            }
          })), _childElement(_element('br', _attributes(), _children(), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
            end: {
              column: 232,
              line: 131
            },
            start: {
              column: 227,
              line: 131
            }
          }))), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
            end: {
              column: 238,
              line: 131
            },
            start: {
              column: 11,
              line: 131
            }
          });
        }

        async humanizedData() {
          try {

            switch (this.type) {
              case EventTypes.CHANGED:
                return _element('div', _attributes(), _children(_childText('\n          '), _childSpread(this.value.triggers.map(({ location }) => humanizePosition(location.file, location.start.line))), _childText(' \n          '), _childElement(_element('br', _attributes(), _children(), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 16,
                    line: 141
                  },
                  start: {
                    column: 10,
                    line: 141
                  }
                })), _childText(' \n          '), _childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#00AAAA')), _children(_childExpression(this.valueString(this.value.lastValue))), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 85,
                    line: 142
                  },
                  start: {
                    column: 10,
                    line: 142
                  }
                })), _childText(' \u2192 '), _childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#00AAAA')), _children(_childExpression(this.valueString(this.value && this.value.value))), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 173,
                    line: 142
                  },
                  start: {
                    column: 88,
                    line: 142
                  }
                })), _childText('\n          '), _childElement(_element('br', _attributes(), _children(), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 16,
                    line: 143
                  },
                  start: {
                    column: 10,
                    line: 143
                  }
                })), _childText(' \n          '), _childExpression(this.layeredFunctionsString()), _childText('\n          '), _childExpression(this.value.triggers[0].hook.informationString()), _childText('\n        ')), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 14,
                    line: 146
                  },
                  start: {
                    column: 15,
                    line: 139
                  }
                });
              //Todo: Join trigger hook informationString
              case EventTypes.EVALFAIL:
                return _element('div', _attributes(), _children(_childText('\n          '), _childExpression(this.value.triggers ? this.value.triggers.map(({ location }) => humanizePosition(location.file, location.start.line)) : ""), _childText(' \n          '), _childElement(_element('br', _attributes(), _children(), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 16,
                    line: 151
                  },
                  start: {
                    column: 10,
                    line: 151
                  }
                })), _childText(' \n          '), _childExpression(this.value.error.name + ": " + this.value.error.message), _childText('\n        ')), {
                  file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                  end: {
                    column: 14,
                    line: 153
                  },
                  start: {
                    column: 15,
                    line: 149
                  }
                });
              case EventTypes.CREATED:
                {
                  const ae = this.ae;
                  /*const stack = this.value.stack;
                  const locations = await Promise.all(stack.frames.map(frame => frame.getSourceLoc()));
                  return locations.map(location => this.humanizePosition(location.source, location.line));*/
                  const location = ae.meta().get("location");
                  return _element('div', _attributes(), _children(_childText('\n            '), _childElement(_element('span', _attributes(_attributeStringLiteral('style', 'color:#00AAAA')), _children(_childExpression(this.valueString(this.value.lastValue))), {
                    file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                    end: {
                      column: 87,
                      line: 162
                    },
                    start: {
                      column: 12,
                      line: 162
                    }
                  })), _childText('\n            '), _childExpression(this.layeredFunctionsString()), _childText('\n            '), _childExpression(humanizePosition(location.file, location.start.line)), _childText('\n          ')), {
                    file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                    end: {
                      column: 16,
                      line: 165
                    },
                    start: {
                      column: 17,
                      line: 161
                    }
                  });
                }
              case EventTypes.DISPOSED:
                {
                  const ae = this.value;
                  const location = ae.meta().get("location");
                  return humanizePosition(location.file, location.start.line);
                }
              case EventTypes.DEPCHANGED:
                {
                  return _element('div', _attributes(), _children(_childText('\n              Added: '), _childExpression(this.value.added.length), _childText('\n              Removed: '), _childExpression(this.value.removed.length), _childText('\n              Matching: '), _childExpression(this.value.matching.length), _childText('\n            ')), {
                    file: 'https://lively-kernel.org/lively4/composed-offset/src/client/reactive/active-expression/events/event.js',
                    end: {
                      column: 18,
                      line: 179
                    },
                    start: {
                      column: 17,
                      line: 175
                    }
                  });
                }
              case EventTypes.CBADDED:
              case EventTypes.CBREMOVED:
              case EventTypes.REFINE:
              case EventTypes.UNREFINE:
              case EventTypes.LAYERCREATED:
              case EventTypes.CUSTOM:
              default:
                return (this.value || "").toString();
            }
          } catch (e) {
            // #TODO better guard against undefined this.value than this
            debugger;
            return "";
          }
        }

      }

      _export('default', Event);

      Object.defineProperty(_recorder_._src_client_reactive_active_expression_events_event_js, 'Event', {
        get() {
          return Event;
        },

        set(thisIsVererySecretVariableName) {
          _export('default', Event = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
    }
  };
});