(function(f,v){typeof exports=="object"&&typeof module<"u"?module.exports=v(require("grapesjs")):typeof define=="function"&&define.amd?define(["grapesjs"],v):(f=typeof globalThis<"u"?globalThis:f||self,f.StudioSdkPlugins_dialogComponent=v())})(this,function(){"use strict";const f="app.grapesjs.com",v="app-stage.grapesjs.com",T=[f,v,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],O="license:check:start",S="license:check:end",k=()=>typeof window<"u",j=({isDev:n,isStage:s})=>`${n?"":`https://${s?v:f}`}/api`,_=()=>{const n=k()&&window.location.hostname;return!!n&&(T.includes(n)||T.some(s=>n.endsWith(s)))};var b=(n=>(n.free="free",n.startup="startup",n.business="business",n.enterprise="enterprise",n))(b||{});async function $({path:n,baseApiUrl:s,method:i="GET",headers:t={},params:p,body:l}){const u=`${s||j({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 C={[b.free]:0,[b.startup]:10,[b.business]:20,[b.enterprise]:30};function N(n){const s=n;return s.init=i=>t=>n(t,i),s}const D=n=>N(n);async function q({editor:n,plan:s,pluginName:i,licenseKey:t,cleanup:p}){let l="",g=!1;const u=_(),r=o=>{console.warn("Cleanup plugin:",i,"Reason:",o),p()},d=(o={})=>{var e;const{error:c,sdkLicense:W}=o,P=(e=o.plan)==null?void 0:e.category;if(!(W||o.license)||c)r(c||"Invalid license");else if(P){const w=C[s],E=C[P];w>E&&r({pluginRequiredPlan:s,licensePlan:P})}};n.on(O,o=>{l=o==null?void 0:o.baseApiUrl,g=!0}),n.on(S,o=>{d(o)}),setTimeout(async()=>{if(!g){if(u)return;if(t){const o=await A({licenseKey:t,pluginName:i,baseApiUrl:l});o&&d(o)}else r("The `licenseKey` option not provided")}},2e3)}async function A(n){const{licenseKey:s,pluginName:i,baseApiUrl:t}=n;try{return(await $({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 x=(...n)=>n.map(s=>`[data-gjs-type="${s}"]`).join(","),U="dialogComponent";return D(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",W="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:W}],draggable:x("wrapper"),droppable:x(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 w(h){const m=e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]'),y=e.querySelector('[data-type-role="gjs-plg-dialog-close"]');function L(){m.style.display="none"}h?(e._onCloseWhenPressingX=L,y.addEventListener("click",e._onCloseWhenPressingX)):(y.removeEventListener("click",e._onCloseWhenPressingX),e._onCloseWhenPressingX=null)}function E(h){const m=e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]');function y(L){L.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 I(h){const m=e.querySelector('[data-type-role="gjs-plg-dialog-overlay"]');function y(){const L=document.documentElement.scrollTop;h&&L>=Number(h)&&(m.style.display="flex")}h?(e._onOpenWhenScrollingToLevel=y,document.addEventListener("scroll",e._onOpenWhenScrollingToLevel)):(document.removeEventListener("scroll",e._onOpenWhenScrollingToLevel),e._onOpenWhenScrollingToLevel=null)}w(a.closeWhenPressingX),E(a.closeWhenPressingEsc),X(a.openWhenLeavingWindow),I(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(W,{model:{toHTML(){return""},defaults:{tagName:"button",attributes:{class:W,"data-type-role":W},components:"Dialog (click me to open)",droppable:!1,draggable:!1}},view:{init(){const{em:a,el:e}=this,w="core:preview",E=a.Commands.events;this.listenTo(a,`${E.runCommand}${w}`,()=>{e.style.display="none"}),this.listenTo(a,`${E.stopCommand}${w}`,()=>{e.style.display=""})}}}),q({editor:n,licenseKey:p,plan:b.startup,pluginName:U,cleanup:()=>{i.remove(l),t.removeType(l)}})})});
