"use strict";require("grapesjs");function x(e,s,i){let t,o,a,p,c;const l=s;function u(){const r=Date.now()-p;r<l&&r>=0?t=setTimeout(u,l-r):(t=null,c=e.apply(a,o),a=o=null)}const n=function(){return a=this,o=arguments,p=Date.now(),t||(t=setTimeout(u,l)),c};return n.clear=function(){t&&(clearTimeout(t),t=null)},n.flush=function(){t&&(c=e.apply(a,o),a=o=null,clearTimeout(t),t=null)},n}const E="page://",m="list-pages",d="list-pages-item",g="list-pages-link",R=e=>e.components().reduce((s,i)=>{var p;const t=i.findFirstType(g),o=(p=t==null?void 0:t.attributes.attributes)==null?void 0:p.href,a=o!=null&&o.startsWith(E)?o.replace(E,""):"";if(a&&t){const c=s[a];c?c.push(t):s[a]=[t]}return s},{}),$="app.grapesjs.com",_="app-stage.grapesjs.com",k=[$,_,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],S="license:check:start",D="license:check:end",U=()=>typeof window<"u",W=({isDev:e,isStage:s})=>`${e?"":`https://${s?_:$}`}/api`,O=()=>{const e=U()&&window.location.hostname;return!!e&&(k.includes(e)||k.some(s=>e.endsWith(s)))};var h=(e=>(e.free="free",e.startup="startup",e.business="business",e.enterprise="enterprise",e))(h||{});async function B({path:e,baseApiUrl:s,method:i="GET",headers:t={},params:o,body:a}){const c=`${s||W({isDev:!1,isStage:!1})}${e}`,l={method:i,headers:{"Content-Type":"application/json",...t}};a&&(l.body=JSON.stringify(a));const u=o?new URLSearchParams(o).toString():"",n=u?`?${u}`:"",r=await fetch(`${c}${n}`,l);if(!r.ok)throw new Error(`HTTP error! status: ${r.status}`);return r.json()}const v={[h.free]:0,[h.startup]:10,[h.business]:20,[h.enterprise]:30};function K(e){const s=e;return s.init=i=>t=>e(t,i),s}const j=e=>K(e);async function F({editor:e,plan:s,pluginName:i,licenseKey:t,cleanup:o}){let a="",p=!1;const c=O(),l=n=>{console.warn("Cleanup plugin:",i,"Reason:",n),o()},u=(n={})=>{var P;const{error:r,sdkLicense:I}=n,f=(P=n.plan)==null?void 0:P.category;if(!(I||n.license)||r)l(r||"Invalid license");else if(f){const H=v[s],b=v[f];H>b&&l({pluginRequiredPlan:s,licensePlan:f})}};e.on(S,n=>{a=n==null?void 0:n.baseApiUrl,p=!0}),e.on(D,n=>{u(n)}),setTimeout(async()=>{if(!p){if(c)return;if(t){const n=await G({licenseKey:t,pluginName:i,baseApiUrl:a});n&&u(n)}else l("The `licenseKey` option not provided")}},2e3)}async function G(e){const{licenseKey:s,pluginName:i,baseApiUrl:t}=e;try{return(await B({baseApiUrl:t,path:`/sdk/${s||"na"}`,method:"POST",params:{d:window.location.hostname,pn:i}})).result||{}}catch(o){return console.error("Error during SDK license check:",o),!1}}const M="gjs-plg-",q="listPagesComponent",T="data-type-role",X="page_root",Y=e=>s=>s.is(e),Z=e=>(s,i)=>i.is(e),w=e=>s=>{var i;return((i=s.getAttribute)==null?void 0:i.call(s,T))===e},J=function(e,s={}){const{Blocks:i,Components:t,Pages:o}=e,{licenseKey:a,block:p={}}=s,c="List Pages",l="List Pages Item",u="Link",n=`${M}${m}`,r=`${M}${g}`,I='<svg viewBox="0 0 24 24"><path d="M11 15H17V17H11V15M9 7H7V9H9V7M11 13H17V11H11V13M11 9H17V7H11V9M9 11H7V13H9V11M21 5V19C21 20.1 20.1 21 19 21H5C3.9 21 3 20.1 3 19V5C3 3.9 3.9 3 5 3H19C20.1 3 21 3.9 21 5M19 5H5V19H19V5M9 15H7V17H9V15Z"/></svg>',f='<svg viewBox="0 0 24 24"><path d="M7,5H21V7H7V5M7,13V11H21V13H7M4,4.5A1.5,1.5 0 0,1 5.5,6A1.5,1.5 0 0,1 4,7.5A1.5,1.5 0 0,1 2.5,6A1.5,1.5 0 0,1 4,4.5M4,10.5A1.5,1.5 0 0,1 5.5,12A1.5,1.5 0 0,1 4,13.5A1.5,1.5 0 0,1 2.5,12A1.5,1.5 0 0,1 4,10.5M7,19V17H21V19H7M4,16.5A1.5,1.5 0 0,1 5.5,18A1.5,1.5 0 0,1 4,19.5A1.5,1.5 0 0,1 2.5,18A1.5,1.5 0 0,1 4,16.5Z" /></svg>';t.addType(m,{block:p&&{label:c,media:I,category:"Extra",select:!0,...s.block},isComponent:w(m),model:{defaults:{tagName:"ul",name:c,[X]:"",classes:n,attributes:{[T]:m},droppable:Y(d),styles:`
          .${n} {
            list-style: none;
            margin: 0;
            padding: 0;
          }
        `},init(){this.syncPageItems=x(this.syncPageItems.bind(this),0),this.listenTo(this.em,o.events.all,this.syncPageItems),this.syncPageItems()},syncPageItems(){if(e.Components.getSymbolInfo(this).isInstance)return;const y=this.em.Pages.getAll(),P=this.components(),H=R(this);y.forEach((b,N)=>{const V=b.id,A=b.getName(),L=H[V];L?L==null||L.forEach(C=>{C.getInnerHTML()!==A&&C.components(A)}):P.add({type:d,components:[{type:g,attributes:{href:`page://${V}`},components:{type:"textnode",content:A}}]},{at:N})})}}}),t.addType(d,{isComponent:w(d),model:{defaults:{name:l,tagName:"li",icon:f,attributes:{[T]:d},components:{type:g},draggable:Z(m)}}}),t.addType(g,{extend:"link",isComponent:w(g),model:{defaults:{removable:!1,copyable:!1,delegate:{move:y=>y.closestType(d)},name:u,classes:r,attributes:{[T]:g},components:{type:"textnode",content:"Item"},styles:`
          .${r} {
            text-decoration: none;
          }
        `}}}),F({editor:e,licenseKey:a,plan:h.startup,pluginName:q,cleanup:()=>{i.remove(m),t.removeType(m)}})},z=j(J);module.exports=z;
