import { React, type AllWidgetProps } from 'jimu-core'
import { MapViewManager, JimuMapViewComponent, type JimuMapView } from 'jimu-arcgis'
import reactiveUtils from 'esri/core/reactiveUtils'
const { useEffect, useState } = React
let handler
const Widget = (props: AllWidgetProps<any>) => {
const viewManager = MapViewManager.getInstance()
const mapView = viewManager.getJimuMapViewById(viewManager.getAllJimuMapViewIds()[0])
const [jimuMapView, setJimuMapView] = useState<JimuMapView>(mapView)
const [mapReady, setMapReady] = useState(false)
useEffect(() => {
if (jimuMapView) {
reactiveUtils
.whenOnce(() => jimuMapView.view.ready)
.then(() => {
setMapReady(true)
alert('map ready')
if (props.state === 'OPENED') {
alert('open')
handler = (jmv: JimuMapView) => {
alert('test')
if (jmv) { alert('Event Handler ready') } else { alert('Not Ready') }
}
} else if (props.state === 'CLOSED') {
alert('closed')
}
}
)
}
}, [jimuMapView, props.state])
return (
<div className="widget-starter jimu-widget">
{
props.useMapWidgetIds &&
props.useMapWidgetIds.length === 1 && (
<JimuMapViewComponent
useMapWidgetId={props.useMapWidgetIds?.[0]}
onActiveViewChange={handler}
/>
)
}
{/* <p>Lat/Lon: {latitude} {longitude} </p> */}
<p><h9>Click on Map to get the coordinates </h9></p>
</div>
)
}
export default Widget