import { defineComponent, inject, computed, ref, reactive, resolveComponent, openBlock, createBlock, unref, withCtx, createElementVNode, createVNode, createTextVNode } from "vue";
import { Aim } from "@element-plus/icons-vue";
import { X, T as TooltipHelper, b as _export_sfc } from "./index-e8d5fc75.mjs";
import "ol";
import "proj4";
import "@supermapgis/iclient-ol";
import "element-plus";
const _hoisted_1 = { class: "coordinate-content" };
const _hoisted_2 = { class: "format-selection" };
const _hoisted_3 = { class: "coordinate-input" };
const _hoisted_4 = { class: "operation-tip" };
const _hoisted_5 = { class: "dialog-footer" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "CoordinateLocationDialog",
  props: {
    modelValue: { type: Boolean }
  },
  emits: ["update:modelValue"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const EPlusDialog = X;
    const props = __props;
    const emit = __emit;
    const measurementToolRef = inject("measurementTool");
    const measurementTool = computed(() => measurementToolRef == null ? void 0 : measurementToolRef.value);
    const mapOperationToolRef = inject("mapOperationTool");
    const mapOperationTool = computed(() => mapOperationToolRef == null ? void 0 : mapOperationToolRef.value);
    const mapRef = inject("map");
    const map = computed(() => mapRef == null ? void 0 : mapRef.value);
    let tooltipHelper = null;
    const dialogVisible = computed({
      get: () => props.modelValue,
      set: (value) => emit("update:modelValue", value)
    });
    const selectedFormat = ref("decimal");
    const coordinateForm = reactive({
      longitude: "116.709805",
      latitude: "39.926296"
    });
    const decimalToDMS = (decimal) => {
      const degrees = Math.floor(Math.abs(decimal));
      const minutes = Math.floor((Math.abs(decimal) - degrees) * 60);
      const seconds = ((Math.abs(decimal) - degrees) * 60 - minutes) * 60;
      const direction = decimal >= 0 ? "" : "-";
      return `${direction}${degrees}°${minutes}'${seconds.toFixed(2)}"`;
    };
    const dmsToDecimal = (dms) => {
      const regex = /(-?)(\d+)°(\d+)'([\d.]+)"/;
      const match = dms.match(regex);
      if (!match)
        return 0;
      const [, sign, degrees, minutes, seconds] = match;
      const decimal = parseInt(degrees) + parseInt(minutes) / 60 + parseFloat(seconds) / 3600;
      return sign === "-" ? -decimal : decimal;
    };
    const onFormatChange = () => {
      console.log("坐标格式变化:", selectedFormat.value);
      updateCoordinateDisplay();
    };
    const updateCoordinateDisplay = () => {
      if (!coordinateForm.longitude || !coordinateForm.latitude)
        return;
      const lng = parseFloat(coordinateForm.longitude);
      const lat = parseFloat(coordinateForm.latitude);
      if (isNaN(lng) || isNaN(lat))
        return;
      if (selectedFormat.value === "dms") {
        if (!coordinateForm.longitude.includes("°")) {
          coordinateForm.longitude = decimalToDMS(lng);
          coordinateForm.latitude = decimalToDMS(lat);
        }
      } else {
        if (coordinateForm.longitude.includes("°")) {
          coordinateForm.longitude = dmsToDecimal(coordinateForm.longitude).toFixed(
            6
          );
          coordinateForm.latitude = dmsToDecimal(coordinateForm.latitude).toFixed(
            6
          );
        }
      }
    };
    const getCurrentLocation = () => {
      if (!measurementTool.value) {
        console.warn("measurementTool未初始化");
        console.warn("测量工具未初始化");
        return;
      }
      if (!map.value) {
        console.warn("地图对象未初始化");
        console.warn("地图未初始化");
        return;
      }
      console.log("开始图上拾取坐标");
      console.log("请在地图上点击选择位置");
      if (!tooltipHelper) {
        tooltipHelper = new TooltipHelper(map.value);
      }
      tooltipHelper.showTooltip("点击地图获取坐标信息");
      measurementTool.value.startCoordinateMeasurement((result) => {
        if (result && result.longitude && result.latitude) {
          setPickedCoordinate(
            parseFloat(result.longitude),
            parseFloat(result.latitude)
          );
          if (tooltipHelper) {
            tooltipHelper.hideTooltip();
          }
        }
      });
    };
    const setPickedCoordinate = (longitude, latitude) => {
      if (selectedFormat.value === "decimal") {
        coordinateForm.longitude = longitude.toFixed(6);
        coordinateForm.latitude = latitude.toFixed(6);
      } else {
        coordinateForm.longitude = decimalToDMS(longitude);
        coordinateForm.latitude = decimalToDMS(latitude);
      }
      console.log("坐标已获取");
    };
    const locateOnMap = () => {
      if (!coordinateForm.longitude || !coordinateForm.latitude) {
        console.warn("请输入经纬度坐标");
        return;
      }
      if (!mapOperationTool.value) {
        console.warn("地图操作工具未初始化");
        return;
      }
      let longitude;
      let latitude;
      try {
        if (selectedFormat.value === "dms") {
          longitude = dmsToDecimal(coordinateForm.longitude);
          latitude = dmsToDecimal(coordinateForm.latitude);
        } else {
          longitude = parseFloat(coordinateForm.longitude);
          latitude = parseFloat(coordinateForm.latitude);
        }
        if (isNaN(longitude) || isNaN(latitude)) {
          console.error("坐标格式不正确");
          return;
        }
        console.log("定位到坐标:", { longitude, latitude });
        mapOperationTool.value.locateToCoordinate(longitude, latitude);
      } catch (error) {
        console.error("坐标解析失败");
      }
    };
    const handleClose = () => {
      if (measurementTool.value) {
        measurementTool.value.clearMeasurements();
      }
      if (tooltipHelper) {
        tooltipHelper.destroy();
        tooltipHelper = null;
      }
      dialogVisible.value = false;
    };
    __expose({
      setPickedCoordinate
    });
    return (_ctx, _cache) => {
      const _component_el_radio_button = resolveComponent("el-radio-button");
      const _component_el_radio_group = resolveComponent("el-radio-group");
      const _component_LocationInformation = resolveComponent("LocationInformation");
      const _component_el_icon = resolveComponent("el-icon");
      const _component_el_input = resolveComponent("el-input");
      const _component_el_form_item = resolveComponent("el-form-item");
      const _component_el_form = resolveComponent("el-form");
      const _component_InfoFilled = resolveComponent("InfoFilled");
      const _component_Pointer = resolveComponent("Pointer");
      const _component_el_button = resolveComponent("el-button");
      return openBlock(), createBlock(unref(EPlusDialog), {
        modelValue: dialogVisible.value,
        "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => dialogVisible.value = $event),
        title: "坐标拾取",
        width: "400px",
        "cache-id": "coordinate-dialog",
        "show-modal": false,
        "modal-class": "pointer-events-none",
        class: "coordinate-dialog",
        onClose: handleClose
      }, {
        footer: withCtx(() => [
          createElementVNode("div", _hoisted_5, [
            createVNode(_component_el_button, {
              class: "action-btn pick-btn",
              onClick: getCurrentLocation
            }, {
              default: withCtx(() => [
                createVNode(_component_el_icon, { class: "mr-1" }, {
                  default: withCtx(() => [
                    createVNode(_component_Pointer)
                  ]),
                  _: 1
                }),
                _cache[7] || (_cache[7] = createTextVNode(" 图上拾取 ", -1))
              ]),
              _: 1
            }),
            createVNode(_component_el_button, {
              type: "primary",
              class: "action-btn locate-btn",
              onClick: locateOnMap
            }, {
              default: withCtx(() => [
                createVNode(_component_el_icon, { class: "mr-1" }, {
                  default: withCtx(() => [
                    createVNode(unref(Aim))
                  ]),
                  _: 1
                }),
                _cache[8] || (_cache[8] = createTextVNode(" 坐标定位 ", -1))
              ]),
              _: 1
            })
          ])
        ]),
        default: withCtx(() => [
          createElementVNode("div", _hoisted_1, [
            createElementVNode("div", _hoisted_2, [
              createVNode(_component_el_radio_group, {
                modelValue: selectedFormat.value,
                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => selectedFormat.value = $event),
                size: "default",
                fill: "var(--el-color-primary)",
                onChange: onFormatChange
              }, {
                default: withCtx(() => [
                  createVNode(_component_el_radio_button, { value: "decimal" }, {
                    default: withCtx(() => [..._cache[4] || (_cache[4] = [
                      createTextVNode("十进制", -1)
                    ])]),
                    _: 1
                  }),
                  createVNode(_component_el_radio_button, { value: "dms" }, {
                    default: withCtx(() => [..._cache[5] || (_cache[5] = [
                      createTextVNode("度分秒", -1)
                    ])]),
                    _: 1
                  })
                ]),
                _: 1
              }, 8, ["modelValue"])
            ]),
            createElementVNode("div", _hoisted_3, [
              createVNode(_component_el_form, {
                model: coordinateForm,
                "label-position": "top",
                size: "large"
              }, {
                default: withCtx(() => [
                  createVNode(_component_el_form_item, { label: "经度" }, {
                    default: withCtx(() => [
                      createVNode(_component_el_input, {
                        modelValue: coordinateForm.longitude,
                        "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => coordinateForm.longitude = $event),
                        placeholder: "请输入经度",
                        type: selectedFormat.value === "decimal" ? "number" : "text",
                        step: selectedFormat.value === "decimal" ? "0.000001" : void 0
                      }, {
                        prefix: withCtx(() => [
                          createVNode(_component_el_icon, null, {
                            default: withCtx(() => [
                              createVNode(_component_LocationInformation)
                            ]),
                            _: 1
                          })
                        ]),
                        _: 1
                      }, 8, ["modelValue", "type", "step"])
                    ]),
                    _: 1
                  }),
                  createVNode(_component_el_form_item, { label: "纬度" }, {
                    default: withCtx(() => [
                      createVNode(_component_el_input, {
                        modelValue: coordinateForm.latitude,
                        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => coordinateForm.latitude = $event),
                        placeholder: "请输入纬度",
                        type: selectedFormat.value === "decimal" ? "number" : "text",
                        step: selectedFormat.value === "decimal" ? "0.000001" : void 0
                      }, {
                        prefix: withCtx(() => [
                          createVNode(_component_el_icon, null, {
                            default: withCtx(() => [
                              createVNode(_component_LocationInformation)
                            ]),
                            _: 1
                          })
                        ]),
                        _: 1
                      }, 8, ["modelValue", "type", "step"])
                    ]),
                    _: 1
                  })
                ]),
                _: 1
              }, 8, ["model"])
            ]),
            createElementVNode("div", _hoisted_4, [
              createVNode(_component_el_icon, null, {
                default: withCtx(() => [
                  createVNode(_component_InfoFilled)
                ]),
                _: 1
              }),
              _cache[6] || (_cache[6] = createElementVNode("span", null, "支持手动输入坐标或在地图上点击拾取", -1))
            ])
          ])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
});
const CoordinateLocationDialog = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-60184fa9"]]);
export {
  CoordinateLocationDialog as default
};
