"use strict";
const common_vendor = require("../common/vendor.js");
const common_assets = require("../common/assets.js");
if (!Array) {
  const _easycom_uv_icon2 = common_vendor.resolveComponent("uv-icon");
  _easycom_uv_icon2();
}
const _easycom_uv_icon = () => "../node-modules/@climblee/uv-ui/components/uv-icon/uv-icon.js";
if (!Math) {
  _easycom_uv_icon();
}
const _sfc_main = {
  __name: "MapComponent",
  props: {
    userLocation: {
      type: Object,
      default: null
    },
    positionList: {
      type: Array,
      default: () => []
    },
    filteredPositions: {
      type: Array,
      default: () => []
    },
    searchKeyword: {
      type: String,
      default: ""
    }
  },
  emits: [
    "relocate",
    "scan",
    "showList",
    "markerTap",
    "mapCenterChange"
  ],
  setup(__props, { expose: __expose, emit: __emit }) {
    const collapseRef = common_vendor.ref(null);
    common_vendor.ref([
      {
        title: "扫码使用",
        desc: "找到附近设备，扫描设备上的二维码"
      },
      {
        title: "免押金支付",
        desc: "无需支付押金，使用支付分免押即可完成租借"
      },
      {
        title: "开始使用",
        desc: "设备自动解锁，风扇弹出后取出即可开始使用"
      },
      {
        title: "归还设备",
        desc: "使用完毕后，按照设备规格要求将风扇还入即可结束订单"
      }
    ]);
    const props = __props;
    const emit = __emit;
    const isLoading = common_vendor.ref(true);
    const mapCenter = common_vendor.ref({
      longitude: 116.397128,
      latitude: 39.916527
    });
    const mapZoom = common_vendor.ref(17);
    const mapMarkers = common_vendor.ref([]);
    const mapContext = common_vendor.ref(null);
    const updateMapMarkers = () => {
      mapMarkers.value = [];
      if (props.userLocation) {
        mapMarkers.value.push({
          id: 0,
          // ID必须是数字
          // iconPath: '/static/scan-icon.png',
          width: 32,
          height: 32,
          latitude: props.userLocation.latitude,
          longitude: props.userLocation.longitude,
          title: "我的位置",
          callout: {
            content: "我的位置",
            color: "#ffffff",
            fontSize: 12,
            borderRadius: 4,
            bgColor: "#2196F3",
            padding: 6,
            display: "BYCLICK"
            // 点击时显示
          },
          customCallout: {
            anchorX: 0,
            anchorY: 0
          }
        });
      }
      if (props.filteredPositions && props.filteredPositions.length > 0) {
        props.filteredPositions.forEach((pos, index) => {
          if (pos.longitude && pos.latitude) {
            const lat = parseFloat(pos.latitude);
            const lng = parseFloat(pos.longitude);
            if (lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180) {
              mapMarkers.value.push({
                id: index + 1,
                // ID必须是数字，避免和用户位置的ID冲突
                // iconPath: '/static/scan-icon.png',
                width: 30,
                height: 30,
                latitude: lat,
                longitude: lng,
                title: pos.name,
                position: pos,
                // 存储原始位置数据，用于事件处理
                callout: {
                  content: pos.name,
                  color: "#333333",
                  fontSize: 12,
                  borderRadius: 4,
                  bgColor: "#ffffff",
                  padding: 6,
                  display: "BYCLICK"
                  // 点击时显示
                }
              });
            } else {
              common_vendor.index.__f__("warn", "at components/MapComponent.vue:176", `忽略无效坐标: ${pos.name}, 纬度=${lat}, 经度=${lng}`);
            }
          }
        });
      }
      isLoading.value = false;
    };
    const moveToLocation = (location) => {
      if (!location || !location.longitude || !location.latitude)
        return;
      if (mapContext.value) {
        mapContext.value.moveToLocation({
          longitude: location.longitude,
          latitude: location.latitude,
          success: () => {
            common_vendor.index.__f__("log", "at components/MapComponent.vue:194", "地图已移动到指定位置");
          },
          fail: (error) => {
            common_vendor.index.__f__("error", "at components/MapComponent.vue:197", "移动地图失败:", error);
          }
        });
      }
    };
    common_vendor.watch(() => props.userLocation, (newLocation) => {
      if (newLocation && newLocation.longitude && newLocation.latitude) {
        mapCenter.value = {
          longitude: newLocation.longitude,
          latitude: newLocation.latitude
        };
        updateMapMarkers();
        moveToLocation(newLocation);
      }
    }, {
      immediate: true,
      deep: true
    });
    common_vendor.watch(() => props.filteredPositions, (newPositions) => {
      updateMapMarkers();
    }, {
      deep: true
    });
    const onMapUpdated = () => {
      isLoading.value = false;
    };
    const onMapRegionChange = (e) => {
      if (e.type === "end" && e.causedBy === "drag") {
        if (mapContext.value) {
          mapContext.value.getCenterLocation({
            success: (res) => {
              if (res.longitude && res.latitude) {
                mapCenter.value = {
                  longitude: res.longitude,
                  latitude: res.latitude
                };
                emit("mapCenterChange", mapCenter.value);
              }
            }
          });
        }
      }
    };
    const onMapMarkerTap = (e) => {
      const markerId = e.markerId;
      const marker = mapMarkers.value.find((item) => item.id === markerId);
      if (marker) {
        if (markerId === 0) {
          common_vendor.index.showToast({
            title: "这是您的位置",
            icon: "none"
          });
          return;
        }
        if (marker.position) {
          emit("markerTap", marker.position);
        }
      }
    };
    const onCalloutTap = (e) => {
      const markerId = e.markerId;
      const marker = mapMarkers.value.find((item) => item.id === markerId);
      if (marker && marker.position) {
        emit("markerTap", marker.position);
      }
    };
    const onMapError = (error) => {
      common_vendor.index.__f__("error", "at components/MapComponent.vue:283", "地图加载失败:", error);
      isLoading.value = false;
    };
    const handleRelocate = () => {
      emit("relocate");
    };
    const handleScan = () => {
      emit("scan");
    };
    const handleShowList = () => {
      emit("showList");
    };
    common_vendor.onMounted(() => {
      common_vendor.nextTick$1(() => {
        mapContext.value = common_vendor.index.createMapContext("map");
        updateMapMarkers();
        if (collapseRef.value) {
          collapseRef.value.init();
        }
      });
    });
    common_vendor.onUnmounted(() => {
      mapContext.value = null;
    });
    __expose({
      mapCenter: common_vendor.computed(() => mapCenter.value),
      moveToLocation,
      updateMapMarkers,
      initCollapse: () => {
        if (collapseRef.value) {
          collapseRef.value.init();
        }
      }
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: mapCenter.value.longitude,
        b: mapCenter.value.latitude,
        c: mapMarkers.value,
        d: mapZoom.value,
        e: common_vendor.o(onMapRegionChange),
        f: common_vendor.o(onMapMarkerTap),
        g: common_vendor.o(onCalloutTap),
        h: common_vendor.o(onMapUpdated),
        i: common_vendor.o(onMapError),
        j: isLoading.value
      }, isLoading.value ? {} : {}, {
        k: common_vendor.p({
          name: "map-fill",
          size: "18"
        }),
        l: common_vendor.o(handleRelocate),
        m: common_assets._imports_0,
        n: common_vendor.o(handleScan),
        o: common_assets._imports_1$1,
        p: common_vendor.o(handleShowList)
      });
    };
  }
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-651a9dc3"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../.sourcemap/mp-weixin/components/MapComponent.js.map
