import{Modal as e,Form as i,Row as t,Col as n,Select as a,Input as r,Button as l,Spin as o}from"antd";import{useRef as d,useState as s,useEffect as c}from"react";import{dynamicLoadJs as u,dynamicLoadCss as p}from"../../utils/index.js";import w from"./CesiumMap.js";import"./index.less";import{jsxs as m,jsx as h}from"react/jsx-runtime";const g=g=>{const{visible:y,onClose:b,longitude:v,latitude:C,onConfirm:f,area:L="",showArea:M=!1,disable:B=!1,type:k="baidu"}=g,j=d(null),x=d(null),D=d(null),[G,O]=s(!1),[I,A]=s(v||""),[R,z]=s(C||""),[K,P]=s(""),[T,S]=s("");c(()=>{A(v||""),z(C||""),S(L||"")},[v,C,L]);const W=()=>{if(!window?.BMapGL?.Map)return void setTimeout(()=>{W()},50);const e=new window.BMapGL.Map(j.current);if(x.current=e,e.centerAndZoom(new window.BMapGL.Point(v||window.mapLongitude,C||window.mapLatitude),16),e.enableScrollWheelZoom(!0),v&&C){const i=new window.BMapGL.Point(v,C),t=new window.BMapGL.Marker(i);e.addOverlay(t)}B||e.addEventListener("click",i=>{e.clearOverlays();const t=new window.BMapGL.Point(i.latlng.lng,i.latlng.lat),n=new window.BMapGL.Marker(t);e.addOverlay(n),z(i.latlng.lat),A(i.latlng.lng)})},E=()=>{if(O(!0),j.current){if(!x.current){if("baidu"===k)return void W();"cesium"===k&&(()=>{const{init:e,flyTo:i,addMarkPoint:t,getLongitudeAndLatitude:n}=new w,{viewer:a}=e();x.current=a,i({longitude:v||window.mapLongitude,latitude:C||window.mapLatitude,height:9e5}),v&&C&&t({longitude:v,latitude:C}),B||n((e,i)=>{if(e)return;const{longitude:t,latitude:n}=i;z(n),A(t)})})()}O(!1)}},Z=()=>{P(""),x.current&&(x.current.destroy(),x.current=null),b&&b()};c(()=>{let e;return y&&(e=setTimeout(()=>{(()=>{if(window.mapLongitude||window.mapLatitude)return"baidu"===k?window.mapBaiDuKey?void(async()=>{window.BMapGL?E():window?.base?.loadDynamicResource?(await window.base.loadDynamicResource({url:`https://api.map.baidu.com/api?v=1.0&type=webgl&ak=${window.mapBaiDuKey}&callback=initialize`,type:"script",attr:{type:"text/javascript"}}),E()):(await u(`https://api.map.baidu.com/api?v=1.0&type=webgl&ak=${window.mapBaiDuKey}&callback=initialize`),E())})():void console.error("【Map】 请在window设置变量 mapBaiDuKey，以供地图初始化加载使用"):void("cesium"===k&&(async()=>{window.Cesium?E():window?.base?.loadDynamicResource?(await window.base.loadDynamicResource({url:"https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Cesium.js",type:"script",attr:{type:"text/javascript"}}),await window.base.loadDynamicResource({url:"https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Widgets/widgets.css",type:"link",attr:{rel:"stylesheet",type:"text/css"}}),E()):(await u("https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Cesium.js"),await p("https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Widgets/widgets.css"),E())})());console.error("【Map】 请在window设置变量 mapLongitude 和 mapLatitude，以供地图初始化坐标使用")})()},100)),()=>{e&&clearTimeout(e)}},[y]);const $=()=>{if(x.current){try{x.current.clearOverlays(),x.current.destroy(),x.current=null}catch(e){console.warn("Error destroying map on unmount:",e)}x.current=null}};return c(()=>()=>{$()},[]),m(e,{open:y,title:"坐标",onCancel:Z,width:1e3,destroyOnHidden:!1,maskClosable:!1,afterClose:$,footer:[h(l,{onClick:Z,children:"取消"},"back"),!B&&h(l,{type:"primary",onClick:()=>{f&&f(I,R,{area:T}),Z()},children:"确定"},"submit")],children:[m(i,{labelAlign:"right",labelCol:{span:6},wrapperCol:{span:18},children:[M&&h(t,{gutter:24,children:h(n,{span:12,children:h(i.Item,{label:"所属区域",children:m(a,{value:T,onChange:e=>S(e),allowClear:!0,children:[h(a.Option,{value:"1",children:"东港区"}),h(a.Option,{value:"2",children:"西港区"})]})})})}),!B&&"baidu"===k&&m(t,{gutter:24,children:[h(n,{span:12,children:h(i.Item,{label:"关键字搜索",children:h(r,{value:K,onChange:e=>P(e.target.value),allowClear:!0})})}),h(n,{span:12,children:m(i.Item,{label:" ",colon:!1,style:{textAlign:"right"},children:[h(l,{type:"primary",onClick:()=>{K&&x.current&&(D.current=new window.BMapGL.LocalSearch(x.current,{renderOptions:{map:x.current}}),D.current.search(K))},children:"搜索"}),h(l,{onClick:()=>{P(""),x.current&&D.current.search("")},style:{marginLeft:8},children:"重置"})]})})]}),m(t,{gutter:24,children:[h(n,{span:12,children:h(i.Item,{label:"经度",children:h(r,{disabled:!0,value:I})})}),h(n,{span:12,children:h(i.Item,{label:"纬度",children:h(r,{disabled:!0,value:R})})})]})]}),h("div",{ref:j,id:"map_container",style:{width:"100%",height:"500px",position:"relative"},children:h(o,{size:"large",tip:"地图正在加载中...",spinning:G,children:h("div",{style:{position:"absolute",top:0,left:0,right:0,bottom:0,display:"flex",justifyContent:"center",alignItems:"center",backgroundColor:"rgba(0, 0, 0, 0.5)",zIndex:1e3}})})})]})};g.displayName="MapSelector";export{g as default};
