import axiosInstance from './axiosConfig';

/**
 * 获取地图编辑者列表
 * @returns {Promise<any>} 返回一个包含地图编辑者信息的Promise对象
 */
export const getMapEditors = async () => {
    try {
        const response = await axiosInstance.get('/map/editors');
        return response.data;
    } catch (error) {
        throw error;
    }
};

/**
 * 用户提交地图评分
 * @param {string} mapName - 地图名称
 * @param {number} reward - 评分(1~5)
 * @returns {Promise<any>} 返回提交评分的响应数据
 */
export const submitMapReward = async (mapName, reward) => {
    try {
        const payload = {
            mapname: mapName,
            reward: reward
        };
        const response = await axiosInstance.post('/user/map/reward', payload);
        return response.data;
    } catch (error) {
        console.error('提交地图评分失败:', error);
        throw error;
    }
};

/**
 * 获取用户地图信息
 * @param {string} mapName - 地图名称
 * @returns {Promise<any>} 返回用户地图信息的Promise对象
 */
export const getUserMapInfo = async (mapName) => {
    try {
        const response = await axiosInstance.get(`/user/map/${mapName}`);
        return response.data;
    } catch (error) {
        console.error('获取用户地图信息失败:', error);
        throw error;
    }
};

/**
 * 更新用户下载地图信息
 * @param {string} mapName - 地图名称
 * @returns {Promise<any>} 返回更新下载信息的响应数据
 */
export const updateUserDownloadMapInfo = async (mapName) => {
    try {
        const response = await axiosInstance.post(`/map/download/${mapName}`);
        return response.data;
    } catch (error) {
        console.error('更新用户下载地图信息失败:', error);
        throw error;
    }
};

/**
 * 获取指定地图评分
 * @param {string} mapName - 地图名称
 * @returns {Promise<any>} 返回一个包含指定地图评分信息的Promise对象
 */
export const getAppointMapRating = async (mapName) => {
    try {
        const response = await axiosInstance.get(`/map/reward/${mapName}`);
        return response.data;
    } catch (error) {
        console.error('获取指定地图评分失败:', error);
        throw error;
    }
};

/**
 * 获取所有地图的评分
 * @returns {Promise<any>} 返回一个包含所有地图评分信息的Promise对象
 */
export const getAllMapRating = async () => {
    try {
        const response = await axiosInstance.get(`/map/reward`);
        return response.data;
    } catch (error) {
        console.error('获取所有地图评分失败:', error);
        throw error;
    }
};

/**
 * 依据条件搜索地图
 * @param {Object} params - 查询参数对象，键为参数名，值为参数值
 * @returns {Promise<any>} 返回一个包含地图信息的Promise对象
 */
export const searchMapsByConditions = async (params) => {
    try {
        // 构建查询字符串
        const query = new URLSearchParams(params).toString();
        const response = await axiosInstance.get(`/map/search?${query}`);
        return response.data;
    } catch (error) {
        console.error('依据条件搜索地图失败:', error);
        throw error;
    }
};

