'use strict';

System.register(['reactive-jsx', 'src/components/widgets/lively-morph.js', 'src/client/lively.js'], function (_export, _context) {
  "use strict";

  var _addSourceLocation, Morph, lively;

  return {
    setters: [function (_reactiveJsx) {
      _addSourceLocation = _reactiveJsx.addSourceLocation;
    }, function (_srcComponentsWidgetsLivelyMorphJs) {
      Morph = _srcComponentsWidgetsLivelyMorphJs.default;
    }, function (_srcClientLivelyJs) {
      lively = _srcClientLivelyJs.default;
    }],
    execute: function () {
      const __SystemJSRewritingHack = {};
      _recorder_._src_components_widgets_lively_notification_js = _recorder_._src_components_widgets_lively_notification_js || {};
      Object.defineProperty(_recorder_._src_components_widgets_lively_notification_js, 'Morph', {
        get() {
          return Morph;
        },

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

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

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

        enumerable: true,
        configurable: true
      });
      class Notification extends Morph {

        initialize() {
          this.render();
          lively.addEventListener("lively", this.shadowRoot.querySelector("#closeButton"), "click", evt => this.onClose(evt));
          lively.addEventListener("lively", this.shadowRoot.querySelector("#moreButton"), "click", evt => this.onMore(evt));
          this.counter = 0;
        }

        // #TODO get rid of this and replace it with lively.bindings
        get title() {
          return this._title;
        }

        set title(text) {
          this._title = text;
          this.render();
        }

        get color() {
          return this.shadowRoot.querySelector("#notification").style.backgroundColor;
        }

        set color(colorString) {
          this.shadowRoot.querySelector("#notification").style.backgroundColor = colorString;
        }

        get message() {
          return this._message;
        }

        set message(text) {
          this._message = text;
          this.render();
        }

        get more() {
          return this._more;
        }

        set more(cb) {
          this._more = cb; // callback
          this.render();
        }

        livelyMigrate(oldInstance) {
          // this is crucial state
          this.title = oldInstance.title;
          this.message = oldInstance.message;
          this.more = oldInstance.more;
        }

        onClose(evt) {
          var owner = this.parentElement;
          this.remove();
          if (owner && owner.hideIfEmpty) {
            owner.hideIfEmpty();
          }
        }

        onMore(evt) {
          if (this.more) this.more();
        }

        render() {
          this.shadowRoot.querySelector("#title").textContent = this.title;
          if (this.message instanceof Error) {
            var messageContainer = this.shadowRoot.querySelector("#message");
            messageContainer.innerHTML = "";
            let widget = _addSourceLocation(document.createElement("lively-error"), {
              file: 'https://lively-kernel.org/lively4/lively4-bp2019/src/components/widgets/lively-notification.js',
              end: {
                column: 57,
                line: 78
              },
              start: {
                column: 19,
                line: 78
              }
            });
            lively.components.openIn(messageContainer, widget).then(() => {
              widget.stack = this.message.stack;
            });
          } else {
            this.shadowRoot.querySelector("#message").textContent = this.message;
          }
          this.shadowRoot.querySelector("#moreButton").hidden = !this.more;

          if (this.counter > 1) {
            this.shadowRoot.querySelector("#counter").textContent = this.counter;
          }
        }

      }

      _export('default', Notification);

      Object.defineProperty(_recorder_._src_components_widgets_lively_notification_js, 'Notification', {
        get() {
          return Notification;
        },

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

          return true;
        },

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