import type { LocationType } from "../wx/location";
import { getFuzzyLocation } from "../wx/location";
import request from "../request/method";


export async function useLocation(type?: LocationType) {
  const scope = 'scope.userFuzzyLocation';
  const { authSetting } = await wx.getSetting();
  if (!authSetting?.[scope]) {
    await wx.authorize({ scope })
  }

  const location = await getFuzzyLocation(type);

  const {prov, city} = await request({
    url: `/surplus/getAddrByLatLong`,
    method: 'GET',
    params: {
      longitude: location.longitude,
      latitude: location.latitude,
      type,
    },
    transform({ data }) { return data; }
  });
  

  return {
    longitude: location.longitude,
    latitude: location.latitude,
    description: `${prov}${city}`.replace(/省|市|自治区|特别行政区|壮族|回族|维吾尔/g,''),
  }
}