import axios from "axios";

const API_KEY = "d101001e-49f5-4a17-ac39-2d67f64b3f9b";

export const getCoordinates = async (city) => {
  try {
    const response = await axios.get(
      `https://geocode-maps.yandex.ru/1.x/?apikey=${API_KEY}&geocode=${city}&format=json`
    );

    const coordinates =
      response.data?.response?.GeoObjectCollection?.featureMember[0]?.GeoObject
        ?.Point?.pos;

    if (coordinates) {
      const [longitude, latitude] = coordinates.split(" ");
      return { longitude, latitude };
    }

    throw new Error("Координаты не найдены");
  } catch (error) {
    console.error("Ошибка при получении координат:", error);
    throw error;
  }
};
