import { getRequest } from "@/api/customFetch";
import { getLocationByReverseGeography } from "@/api/interfaceDocument";
import { mapKey } from "@/utils/useMap";
import { useUserStore } from "@/store/user";
import { Address } from "@/types/tdt";

export const useLocationName = (longitude: string, latitude: string) => {
  const userStore = useUserStore();
  userStore.setCoordinate(longitude, latitude);
  userStore.setDeviceCoordinate(longitude, latitude);
  getRequest(getLocationByReverseGeography(), {
    postStr: encodeURIComponent(JSON.stringify({ lon: longitude, lat: latitude, ver: 1 })),
    type: "geocode",
    tk: mapKey,
  }).then((resp) => {
    let _result = resp.result as {
      addressComponent:Address;
    };

    const address = _result.addressComponent.town + _result.addressComponent.road;
    userStore.setAddress(address);
    userStore.setDeviceAddress({ ..._result.addressComponent });
  });
};
