import { useMessage as y, useCanvas as M, useBlock as L, usePage as U, useMaterial as I, useResource as T, getMergeMeta as h, getMetaApi as B, META_SERVICE as D } from "@opentiny/tiny-engine-meta-register";
import { utils as E } from "@opentiny/tiny-engine-utils";
import { isDevelopEnv as R } from "./environments.js";
import { u as b } from "../index-CeNzXk8O.js";
const { deepClone: f } = E;
let a = null;
const k = () => {
  const { scripts: e, styles: o } = I().getCanvasDeps(), t = T().getUtilsDeps(), i = [...e, ...t].reduce((n, s) => (n[s.package] = n[s.package] || s.script, n), {}), c = [...o];
  return {
    scripts: i,
    styles: c
  };
}, P = async () => {
  const { isBlock: e, getPageSchema: o, getCurrentPage: t, getSchema: i } = M(), c = e(), { scripts: n, styles: s } = k();
  if (c) {
    const { getCurrentBlock: m } = L(), C = {
      ...m(),
      page_content: i()
    };
    return f({
      currentPage: C,
      ancestors: [],
      scripts: n,
      styles: s
    });
  }
  const p = o(), g = t(), { getFamily: r } = U(), l = {
    ...g,
    page_content: p
  }, u = await r(l);
  return f({
    currentPage: l,
    ancestors: u,
    scripts: n,
    styles: s
  });
}, S = (e) => {
  a.postMessage(
    {
      source: "designer",
      type: "schema",
      data: e
    },
    a.origin || window.location.origin
  );
};
let v = !1;
const _ = () => {
  const { unsubscribe: e } = y();
  e({
    topic: "schemaChange",
    subscriber: "preview-communication"
  }), e({
    topic: "schemaImport",
    subscriber: "preview-communication"
  }), e({
    topic: "pageOrBlockInit",
    subscriber: "preview-communication"
  }), v = !1;
}, d = async () => {
  if (!a || a.closed) {
    _(), a = null;
    return;
  }
  const e = await P();
  S(e);
}, $ = () => {
  if (v)
    return;
  const { subscribe: e } = y();
  e({
    topic: "schemaChange",
    subscriber: "preview-communication",
    // 防抖更新，防止因为属性变化频繁触发
    callback: b(d, 1e3, !0)
  }), e({
    topic: "schemaImport",
    subscriber: "preview-communication",
    callback: b(d, 1e3, !0)
  }), e({
    topic: "pageOrBlockInit",
    subscriber: "preview-communication",
    callback: d
  }), v = !0;
}, A = () => {
  window.addEventListener("message", async (o) => {
    const t = new URL(o.origin), i = new URL(window.location.href);
    if (t.origin === i.origin || t.host === i.host) {
      const { event: c, source: n } = o.data || {};
      if (n === "preview" && c === "connect" && !a && (a = o.source, $()), n === "preview" && c === "onMounted" && a) {
        const s = await P();
        S(s);
      }
    }
  });
  const e = new BroadcastChannel("tiny-engine-preview-channel");
  e.postMessage({
    event: "connect",
    source: "designer"
  }), e.close();
};
A();
const O = (e, o) => {
  let t = null;
  const i = (c) => {
    if (c.origin === window.location.origin || c.origin.includes(window.location.hostname)) {
      const { event: n, source: s } = c.data || {};
      if (s === "preview" && n === "onMounted" && t) {
        const { scripts: p, styles: g, ancestors: r = [], ...l } = e;
        t.postMessage(
          {
            source: "designer",
            type: "schema",
            data: f({
              currentPage: l,
              ancestors: r,
              scripts: p,
              styles: g
            })
          },
          (t == null ? void 0 : t.origin) || window.location.origin
        ), window.removeEventListener("message", i);
      }
    }
  };
  window.addEventListener("message", i), t = window.open(o, "_blank");
}, q = (e = {}, o = !1) => {
  var l, u, m, w;
  const t = new URLSearchParams(location.search), i = t.get("tenant") || "", c = t.get("pageid"), n = t.get("blockid"), s = (u = (l = B(D.ThemeSwitch)) == null ? void 0 : l.getThemeState()) == null ? void 0 : u.theme, p = (m = h("engine.config")) == null ? void 0 : m.dslMode, g = (w = h("engine.config")) == null ? void 0 : w.platformId;
  let r = `tenant=${i}&id=${t.get("id")}&theme=${s}&framework=${p}`;
  return r += `&platform=${g}`, c && (r += `&pageid=${c}`), n && (r += `&blockid=${n}`), o && (r += `&history=${e.history}`), r += `&previewType=${e.previewType}`, r;
}, F = (e = {}, o = !1) => {
  var r, l;
  const t = window.location.href.split("?")[0] || "./", { scripts: i, styles: c } = k(), n = q(e, o);
  let s = "";
  const p = (l = (r = h("engine.toolbars.preview")) == null ? void 0 : r.options) == null ? void 0 : l.previewUrl, g = R ? "./preview.html" : `${t.endsWith("/") ? t : `${t}/`}preview`;
  if (p ? s = typeof p == "function" ? p(g, n) : `${p}?${n}` : s = `${g}?${n}`, o) {
    O({ ...e, scripts: i, styles: c }, s);
    return;
  }
  if (a && !a.closed) {
    a.focus();
    return;
  }
  a = window.open(s, "_blank"), $();
}, W = (e = {}, o = !1) => {
  F(e, o);
};
export {
  W as previewPage,
  $ as setupSchemaChangeListener
};
//# sourceMappingURL=preview.js.map
