var w = Object.defineProperty;
var E = (n, a, e) => a in n ? w(n, a, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[a] = e;
var f = (n, a, e) => E(n, typeof a != "symbol" ? a + "" : a, e);
/**
  postmate - A powerful, simple, promise-based postMessage library
  @version v1.5.2
  @link https://github.com/dollarshaveclub/postmate
  @author Jacob Kelley <jakie8@gmail.com>
  @license MIT
**/
var h = "application/x-postmate-v1+json", k = 5, L = 0, O = function() {
  return ++L;
}, P = function(a) {
  var e = document.createElement("a");
  e.href = a;
  var t = e.protocol.length > 4 ? e.protocol : window.location.protocol, i = e.host.length ? e.port === "80" || e.port === "443" ? e.hostname : e.host : window.location.host;
  return e.origin || t + "//" + i;
}, I = {
  handshake: 1,
  "handshake-reply": 1,
  call: 1,
  emit: 1,
  reply: 1,
  request: 1
  /**
   * Ensures that a message is safe to interpret
   * @param  {Object} message The postmate message being sent
   * @param  {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check
   * @return {Boolean}
   */
}, v = function(a, e) {
  return !(typeof e == "string" && a.origin !== e || !a.data || typeof a.data == "object" && !("postmate" in a.data) || a.data.type !== h || !I[a.data.postmate]);
}, M = function(a, e) {
  var t = typeof a[e] == "function" ? a[e]() : a[e];
  return c.Promise.resolve(t);
}, _ = /* @__PURE__ */ function() {
  function n(e) {
    var t = this;
    this.parent = e.parent, this.frame = e.frame, this.child = e.child, this.childOrigin = e.childOrigin, this.events = {}, this.listener = function(i) {
      if (!v(i, t.childOrigin)) return !1;
      var s = ((i || {}).data || {}).value || {}, r = s.data, l = s.name;
      i.data.postmate === "emit" && l in t.events && t.events[l].call(t, r);
    }, this.parent.addEventListener("message", this.listener, !1);
  }
  var a = n.prototype;
  return a.get = function(t) {
    var i = this;
    return new c.Promise(function(s) {
      var r = O(), l = function o(d) {
        d.data.uid === r && d.data.postmate === "reply" && (i.parent.removeEventListener("message", o, !1), s(d.data.value));
      };
      i.parent.addEventListener("message", l, !1), i.child.postMessage({
        postmate: "request",
        type: h,
        property: t,
        uid: r
      }, i.childOrigin);
    });
  }, a.call = function(t, i) {
    this.child.postMessage({
      postmate: "call",
      type: h,
      property: t,
      data: i
    }, this.childOrigin);
  }, a.on = function(t, i) {
    this.events[t] = i;
  }, a.destroy = function() {
    window.removeEventListener("message", this.listener, !1), this.frame.parentNode.removeChild(this.frame);
  }, n;
}(), A = /* @__PURE__ */ function() {
  function n(e) {
    var t = this;
    this.model = e.model, this.parent = e.parent, this.parentOrigin = e.parentOrigin, this.child = e.child, this.child.addEventListener("message", function(i) {
      if (v(i, t.parentOrigin)) {
        var s = i.data, r = s.property, l = s.uid, o = s.data;
        if (i.data.postmate === "call") {
          r in t.model && typeof t.model[r] == "function" && t.model[r](o);
          return;
        }
        M(t.model, r).then(function(d) {
          return i.source.postMessage({
            property: r,
            postmate: "reply",
            type: h,
            uid: l,
            value: d
          }, i.origin);
        });
      }
    });
  }
  var a = n.prototype;
  return a.emit = function(t, i) {
    this.parent.postMessage({
      postmate: "emit",
      type: h,
      value: {
        name: t,
        data: i
      }
    }, this.parentOrigin);
  }, n;
}(), c = /* @__PURE__ */ function() {
  function n(e) {
    var t = e.container, i = t === void 0 ? typeof i < "u" ? i : document.body : t, s = e.model, r = e.url, l = e.name, o = e.classListArray, d = o === void 0 ? [] : o;
    return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = l || "", this.frame.classList.add.apply(this.frame.classList, d), i.appendChild(this.frame), this.child = this.frame.contentWindow || this.frame.contentDocument.parentWindow, this.model = s || {}, this.sendHandshake(r);
  }
  var a = n.prototype;
  return a.sendHandshake = function(t) {
    var i = this, s = P(t), r = 0, l;
    return new n.Promise(function(o, d) {
      var p = function u(m) {
        return v(m, s) ? m.data.postmate === "handshake-reply" ? (clearInterval(l), i.parent.removeEventListener("message", u, !1), i.childOrigin = m.origin, o(new _(i))) : d("Failed handshake") : !1;
      };
      i.parent.addEventListener("message", p, !1);
      var g = function() {
        r++, i.child.postMessage({
          postmate: "handshake",
          type: h,
          model: i.model
        }, s), r === k && clearInterval(l);
      }, y = function() {
        g(), l = setInterval(g, 500);
      };
      i.frame.attachEvent ? i.frame.attachEvent("onload", y) : i.frame.onload = y, i.frame.src = t;
    });
  }, n;
}();
c.debug = !1;
c.Promise = function() {
  try {
    return window ? window.Promise : Promise;
  } catch {
    return null;
  }
}();
c.Model = /* @__PURE__ */ function() {
  function n(e) {
    return this.child = window, this.model = e, this.parent = this.child.parent, this.sendHandshakeReply();
  }
  var a = n.prototype;
  return a.sendHandshakeReply = function() {
    var t = this;
    return new c.Promise(function(i, s) {
      var r = function l(o) {
        if (o.data.postmate) {
          if (o.data.postmate === "handshake") {
            t.child.removeEventListener("message", l, !1), o.source.postMessage({
              postmate: "handshake-reply",
              type: h
            }, o.origin), t.parentOrigin = o.origin;
            var d = o.data.model;
            return d && Object.keys(d).forEach(function(p) {
              t.model[p] = d[p];
            }), i(new A(t));
          }
          return s("Handshake Reply Failed");
        }
      };
      t.child.addEventListener("message", r, !1);
    });
  }, n;
}();
function H(n) {
  return { all: n = n || /* @__PURE__ */ new Map(), on: function(a, e) {
    var t = n.get(a);
    t ? t.push(e) : n.set(a, [e]);
  }, off: function(a, e) {
    var t = n.get(a);
    t && (e ? t.splice(t.indexOf(e) >>> 0, 1) : n.set(a, []));
  }, emit: function(a, e) {
    var t = n.get(a);
    t && t.slice().map(function(i) {
      i(e);
    }), (t = n.get("*")) && t.slice().map(function(i) {
      i(a, e);
    });
  } };
}
class b {
  constructor(a, e, t) {
    f(this, "container");
    f(this, "options");
    f(this, "child", null);
    f(this, "iframeSrc");
    f(this, "emitter", H());
    this.container = a, this.iframeSrc = e, this.options = Object.assign({
      defaultLateX: null,
      class: []
    }, t || {});
    let i = document.getElementById("match-editor");
    i && (i.remove(), i = null);
  }
  async open() {
    if (this.child)
      return;
    if (!this.container)
      throw Error("缺少编辑器的容器");
    const e = await new c({
      container: this.container,
      // Element to inject frame into
      url: this.iframeSrc,
      // Page to load, must have postmate.js. This will also be the origin used for communication.
      name: "math-editor-iframe",
      // Set Iframe name attribute. Useful to get `window.name` in the child.
      classListArray: ["editor-iframe"].concat(this.options.class ? this.options.class : []),
      //Classes to add to the iframe via classList, useful for styling.
      model: { defaultLateX: this.options.defaultLateX }
    });
    e.on("ready", (t) => {
      e.call("set", this.options.defaultLateX), this.child = e, this.emitter.emit("ready", t);
    }), e.on("update", (t) => {
      this.emitter.emit("update", t);
    });
  }
  close() {
    this.child && this.child.destroy(), this.child = null;
  }
  async get() {
    if (this.child)
      return await this.child.get("laTeX");
    throw new Error("获取不到编辑器");
  }
  set(a) {
    if (this.child)
      this.child.call("set", a);
    else
      throw new Error("获取不到编辑器");
  }
  on(a, e) {
    this.emitter.on(a, e);
  }
  off(a, e) {
    this.emitter.off(a, e);
  }
}
export {
  b as MathEditor
};
