import { defineComponent, inject, computed, ref, watch, onMounted, onUnmounted, openBlock, createBlock, unref, withCtx, createElementVNode, createElementBlock, Fragment, renderList, normalizeClass, toDisplayString } from "vue";
import { r as resolveBasemapThumbnail, l as layerEventBus, X, b as _export_sfc } from "./index-e8d5fc75.mjs";
import "@element-plus/icons-vue";
import "ol";
import "proj4";
import "@supermapgis/iclient-ol";
import "element-plus";
const _hoisted_1 = { class: "basemap-content" };
const _hoisted_2 = { class: "basemap-grid" };
const _hoisted_3 = ["onClick"];
const _hoisted_4 = { class: "basemap-preview" };
const _hoisted_5 = ["src", "alt"];
const _hoisted_6 = { class: "basemap-name" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "BasemapPanel",
  props: {
    modelValue: { type: Boolean }
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const tdtImgUrl = resolveBasemapThumbnail({
      type: "tianditu-img",
      id: "tianditu-img"
    });
    const tdtVecUrl = resolveBasemapThumbnail({
      type: "tianditu-vec",
      id: "tianditu-vec"
    });
    const gaodeImgUrl = resolveBasemapThumbnail({
      type: "gaode-img",
      id: "gaode-img"
    });
    const gaodeVecUrl = resolveBasemapThumbnail({
      type: "gaode-vec",
      id: "gaode-vec"
    });
    const bdImgUrl = resolveBasemapThumbnail({
      type: "baidu-img",
      id: "baidu-img"
    });
    const bdVecUrl = resolveBasemapThumbnail({
      type: "baidu-vec",
      id: "baidu-vec"
    });
    const tencentImgUrl = resolveBasemapThumbnail({
      type: "tencent-img",
      id: "tencent-img"
    });
    const tencentVecUrl = resolveBasemapThumbnail({
      type: "tencent-vec",
      id: "tencent-vec"
    });
    const osmUrl = resolveBasemapThumbnail({ type: "osm", id: "osm" });
    const customBlueUrl = resolveBasemapThumbnail({ type: "custom", id: "custom" });
    const props = __props;
    const emit = __emit;
    const layerPanelConfig = inject("layerPanelConfig", { baseLayers: [] });
    const visible = computed({
      get: () => props.modelValue,
      set: (value) => {
        if (!value) {
          emit("update:modelValue", false);
        }
      }
    });
    const getInitialSelectedBasemap = () => {
      var _a;
      if (((_a = layerPanelConfig.value) == null ? void 0 : _a.baseLayers) && layerPanelConfig.value.baseLayers.length > 0) {
        let lastVisibleBasemap = null;
        for (let i = layerPanelConfig.value.baseLayers.length - 1; i >= 0; i--) {
          const layer = layerPanelConfig.value.baseLayers[i];
          if (layer.visible === true) {
            lastVisibleBasemap = layer.id;
            break;
          }
        }
        if (lastVisibleBasemap) {
          return lastVisibleBasemap;
        }
      }
      return null;
    };
    const selectedBasemap = ref(getInitialSelectedBasemap());
    const handleClose = () => {
      emit("update:modelValue", false);
    };
    const handleBasemapChanged = (event) => {
      console.log("BasemapPanel: 收到底图切换事件，同步UI状态", event);
      if (selectedBasemap.value !== event.basemapId) {
        selectedBasemap.value = event.basemapId;
      }
    };
    watch(
      () => layerPanelConfig.value,
      () => {
        const newSelectedBasemap = getInitialSelectedBasemap();
        if (selectedBasemap.value !== newSelectedBasemap) {
          selectedBasemap.value = newSelectedBasemap;
        }
      },
      { deep: true, immediate: false }
    );
    watch(
      () => selectedBasemap.value,
      (newValue, oldValue) => {
        console.log(
          "BasemapPanel: selectedBasemap changed from",
          oldValue,
          "to",
          newValue
        );
      }
    );
    onMounted(() => {
      layerEventBus.on("basemap-changed", handleBasemapChanged);
    });
    onUnmounted(() => {
      layerEventBus.off("basemap-changed", handleBasemapChanged);
    });
    const defaultBasemaps = [
      {
        id: "tianditu-img",
        name: "天地图卫星",
        type: "tianditu-img",
        thumbnail: tdtImgUrl
      },
      {
        id: "tianditu-vec",
        name: "天地图矢量",
        type: "tianditu-vec",
        thumbnail: tdtVecUrl
      },
      {
        id: "gaode-img",
        name: "高德卫星",
        type: "gaode-img",
        thumbnail: gaodeImgUrl
      },
      {
        id: "gaode-vec",
        name: "高德矢量",
        type: "gaode-vec",
        thumbnail: gaodeVecUrl
      },
      {
        id: "baidu-img",
        name: "百度影像",
        type: "baidu-img",
        thumbnail: bdImgUrl
      },
      {
        id: "baidu-vec",
        name: "百度矢量",
        type: "baidu-vec",
        thumbnail: bdVecUrl
      },
      {
        id: "tencent-img",
        name: "腾讯影像",
        type: "tencent-img",
        thumbnail: tencentImgUrl
      },
      {
        id: "tencent-vec",
        name: "腾讯矢量",
        type: "tencent-vec",
        thumbnail: tencentVecUrl
      },
      {
        id: "osm",
        name: "OpenStreetMap",
        type: "osm",
        thumbnail: osmUrl
      },
      {
        id: "custom-blue",
        name: "自定义蓝色",
        type: "custom",
        thumbnail: customBlueUrl,
        url: "https://your-custom-tile-server/{z}/{x}/{y}.png"
      }
    ];
    const displayBasemaps = computed(() => {
      var _a;
      if (((_a = layerPanelConfig.value) == null ? void 0 : _a.baseLayers) && layerPanelConfig.value.baseLayers.length > 0) {
        return layerPanelConfig.value.baseLayers.map((layer) => ({
          id: layer.id,
          name: layer.name,
          type: layer.type,
          url: layer.url,
          visible: layer.visible,
          opacity: layer.opacity,
          attribution: layer.attribution,
          maxZoom: layer.maxZoom,
          thumbnail: getBasemapThumbnail(layer.type, layer.id, layer)
        }));
      }
      return defaultBasemaps;
    });
    const getBasemapThumbnail = (type, id, layer) => {
      return resolveBasemapThumbnail({
        type,
        id,
        thumbnail: layer == null ? void 0 : layer.thumbnail,
        baseImgUrl: layer == null ? void 0 : layer.baseImgUrl
      });
    };
    const selectBasemap = (basemap) => {
      const wasSelected = selectedBasemap.value === basemap.id;
      selectedBasemap.value = wasSelected ? null : basemap.id;
      if (wasSelected) {
        layerEventBus.emit("basemap-switch-request", {
          basemapId: basemap.id,
          basemapName: basemap.name,
          basemapType: basemap.type,
          basemapConfig: null
          // 传递null表示关闭底图
        });
        console.log("BasemapPanel: 请求关闭底图:", basemap.name);
      } else {
        layerEventBus.emit("basemap-switch-request", {
          basemapId: basemap.id,
          basemapName: basemap.name,
          basemapType: basemap.type,
          basemapConfig: basemap
        });
        console.log("BasemapPanel: 请求切换底图到:", basemap.name);
      }
    };
    return (_ctx, _cache) => {
      return openBlock(), createBlock(unref(X), {
        modelValue: visible.value,
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => visible.value = $event),
        title: "底图选择",
        width: "400px",
        height: "500px",
        "show-modal": false,
        draggable: true,
        resizable: true,
        "modal-class": "pointer-events-none",
        "cache-id": "basemap-dialog",
        position: "right",
        class: "basemap-dialog",
        "append-to-body": "",
        onClose: handleClose
      }, {
        default: withCtx(() => [
          createElementVNode("div", _hoisted_1, [
            createElementVNode("div", _hoisted_2, [
              (openBlock(true), createElementBlock(Fragment, null, renderList(displayBasemaps.value, (basemap) => {
                return openBlock(), createElementBlock("div", {
                  key: basemap.id,
                  class: normalizeClass(["basemap-item", { active: selectedBasemap.value === basemap.id }]),
                  onClick: ($event) => selectBasemap(basemap)
                }, [
                  createElementVNode("div", _hoisted_4, [
                    createElementVNode("img", {
                      src: basemap.thumbnail,
                      alt: basemap.name,
                      loading: "lazy"
                    }, null, 8, _hoisted_5)
                  ]),
                  createElementVNode("div", _hoisted_6, toDisplayString(basemap.name), 1)
                ], 10, _hoisted_3);
              }), 128))
            ])
          ])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
});
const BasemapPanel = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-5709013f"]]);
export {
  BasemapPanel as default
};
