'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-0b1e2673.js');
require('./_commonjsHelpers-937cd5b3.js');
const index$1 = require('./index-1a9e1965.js');

const styleCss = ".sc-smoothly-frame-h{display:block;width:100%;height:100%}[hidden].sc-smoothly-frame-h{display:none}.sc-smoothly-frame-h>iframe.sc-smoothly-frame{border:none;padding:none;margin:none}";

const SmoothlyFrame = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.trigger = index.createEvent(this, "trigger", 7);
    this.message = index.createEvent(this, "message", 7);
  }
  get contentWindow() {
    const iframe = this.element && this.element.firstElementChild ? this.element.firstElementChild : undefined;
    return (iframe && iframe.contentWindow) || undefined;
  }
  get defaultOrigin() {
    const match = this.url.match(/^(([a-z]+\+)*[a-z]+:\/\/)?[^/^\n]+/);
    return match ? match[0] : "*";
  }
  componentDidLoad() {
    var _a;
    if (this.contentWindow)
      index$1.Message.listen((_a = this.origin) !== null && _a !== void 0 ? _a : this.defaultOrigin, (destination, content) => {
        if (destination == this.name)
          if (index$1.Trigger.is(content))
            this.trigger.emit(content);
          else
            this.message.emit({ destination, content });
      }, window);
  }
  async send(message, content) {
    var _a, _b;
    if (typeof message == "string")
      index$1.Message.send(((_a = this.origin) !== null && _a !== void 0 ? _a : this.defaultOrigin) + "#" + message, content, this.contentWindow);
    else if (index$1.Message.is(message) && this.contentWindow)
      index$1.Message.send({ destination: ((_b = this.origin) !== null && _b !== void 0 ? _b : this.defaultOrigin) + "#" + message.destination, content: message.destination }, this.contentWindow);
  }
  render() {
    return index.h("iframe", { src: this.url + "#" + window.location.origin, height: "100%", width: "100%" });
  }
  get element() { return index.getElement(this); }
};
SmoothlyFrame.style = styleCss;

exports.smoothly_frame = SmoothlyFrame;
