import { defineComponent, toRefs, ref, watch, onMounted, openBlock, createElementBlock } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";
const __default__ = defineComponent({ name: "EMapAmap" });
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: {
    chartConfig: {
      type: Object,
      required: true
    }
  },
  setup(__props) {
    const props = __props;
    let {
      amapKey,
      amapStyleKey,
      amapLon,
      amapLat,
      amapZindex,
      mapMarkerType,
      lang,
      amapStyleKeyCustom,
      features,
      viewMode,
      pitch,
      skyColor,
      marker
    } = toRefs(props.chartConfig.option.mapOptions);
    let mapIns = null;
    let markers = [];
    let AMapIns = null;
    const vChartRef = ref();
    const initMap = (newData) => {
      AMapLoader.load({
        key: amapKey.value,
        version: "1.4.8",
        plugins: ["AMap.PlaceSearch", "AMap.AutoComplete"]
      }).then((AMap) => {
        AMapIns = AMap;
        mapIns = new AMap.Map(vChartRef.value, {
          resizeEnable: true,
          zoom: amapZindex.value,
          center: [amapLon.value, amapLat.value],
          mapStyle: `amap://styles/${amapStyleKeyCustom.value !== "" ? amapStyleKeyCustom.value : amapStyleKey.value}`,
          lang: lang.value,
          features: features.value,
          pitch: pitch.value,
          skyColor: skyColor.value,
          viewMode: viewMode.value,
          willReadFrequently: true
        });
        dataHandle(props.chartConfig.option.dataset);
      }).catch((e) => {
      });
    };
    const dataHandle = (newData) => {
      if (!mapIns && !AMapIns) {
        initMap(props.chartConfig.option);
        return;
      }
      if (Array.isArray(newData.markers)) {
        mapIns.remove(markers);
        markers = [];
        if (mapMarkerType.value === "Marker") {
          newData.markers.forEach((markerItem) => {
            const markerInstance = new AMapIns.Marker({
              position: [markerItem.position[0], markerItem.position[1]],
              offset: new AMapIns.Pixel(-13, -30)
            });
            markers.push(markerInstance);
            markerInstance.setMap(mapIns);
          });
        } else if (mapMarkerType.value === "CircleMarker") {
          newData.markers.forEach((markerItem) => {
            const markerInstance = new AMapIns.CircleMarker({
              center: [markerItem.position[0], markerItem.position[1]],
              radius: markerItem.value,
              ...marker.value
            });
            markers.push(markerInstance);
            markerInstance.setMap(mapIns);
          });
        }
      }
    };
    watch(
      () => props.chartConfig.option.mapOptions,
      (option) => {
        initMap();
      },
      {
        immediate: true,
        deep: true
      }
    );
    watch(
      () => props.chartConfig.option.dataset,
      (newData) => {
        try {
          dataHandle(newData);
        } catch (error) {
          console.log(error);
        }
      },
      {
        deep: false
      }
    );
    onMounted(() => {
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        ref_key: "vChartRef",
        ref: vChartRef
      }, null, 512);
    };
  }
});
export { _sfc_main as default };
