(function(y,g){typeof exports=="object"&&typeof module<"u"?module.exports=g(require("grapesjs")):typeof define=="function"&&define.amd?define(["grapesjs"],g):(y=typeof globalThis<"u"?globalThis:y||self,y.StudioSdkPlugins_listPagesComponent=g())})(this,function(){"use strict";function y(e,s,i){let t,o,a,p,c;const r=s;function u(){const l=Date.now()-p;l<r&&l>=0?t=setTimeout(u,r-l):(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,r)),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 g="page://",m="list-pages",f="list-pages-item",d="list-pages-link",N=e=>e.components().reduce((s,i)=>{var p;const t=i.findFirstType(d),o=(p=t==null?void 0:t.attributes.attributes)==null?void 0:p.href,a=o!=null&&o.startsWith(g)?o.replace(g,""):"";if(a&&t){const c=s[a];c?c.push(t):s[a]=[t]}return s},{}),E="app.grapesjs.com",k="app-stage.grapesjs.com",v=[E,k,"localhost","127.0.0.1",".local-credentialless.webcontainer.io",".local.webcontainer.io","-sandpack.codesandbox.io"],S="license:check:start",R="license:check:end",D=()=>typeof window<"u",U=({isDev:e,isStage:s})=>`${e?"":`https://${s?k:E}`}/api`,W=()=>{const e=D()&&window.location.hostname;return!!e&&(v.includes(e)||v.some(s=>e.endsWith(s)))};var h=(e=>(e.free="free",e.startup="startup",e.business="business",e.enterprise="enterprise",e))(h||{});async function j({path:e,baseApiUrl:s,method:i="GET",headers:t={},params:o,body:a}){const c=`${s||U({isDev:!1,isStage:!1})}${e}`,r={method:i,headers:{"Content-Type":"application/json",...t}};a&&(r.body=JSON.stringify(a));const u=o?new URLSearchParams(o).toString():"",n=u?`?${u}`:"",l=await fetch(`${c}${n}`,r);if(!l.ok)throw new Error(`HTTP error! status: ${l.status}`);return l.json()}const M={[h.free]:0,[h.startup]:10,[h.business]:20,[h.enterprise]:30};function O(e){const s=e;return s.init=i=>t=>e(t,i),s}const B=e=>O(e);async function K({editor:e,plan:s,pluginName:i,licenseKey:t,cleanup:o}){let a="",p=!1;const c=W(),r=n=>{console.warn("Cleanup plugin:",i,"Reason:",n),o()},u=(n={})=>{var L;const{error:l,sdkLicense:w}=n,P=(L=n.plan)==null?void 0:L.category;if(!(w||n.license)||l)r(l||"Invalid license");else if(P){const C=M[s],I=M[P];C>I&&r({pluginRequiredPlan:s,licensePlan:P})}};e.on(S,n=>{a=n==null?void 0:n.baseApiUrl,p=!0}),e.on(R,n=>{u(n)}),setTimeout(async()=>{if(!p){if(c)return;if(t){const n=await F({licenseKey:t,pluginName:i,baseApiUrl:a});n&&u(n)}else r("The `licenseKey` option not provided")}},2e3)}async function F(e){const{licenseKey:s,pluginName:i,baseApiUrl:t}=e;try{return(await j({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 _="gjs-plg-",G="listPagesComponent",b="data-type-role",q="page_root",X=e=>s=>s.is(e),Y=e=>(s,i)=>i.is(e),A=e=>s=>{var i;return((i=s.getAttribute)==null?void 0:i.call(s,b))===e};return B(function(e,s={}){const{Blocks:i,Components:t,Pages:o}=e,{licenseKey:a,block:p={}}=s,c="List Pages",r="List Pages Item",u="Link",n=`${_}${m}`,l=`${_}${d}`,w='<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>',P='<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:w,category:"Extra",select:!0,...s.block},isComponent:A(m),model:{defaults:{tagName:"ul",name:c,[q]:"",classes:n,attributes:{[b]:m},droppable:X(f),styles:`
          .${n} {
            list-style: none;
            margin: 0;
            padding: 0;
          }
        `},init(){this.syncPageItems=y(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 T=this.em.Pages.getAll(),L=this.components(),C=N(this);T.forEach((I,Z)=>{const $=I.id,V=I.getName(),H=C[$];H?H==null||H.forEach(x=>{x.getInnerHTML()!==V&&x.components(V)}):L.add({type:f,components:[{type:d,attributes:{href:`page://${$}`},components:{type:"textnode",content:V}}]},{at:Z})})}}}),t.addType(f,{isComponent:A(f),model:{defaults:{name:r,tagName:"li",icon:P,attributes:{[b]:f},components:{type:d},draggable:Y(m)}}}),t.addType(d,{extend:"link",isComponent:A(d),model:{defaults:{removable:!1,copyable:!1,delegate:{move:T=>T.closestType(f)},name:u,classes:l,attributes:{[b]:d},components:{type:"textnode",content:"Item"},styles:`
          .${l} {
            text-decoration: none;
          }
        `}}}),K({editor:e,licenseKey:a,plan:h.startup,pluginName:G,cleanup:()=>{i.remove(m),t.removeType(m)}})})});
