import "grapesjs";
const C = "app.grapesjs.com", O = "app-stage.grapesjs.com", L = [
  C,
  O,
  "localhost",
  "127.0.0.1",
  ".local-credentialless.webcontainer.io",
  // For stackblitz.com demos
  ".local.webcontainer.io",
  // For stackblitz.com demos
  "-sandpack.codesandbox.io"
  // For Sandpack demos
], S = "license:check:start", j = "license:check:end", _ = () => typeof window < "u", $ = ({ isDev: n, isStage: s }) => `${n ? "" : `https://${s ? O : C}`}/api`, N = () => {
  const n = _() && window.location.hostname;
  return !!n && (L.includes(n) || L.some((s) => n.endsWith(s)));
};
var W = /* @__PURE__ */ ((n) => (n.free = "free", n.startup = "startup", n.business = "business", n.enterprise = "enterprise", n))(W || {});
async function D({
  path: n,
  baseApiUrl: s,
  method: i = "GET",
  headers: t = {},
  params: p,
  body: l
}) {
  const u = `${s || $({ isDev: !1, isStage: !1 })}${n}`, r = {
    method: i,
    headers: {
      "Content-Type": "application/json",
      ...t
    }
  };
  l && (r.body = JSON.stringify(l));
  const d = p ? new URLSearchParams(p).toString() : "", o = d ? `?${d}` : "", c = await fetch(`${u}${o}`, r);
  if (!c.ok)
    throw new Error(`HTTP error! status: ${c.status}`);
  return c.json();
}
const P = {
  [W.free]: 0,
  [W.startup]: 10,
  [W.business]: 20,
  [W.enterprise]: 30
};
function A(n) {
  const s = n;
  return s.init = (i) => (t) => n(t, i), s;
}
const q = (n) => /* @__PURE__ */ A(n);
async function U({
  editor: n,
  plan: s,
  pluginName: i,
  licenseKey: t,
  cleanup: p
}) {
  let l = "", g = !1;
  const u = N(), r = (o) => {
    console.warn("Cleanup plugin:", i, "Reason:", o), p();
  }, d = (o = {}) => {
    var e;
    const { error: c, sdkLicense: v } = o, E = (e = o.plan) == null ? void 0 : e.category;
    if (!(v || o.license) || c)
      r(c || "Invalid license");
    else if (E) {
      const f = P[s], b = P[E];
      f > b && r({ pluginRequiredPlan: s, licensePlan: E });
    }
  };
  n.on(S, (o) => {
    l = o == null ? void 0 : o.baseApiUrl, g = !0;
  }), n.on(j, (o) => {
    d(o);
  }), setTimeout(async () => {
    if (!g) {
      if (u) return;
      if (t) {
        const o = await X({ licenseKey: t, pluginName: i, baseApiUrl: l });
        o && d(o);
      } else
        r("The `licenseKey` option not provided");
    }
  }, 2e3);
}
async function X(n) {
  const { licenseKey: s, pluginName: i, baseApiUrl: t } = n;
  try {
    return (await D({
      baseApiUrl: t,
      path: `/sdk/${s || "na"}`,
      method: "POST",
      params: {
        d: window.location.hostname,
        pn: i
      }
    })).result || {};
  } catch (p) {
    return console.error("Error during SDK license check:", p), !1;
  }
}
const T = (...n) => n.map((s) => `[data-gjs-type="${s}"]`).join(","), I = "dialogComponent", B = function(n, s = {}) {
  const { Blocks: i, Components: t } = n, { licenseKey: p } = s, l = "gjs-plg-dialog", g = "gjs-plg-dialog-overlay", u = "gjs-plg-dialog-content", r = "gjs-plg-dialog-close", d = "gjs-plg-dialog-title", o = "gjs-plg-dialog-body", c = "gjs-plg-dialog-description", v = "gjs-plg-dialog-placeholder";
  i.add(l, {
    label: "Dialog",
    media: `
    <svg viewBox="0 0 24 24">
      <path d="M4 4h16v10H8l-4 4V4z"></path>
    </svg>
  `,
    category: "Extra",
    content: { type: l },
    activate: !0,
    ...s.block
  }), t.addType(l, {
    model: {
      defaults: {
        tagName: "div",
        attributes: { class: l, "data-type-role": l },
        components: [{ type: g }, { type: v }],
        draggable: T("wrapper"),
        droppable: T(l),
        styles: `
          .${l} {
            position: relative;
            z-index: 1;
          }
        `,
        traits: [
          {
            type: "button",
            label: "Open dialog",
            name: "onActive",
            changeProp: !0,
            command(a) {
              var e;
              (e = a.getSelected()) == null || e.trigger("active");
            }
          },
          {
            type: "checkbox",
            label: "Close when pressing X",
            name: "closeWhenPressingX",
            default: !0,
            changeProp: !0
          },
          {
            type: "checkbox",
            label: "Close when pressing ESC",
            name: "closeWhenPressingEsc",
            default: !0,
            changeProp: !0
          },
          {
            type: "checkbox",
            label: "Open when leaving the window",
            name: "openWhenLeavingWindow",
            default: !1,
            changeProp: !0
          },
          {
            type: "number",
            label: "Open when scrolling to level",
            name: "openWhenScrollingToLevel",
            default: 0,
            changeProp: !0
          }
        ],
        "script-props": [
          "closeWhenPressingX",
          "closeWhenPressingEsc",
          "openWhenLeavingWindow",
          "openWhenScrollingToLevel"
        ],
        script: function(a) {
          const e = this;
          function f(h) {
            const m = e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]'), y = e.querySelector('[data-type-role="gjs-plg-dialog-close"]');
            function w() {
              m.style.display = "none";
            }
            h ? (e._onCloseWhenPressingX = w, y.addEventListener("click", e._onCloseWhenPressingX)) : (y.removeEventListener("click", e._onCloseWhenPressingX), e._onCloseWhenPressingX = null);
          }
          function b(h) {
            const m = e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]');
            function y(w) {
              w.key === "Escape" && (m.style.display = "none");
            }
            h ? (e._onCloseWhenPressingEsc = y, e.addEventListener("keydown", e._onCloseWhenPressingEsc)) : (e.removeEventListener("keydown", e._onCloseWhenPressingEsc), e._onCloseWhenPressingEsc = null);
          }
          function x(h) {
            const m = e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]');
            function y() {
              m.style.display = "flex";
            }
            h ? (e._onOpenWhenLeavingWindow = y, document.addEventListener("mouseleave", e._onOpenWhenLeavingWindow)) : (document.removeEventListener("mouseleave", e._onOpenWhenLeavingWindow), e._onOpenWhenLeavingWindow = null);
          }
          function k(h) {
            const m = e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]');
            function y() {
              const w = document.documentElement.scrollTop;
              h && w >= Number(h) && (m.style.display = "flex");
            }
            h ? (e._onOpenWhenScrollingToLevel = y, document.addEventListener("scroll", e._onOpenWhenScrollingToLevel)) : (document.removeEventListener("scroll", e._onOpenWhenScrollingToLevel), e._onOpenWhenScrollingToLevel = null);
          }
          f(a.closeWhenPressingX), b(a.closeWhenPressingEsc), x(a.openWhenLeavingWindow), k(a.openWhenScrollingToLevel);
        }
      },
      init() {
        this.on("active", this.handleOpenWhenClickPlaceholder);
      },
      handleOpenWhenClickPlaceholder() {
        this.view.el.querySelector(
          '[data-type-role="gjs-plg-dialog-placeholder"]'
        ).addEventListener("click", () => {
          const e = this.view.el.querySelector(
            '[data-type-role="gjs-plg-dialog-overlay"]'
          );
          e.style.display = "flex";
        });
      }
    },
    view: {
      onActive() {
        const a = this.el.querySelector('[data-type-role="gjs-plg-dialog-overlay"]');
        a.style.display = "flex";
      }
    }
  }), t.addType(g, {
    model: {
      defaults: {
        tagName: "div",
        attributes: { class: g, "data-type-role": g },
        components: { type: u },
        droppable: !1,
        draggable: !1,
        styles: `
          .${g} {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: none;
            align-items: center;
            justify-content: center;
            background-color: rgba(0, 0, 0, 0.5);
          }
        `
      }
    }
  }), t.addType(u, {
    model: {
      defaults: {
        tagName: "div",
        attributes: { class: u, "data-type-role": u },
        components: [{ type: r }, { type: d }, { type: o }],
        droppable: !1,
        draggable: !1,
        styles: `
          .${u} {
            position: relative;
            background-color: #fff;
            border-radius: 10px;
            border: 1px solid #eee;
            padding: 20px;
            width: 800px;
            height: 600px;
            box-sizing: border-box;
          }
        `
      }
    }
  }), t.addType(r, {
    model: {
      defaults: {
        tagName: "button",
        attributes: { class: r, "data-type-role": r },
        components: "&times;",
        droppable: !1,
        draggable: !1,
        styles: `
          .${r} {
            position: absolute;
            top: 10px;
            right: 10px;
            width: 30px;
            height: 30px;
            background: none;
            border: none;
            font-size: 20px;
            cursor: pointer;
          }
        `
      }
    }
  }), t.addType(d, {
    model: {
      defaults: {
        tagName: "h2",
        attributes: { class: d, "data-type-role": d },
        components: "Dialog Title",
        editable: !0,
        droppable: !1,
        draggable: !1,
        styles: `
          .${d} {
            margin: 0;
            padding: 0 40px 20px 0;
          }
        `
      }
    }
  }), t.addType(o, {
    model: {
      defaults: {
        tagName: "div",
        attributes: { class: o, "data-type-role": o },
        components: [{ type: c }],
        styles: `
          .${o} {
            height: 90%;
            overflow: auto;
          }
        `
      }
    }
  }), t.addType(c, {
    model: {
      defaults: {
        tagName: "p",
        attributes: { class: c, "data-type-role": c },
        components: "This is a customizable dialog!",
        editable: !0,
        styles: `
          .${c} {
            margin: 0;
          }
        `
      }
    }
  }), t.addType(v, {
    model: {
      toHTML() {
        return "";
      },
      defaults: {
        tagName: "button",
        attributes: { class: v, "data-type-role": v },
        components: "Dialog (click me to open)",
        droppable: !1,
        draggable: !1
      }
    },
    view: {
      init() {
        const { em: a, el: e } = this, f = "core:preview", b = a.Commands.events;
        this.listenTo(a, `${b.runCommand}${f}`, () => {
          e.style.display = "none";
        }), this.listenTo(a, `${b.stopCommand}${f}`, () => {
          e.style.display = "";
        });
      }
    }
  }), U({
    editor: n,
    licenseKey: p,
    plan: W.startup,
    pluginName: I,
    cleanup: () => {
      i.remove(l), t.removeType(l);
    }
  });
}, H = q(B);
export {
  H as default
};
