export function useRuler() {
  let ruler;
  let isRanging = ref(false);
  const initRuler = (map, AMap) => {
    // 自定义测距工具样式
    const startMarkerOptions = {
      icon: new AMap.Icon({
        size: new AMap.Size(19, 31), // 图标大小
        imageSize: new AMap.Size(19, 31),
        image: '//webapi.amap.com/theme/v1.3/markers/b/start.png'
      }),
      offset: new AMap.Pixel(-9, -31)
    };
    const endMarkerOptions = {
      icon: new AMap.Icon({
        size: new AMap.Size(19, 31), // 图标大小
        imageSize: new AMap.Size(19, 31),
        image: '//webapi.amap.com/theme/v1.3/markers/b/end.png'
      }),
      offset: new AMap.Pixel(-9, -31)
    };
    const midMarkerOptions = {
      icon: new AMap.Icon({
        size: new AMap.Size(19, 31), // 图标大小
        imageSize: new AMap.Size(19, 31),
        image: '//webapi.amap.com/theme/v1.3/markers/b/mid.png'
      }),
      offset: new AMap.Pixel(-9, -31)
    };
    const lineOptions = {
      strokeStyle: 'solid',
      strokeColor: '#FF33FF',
      strokeOpacity: 1,
      strokeWeight: 2
    };
    const rulerOptions = {
      startMarkerOptions: startMarkerOptions,
      midMarkerOptions: midMarkerOptions,
      endMarkerOptions: endMarkerOptions,
      lineOptions: lineOptions
    };

    // 初始化自定义样式测距工具
    ruler = new AMap.RangingTool(map, rulerOptions);
  };

  // 切换测距工具状态
  const toggleRangingTool = () => {
    if (ruler) {
      if (isRanging.value) {
        ruler.turnOff(); // 关闭测距工具
      } else {
        ruler.turnOn(); // 启动测距工具
      }
      isRanging.value = !isRanging.value; // 切换状态
    }
  };
  return {
    initRuler,
    isRanging,
    toggleRangingTool
  };
}
