System.register(['utils'], function (_export, _context) {
  "use strict";

  var PausableLoop;
  return {
    setters: [function (_utils) {
      PausableLoop = _utils.PausableLoop;
    }],
    execute: function () {
      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js = _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js || {};
      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.__unload__ = __unload__;
      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.toDOMNode = toDOMNode;
      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.removeObsoleteListeners = removeObsoleteListeners;
      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.PausableLoop = PausableLoop;


      const REMOVE_LISTENER_BY_CHECK_FUNCTION = new Map(); // nodeDetachedFromDOM -> removeListener

      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.REMOVE_LISTENER_BY_CHECK_FUNCTION = REMOVE_LISTENER_BY_CHECK_FUNCTION;
      function removeObsoleteListeners() {
        Array.from(_recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.REMOVE_LISTENER_BY_CHECK_FUNCTION).forEach(([nodeDetachedFromDOM, removeListener]) => {
          if (nodeDetachedFromDOM()) {
            removeListener();
            _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.REMOVE_LISTENER_BY_CHECK_FUNCTION.delete(nodeDetachedFromDOM);
          }
        });
        if (_recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.REMOVE_LISTENER_BY_CHECK_FUNCTION.size === 0) {
          _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.removeLoop.pause();
        }
      }

      // `this` is an ActiveExpression 
      function toDOMNode(builder = x => x) {
        const { value } = this.evaluateToCurrentValue();
        let currentNode = builder(value.value);

        function updateDOMNode(val) {
          // lively.notify("change aexpr result", val)
          const newNode = builder(val);
          currentNode.replaceWith(newNode);
          currentNode = newNode;
        };

        function nodeDetachedFromDOM() {
          return currentNode.getRootNode({ composed: true }) !== document;
        }

        const removeListener = () => this.offChange(updateDOMNode);

        this.onChange(updateDOMNode);

        _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.REMOVE_LISTENER_BY_CHECK_FUNCTION.set(nodeDetachedFromDOM, removeListener);
        _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.removeLoop.ensureRunning();

        return currentNode;
      }

      _export('toDOMNode', toDOMNode);

      const removeLoop = new _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.PausableLoop(_recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.removeObsoleteListeners);

      _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.removeLoop = removeLoop;
      function __unload__() {
        _recorder_._src_client_reactive_reactive_jsx_ui_aexpr_js.removeLoop.pause();
        //cancelAnimationFrame(checkRequest);
        // lively.notify("unload module", "UI AEXPR", undefined, null, "red");
      }

      _export('__unload__', __unload__);
    }
  };
});