import { guid } from '../../utils/CommonUtils';
/**
 * 管理地图对象
 * @module OpenLayersManager
 * @category map
 */
let mainMapId;
const maps = new Map();
const layers = new Map();
/**
 * @function OpenLayersManager.addMap
 * @description 向MapManager内加入地图对象，第一个加入的地图将视为主地图对象
 * @param {OpenLayersMapType} map - 地图对象
 * @param {string | number} mapId - 地图对象标识ID
 * @return {OpenLayersMapType} map
 */
const addMap = (map, mapId) => {
    const id = mapId || guid();
    map.uid = id;
    map.instanceType = 'OpenLayers';
    if (!mainMapId) {
        mainMapId = id;
    }
    if (maps.has(id)) {
        return {
            map: maps.get(id),
            message: `当前已经存在标识为${id}的map对象`
        };
    }
    else {
        maps.set(id, map);
        return {
            map,
            message: 'map对象添加成功'
        };
    }
};
/**
 * @function OpenLayersManager.removeMap
 * @description 从MapManager中移除地图对象
 * @param {OpenLayersMapType | string | number} map - 地图对象或地图id，为空时删除主地图对象
 * @return {string} message - 提示信息
 */
const removeMap = (mapOrId) => {
    let id;
    if (mapOrId) {
        if (typeof mapOrId === 'object') {
            id = mapOrId.uid;
        }
        else {
            id = mapOrId;
        }
    }
    else {
        id = mainMapId;
    }
    if (id === mainMapId) {
        mainMapId = undefined;
    }
    if (maps.has(id)) {
        maps.delete(id);
        return 'map对象删除成功';
    }
    else {
        return '不存在当前map对象';
    }
};
/**
 * @function OpenLayersManager.getMap
 * @description 获取地图对象
 * @param {string|number|null} mapId - 地图id，为空时获取第一个加入MapManager的主地图对象
 * @return {OpenLayersMapType} map
 */
const getMap = (mapId) => {
    if (!mapId) {
        mapId = mainMapId;
    }
    const map = maps.get(mapId);
    return map;
};
/**
 * @function OpenLayersManager.getAllMap
 * @description 获取所有地图对象
 * @return {OpenLayersMapType[]}
 */
const getAllMap = () => {
    const arr = [];
    maps.forEach((map) => {
        arr.push(map);
    });
    return arr;
};
/**
 * @function OpenLayersManager.hasMap
 * @description 判断是否存在地图对象
 * @param {string|number} id - 图层id
 * @return {boolean} 是否存在当前id的Map
 */
const hasMap = (id) => {
    return maps.has(id);
};
/**
 * @function OpenLayersManager.setLayer
 * @description 存储图层对象
 * @param {string|number} id - 图层id
 * @param {layer} layer - 图层对象
 */
const setLayer = (id, layer) => {
    return layers.set(id, layer);
};
/**
 * @function OpenLayersManager.getLayer
 * @description 获取图层对象
 * @param {string|number} id - 图层id
 * @return {object} layer - 图层对象
 */
const getLayer = (id) => {
    return layers.get(id);
};
/**
 * @function OpenLayersManager.hasLayer
 * @description 判断是否存在图层对象
 * @param {string|number} id - 图层id
 * @return {boolean} 是否存在当前id的图层
 */
const hasLayer = (id) => {
    return layers.has(id);
};
/**
 * @function OpenLayersManager.deleteLayer
 * @description 删除图层对象
 * @param {string|number} id - 图层id
 * @return {boolean} 是否删除
 */
const deleteLayer = (id) => {
    return layers.delete(id);
};
export { addMap, getMap, getAllMap, removeMap, hasMap, setLayer, getLayer, hasLayer, deleteLayer };
