import "grapesjs";
const P = "app.grapesjs.com", B = "app-stage.grapesjs.com", N = [
  P,
  B,
  "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
], D = "license:check:start", U = "license:check:end", R = () => typeof window < "u", W = ({ isDev: t, isStage: a }) => `${t ? "" : `https://${a ? B : P}`}/api`, F = () => {
  const t = R() && window.location.hostname;
  return !!t && (N.includes(t) || N.some((a) => t.endsWith(a)));
};
var x = /* @__PURE__ */ ((t) => (t.free = "free", t.startup = "startup", t.business = "business", t.enterprise = "enterprise", t))(x || {});
async function K({
  path: t,
  baseApiUrl: a,
  method: r = "GET",
  headers: c = {},
  params: p,
  body: h
}) {
  const w = `${a || W({ isDev: !1, isStage: !1 })}${t}`, d = {
    method: r,
    headers: {
      "Content-Type": "application/json",
      ...c
    }
  };
  h && (d.body = JSON.stringify(h));
  const m = p ? new URLSearchParams(p).toString() : "", n = m ? `?${m}` : "", b = await fetch(`${w}${n}`, d);
  if (!b.ok)
    throw new Error(`HTTP error! status: ${b.status}`);
  return b.json();
}
const O = {
  [x.free]: 0,
  [x.startup]: 10,
  [x.business]: 20,
  [x.enterprise]: 30
};
function G(t) {
  const a = t;
  return a.init = (r) => (c) => t(c, r), a;
}
const Z = (t) => /* @__PURE__ */ G(t);
async function q({
  editor: t,
  plan: a,
  pluginName: r,
  licenseKey: c,
  cleanup: p
}) {
  let h = "", g = !1;
  const w = F(), d = (n) => {
    console.warn("Cleanup plugin:", r, "Reason:", n), p();
  }, m = (n = {}) => {
    var A;
    const { error: b, sdkLicense: l } = n, v = (A = n.plan) == null ? void 0 : A.category;
    if (!(l || n.license) || b)
      d(b || "Invalid license");
    else if (v) {
      const E = O[a], $ = O[v];
      E > $ && d({ pluginRequiredPlan: a, licensePlan: v });
    }
  };
  t.on(D, (n) => {
    h = n == null ? void 0 : n.baseApiUrl, g = !0;
  }), t.on(U, (n) => {
    m(n);
  }), setTimeout(async () => {
    if (!g) {
      if (w) return;
      if (c) {
        const n = await z({ licenseKey: c, pluginName: r, baseApiUrl: h });
        n && m(n);
      } else
        d("The `licenseKey` option not provided");
    }
  }, 2e3);
}
async function z(t) {
  const { licenseKey: a, pluginName: r, baseApiUrl: c } = t;
  try {
    return (await K({
      baseApiUrl: c,
      path: `/sdk/${a || "na"}`,
      method: "POST",
      params: {
        d: window.location.hostname,
        pn: r
      }
    })).result || {};
  } catch (p) {
    return console.error("Error during SDK license check:", p), !1;
  }
}
const J = "gjs-plg-", X = "fsLightboxComponent", k = "data-fslightbox", C = "data-type", Y = "fslightbox-open", T = "fslightbox", S = {
  image: '<svg viewBox="0 0 24 24"><path d="m8.5 13.5 2.5 3 3.5-4.5 4.5 6H5m16 1V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2Z"/></svg>',
  video: '<svg viewBox="0 0 24 24"><path d="M17 10.5V7a1 1 0 0 0-1-1H4a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-3.5l4 4v-11l-4 4Z"/></svg>',
  imageLightbox: '<svg viewBox="0 0 24 24"><path d="M4,2H20A2,2 0 0,1 22,4V16A2,2 0 0,1 20,18H16L12,22L8,18H4A2,2 0 0,1 2,16V4A2,2 0 0,1 4,2M19,15V7L15,11L13,9L7,15H19M7,5A2,2 0 0,0 5,7A2,2 0 0,0 7,9A2,2 0 0,0 9,7A2,2 0 0,0 7,5Z" /></svg>',
  external: '<svg viewBox="0 0 24 24"><path d="M14 3v2h3.6l-9.8 9.8 1.4 1.4L19 6.4V10h2V3m-2 16H5V5h7V3H5a2 2 0 0 0-2 2v14c0 1.1.9 2 2 2h14a2 2 0 0 0 2-2v-7h-2v7z"/></svg>',
  eye: '<svg viewBox="0 0 24 24"><path d="M12 9a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0 8a5 5 0 0 1-5-5 5 5 0 0 1 5-5 5 5 0 0 1 5 5 5 5 0 0 1-5 5m0-12.5C7 4.5 2.7 7.6 1 12a11.8 11.8 0 0 0 22 0c-1.7-4.4-6-7.5-11-7.5Z"/></svg>'
}, Q = function(t, a = {}) {
  const { Blocks: r, Components: c } = t, p = "Lightbox", h = `${J}${T}`, g = {
    toolbarIconOpen: S.eye,
    block: {},
    defaultSrc: "https://placehold.co/300/777/white.png?text=Image",
    cdnScript: "https://cdn.jsdelivr.net/npm/fslightbox@3.4.2/index.js",
    ...a
  }, { block: w, toolbarIconOpen: d, defaultSrc: m } = g, n = function(s) {
    const e = this, i = "data-fslightbox", o = window, f = () => {
      var H;
      const u = o.refreshFsLightbox;
      u == null || u();
      const j = e.getAttribute(i);
      if ((H = o.fsLightboxInstances) == null || H[j], !o._isEditor) return;
      Array.from(document.querySelectorAll(`[${i}]`)).forEach((y) => {
        const L = y.onclick;
        y.onclick = function(I) {
          I.preventDefault();
          const M = y;
          !M.__skipLb && (L == null || L(I)), M.__skipLb = !0;
        };
      });
    };
    if (o.refreshFsLightbox)
      f();
    else {
      const u = document.createElement("script");
      u.src = s.cdnScript, u.onload = f, document.head.appendChild(u);
    }
  }, b = (s) => ({
    id: `${T}-open-lb`,
    label: d,
    command: () => {
      const e = s.getView();
      e == null || e.toggleLightbox();
    }
  }), l = "source-type", v = {
    type: "radio",
    name: l,
    label: "Source type",
    changeProp: !0,
    options: [
      { id: "image", label: "Image", icon: S.image },
      { id: "video", label: "Video", icon: S.video },
      { id: "el", label: "Custom Element", icon: S.external }
    ],
    // @ts-expect-error Studio related prop
    labelToTitle: !0
  }, V = {
    id: "href-image",
    name: "href",
    type: "file",
    label: "Source Image"
  }, A = {
    id: "href-video",
    name: "href",
    label: "Source Video",
    placeholder: "https://www.youtube.com/watch?v=..."
  }, E = {
    id: "href-el",
    name: "href",
    label: "Source Element",
    placeholder: "#custom-element-id"
  }, $ = {
    image: [V],
    video: [A],
    el: [E]
  };
  c.addType(T, {
    block: w && {
      label: p,
      media: S.imageLightbox,
      category: "Extra",
      select: !0,
      ...g.block
    },
    isComponent: (s) => {
      var e;
      return (e = s.hasAttribute) == null ? void 0 : e.call(s, k);
    },
    extendFn: ["initToolbar"],
    model: {
      defaults: {
        tagName: "a",
        name: p,
        icon: S.imageLightbox,
        classes: h,
        draggable: (s, e) => !e.is(T),
        attributes: {
          [k]: "lightbox",
          [C]: "image",
          href: m
        },
        components: { type: "image", src: m },
        script: n,
        cdnScript: g.cdnScript,
        "script-props": ["attributes", "cdnScript"],
        [l]: "image",
        traits: [
          v,
          {
            name: k,
            label: "Group name"
          },
          {
            type: "button",
            label: "Toggle Lightbox",
            command(s, e) {
              var o;
              const i = (o = e.target) == null ? void 0 : o.getView();
              i == null || i.toggleLightbox();
            }
          }
        ],
        styles: `
          .${h} {
            display: inline-block;
            padding: 10px;
          }
        `
      },
      init() {
        const s = [l].map((e) => `change:${e}`).join(" ");
        this.on(s, this.updateTraits), this.on(`change:${l}`, this.onSourceTypeChange), this.on("change:attributes:href", this.adjustSourceType), this.updateTraits({ init: !0 }), this.adjustSourceType();
      },
      onSourceTypeChange(s, e, i) {
        if (i.adjust) return;
        const f = this.get(l) === "video" ? "video" : "image";
        this.addAttributes({ [C]: f, href: "" });
      },
      adjustSourceType() {
        const s = this.get(l), e = this.getAttributes().href || "", i = { adjust: !0 };
        if (s === "video") {
          const o = e.includes("youtube.com") ? "youtube" : "video";
          this.addAttributes({ [C]: o }, i);
        } else s !== "el" && e.startsWith("#") ? this.set(l, "el", i) : s === "image" && this.addAttributes({ [C]: "image" }, i);
      },
      updateTraits(s = {}) {
        const { changed: e } = this, { init: i } = s;
        if (e[v.name] || i) {
          this.removeTrait([V.name, A.name, E.name]);
          const o = this.get(l) || "image", f = this.getTraitIndex(l) + 1;
          this.addTrait($[o], { at: f });
        }
      },
      initToolbar() {
        if (!d) return;
        const { toolbar: s } = this, e = b(this);
        !s.find((o) => o.id === e.id) && (s == null || s.unshift(e));
      }
    },
    view: {
      onRender() {
        this.el.__skipLb = !0;
      },
      toggleLightbox() {
        var y;
        const { em: s, el: e, model: i } = this, { Canvas: o } = s, f = i.getAttributes()[k], { defaultView: u, documentElement: j } = e.ownerDocument, _ = (y = u.fsLightboxInstances) == null ? void 0 : y[f];
        if (j.classList.contains(Y))
          _.close();
        else {
          e.__skipLb = !1;
          let L;
          _.props.onOpen = () => {
            L = o.getSpots(), setTimeout(() => o.removeSpots());
          }, _.props.onClose = () => {
            L.forEach((I) => o.addSpot(I.attributes));
          }, e.click();
        }
      }
    }
  }), q({
    editor: t,
    plan: x.startup,
    licenseKey: g.licenseKey,
    pluginName: X,
    cleanup: () => {
      r.remove(T), c.removeType(T);
    }
  });
}, te = Z(Q);
export {
  te as default
};
