(()=>{"use strict";const e=window.wp.blocks,t=window.wp.i18n,n=window.wp.data,o=window.wp.components,r=window.wp.blockEditor,s=window.wp.element,i=window.ReactJSXRuntime,a=JSON.parse('{"UU":"inventory-presser/map"}');(0,e.getBlockType)(a.UU)||(0,e.registerBlockType)(a.UU,{edit:function({attributes:e,setAttributes:a}){const{locationSlug:l=[]}=e,c=(0,r.useBlockProps)(),d=(0,s.useRef)(null),p=(0,s.useRef)(null),u=(0,n.useSelect)(e=>e("core").getEntityRecords("taxonomy","location",{per_page:-1,hide_empty:!1})||[],[]),m=u.filter(e=>-1!==l.indexOf(e.slug)&&e.meta?.address_lat&&e.meta?.address_lon).map(e=>({name:e.name,lat:parseFloat(e.meta.address_lat),lon:parseFloat(e.meta.address_lon),address:e.description?e.description.replace(/\r/g,"").replace(/\n/g,"<br />"):""})),h="undefined"!=typeof invp_blocks?invp_blocks.mapbox_public_token:"",w=m.length>0&&!!h,g=l.join(","),b=m.map(e=>`${e.lat},${e.lon}`).join("|");let f;return(0,s.useEffect)(()=>{const e=d.current,t=window.L;if(p.current&&(p.current.remove(),p.current=null),!e||!t||!h||0===m.length)return;const n=t.map(e,{dragging:!1,touchZoom:!1,doubleClickZoom:!1,scrollWheelZoom:!1,boxZoom:!1,keyboard:!1});p.current=n,t.tileLayer("https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token="+h,{attribution:'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',id:"mapbox/streets-v11",tileSize:512,zoomOffset:-1,accessToken:h}).addTo(n);const o=m.map(e=>{const o=t.marker([e.lat,e.lon]).addTo(n);return o.bindPopup("<b>"+e.name+"</b><br />"+e.address),o});if(1===o.length)n.setView([m[0].lat,m[0].lon],13);else{const e=new t.featureGroup(o);n.fitBounds(e.getBounds())}return()=>{p.current&&(p.current.remove(),p.current=null)}},[g,b,h]),l.length?h?u.length&&!m.length&&(f=(0,t.__)("No map coordinates found for the selected locations.","inventory-presser")):f=(0,t.__)("A Mapbox API token is required. Add one in Inventory Presser settings.","inventory-presser"):f=(0,t.__)("Select one or more locations in the block settings.","inventory-presser"),(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(r.InspectorControls,{children:(0,i.jsx)(o.PanelBody,{title:(0,t.__)("Locations","inventory-presser"),initialOpen:!0,children:u?.length?u.map(e=>(0,i.jsx)(o.CheckboxControl,{label:e.name,checked:-1!==l.indexOf(e.slug),onChange:()=>function(e){const t=-1===l.indexOf(e)?[...l,e]:l.filter(t=>t!==e);a({locationSlug:t})}(e.slug)},e.id)):(0,i.jsx)("p",{children:(0,t.__)("No locations found. Add Location terms in the admin screen.","inventory-presser")})})}),(0,i.jsxs)("div",{...c,children:[f&&(0,i.jsx)("p",{children:f}),(0,i.jsx)("div",{ref:d,className:"invp-map",style:{width:"100%",height:w?"300px":"0",display:w?"block":"none"}})]})]})}})})();