(()=>{'use strict'; const params=new URLSearchParams(location.search), q=(k,d)=>params.get(k)??d, has=k=>params.has(k), flag=(k,d=true)=>has(k)?!['0','false','no','off'].includes(q(k,'').toLowerCase()):d; const n=(k,d,lo,hi)=>{if(!has(k)||!q(k,'').trim())return d;const v=Number(q(k,''));return Number.isFinite(v)?Math.max(lo,Math.min(hi,v)):d}; const status=document.getElementById('status'), panel=document.querySelector('.panel'); const ui=q('ui','full'), controls=flag('controls',ui==='full'), interactive=flag('interactive'), buildings=flag('buildings'), sync=flag('sync',false); if(ui==='minimal'||ui==='none')panel.hidden=true; const center=[n('lon',2.3522,-180,180),n('lat',48.8566,-85,85)]; const resolve=u=>new URL(u,location.href).href.replace(/%7B/gi,'{').replace(/%7D/gi,'}'); const styleURL=q('style',''); const source=q('source','openfreemap'); const tileURL=q('tiles',''); function rasterStyle(tiles){return {version:8,sources:{raster:{type:'raster',tiles:[resolve(tiles)],tileSize:n('tileSize',256,128,512),attribution:q('attribution','')}} ,layers:[{id:'raster',type:'raster',source:'raster'}]}} function vectorStyle(tiles){return {version:8,sources:{custom:{type:'vector',tiles:[resolve(tiles)]}},layers:[]}} function localStyle(){return fetch('/web/style.json').then(r=>{if(!r.ok)throw Error('style '+r.status);return r.json()}).then(async s=>{const t=await fetch('/web/planet.json').then(r=>r.json());t.tiles=t.tiles.map(u=>resolve(u.replace(/^https?:\/\/192\.168\.1\.209:8088/,'').replace(/^https?:\/\/tiles\.openfreemap\.org/,'/ofm')));s.sources.openmaptiles={...t,type:'vector'};if(s.sprite)s.sprite=resolve(s.sprite);if(s.glyphs)s.glyphs=resolve(s.glyphs);return s})} async function chooseStyle(){if(styleURL)return resolve(styleURL);if(source==='raster'||source==='xyz') {if(!tileURL)throw Error('Укажи &tiles=...');return rasterStyle(tileURL)}if(source==='vector'){if(!tileURL)throw Error('Укажи &tiles=...');return vectorStyle(tileURL)}if(source==='pmtiles'){const file=q('file','');if(!file)throw Error('Укажи &file=...');if(!window.pmtiles)throw Error('PMTiles library missing');const protocol=new pmtiles.Protocol();maplibregl.addProtocol('pmtiles',protocol.tile);return {version:8,sources:{custom:{type:'vector',url:'pmtiles://'+resolve(file)}},layers:[]}}return localStyle()} function pin(map,raw,label){const a=raw.split(',').map(Number);if(a.length<2||!a.every(Number.isFinite)||Math.abs(a[0])>90||Math.abs(a[1])>180)return;const m=new maplibregl.Marker().setLngLat([a[1],a[0]]);if(label)m.setPopup(new maplibregl.Popup().setText(label));m.addTo(map)} function share(map){const u=new URL(location.href),c=map.getCenter();for(const [k,v] of Object.entries({lat:c.lat.toFixed(6),lon:c.lng.toFixed(6),zoom:map.getZoom().toFixed(2),pitch:map.getPitch().toFixed(1),bearing:map.getBearing().toFixed(1)}))u.searchParams.set(k,v);return u} async function run(){const style=await chooseStyle();const map=new maplibregl.Map({container:'map',style,center,zoom:n('zoom',11,0,22),pitch:n('pitch',0,0,85),bearing:n('bearing',0,-360,360),interactive,attributionControl:true});window.map=map; if(controls){map.addControl(new maplibregl.NavigationControl());map.addControl(new maplibregl.ScaleControl())} if(has('bbox')){const a=q('bbox','').split(',').map(Number);if(a.length===4&&a.every(Number.isFinite)&&a[0]{status.textContent='Готово';if(buildings&&map.getSource('openmaptiles')&&!map.getLayer('map-3d'))map.addLayer({id:'map-3d',type:'fill-extrusion',source:'openmaptiles','source-layer':'building',minzoom:13,paint:{'fill-extrusion-color':'#a5b6c6','fill-extrusion-height':['coalesce',['get','render_height'],['get','height'],8],'fill-extrusion-base':['coalesce',['get','render_min_height'],0],'fill-extrusion-opacity':0.78}}); if(has('geojson')){const url=resolve(q('geojson',''));map.addSource('user-geojson',{type:'geojson',data:url});map.addLayer({id:'user-geojson-fill',type:'fill',source:'user-geojson',filter:['==',['geometry-type'],'Polygon'],paint:{'fill-color':'#4c9aff','fill-opacity':0.25}});map.addLayer({id:'user-geojson-line',type:'line',source:'user-geojson',paint:{'line-color':'#4c9aff','line-width':3}});map.addLayer({id:'user-geojson-points',type:'circle',source:'user-geojson',filter:['==',['geometry-type'],'Point'],paint:{'circle-color':'#ffb74d','circle-radius':6}})} }); map.on('error',e=>{status.textContent='Ошибка: '+(e.error?.message||e.message)}); if(sync)map.on('moveend',()=>history.replaceState(null,'',share(map))); document.getElementById('copy').onclick=async()=>{const url=share(map).toString();try{await navigator.clipboard.writeText(url);document.getElementById('copy').textContent='Скопировано'}catch{prompt('Ссылка:',url)}}; } run().catch(e=>{status.textContent='Ошибка: '+e.message}); })();