import { cloneDeep } from 'lodash';
import { Dict, LMConfig } from '@zenview/micro-basic-library';
export const creatOverlayLayer = ({ map, name, isCluster, callback, clusterOpt }) => {
  const { ZenMap } = window;
  const hasLayer = map.getLayerByName(name);
  if (hasLayer) {
    callback && callback(hasLayer);
    return;
  }
  // eslint-disable-next-line no-unused-vars
  let opt = clusterOpt
    ? clusterOpt
    : {
        distance: 100,
        clusterClickModel: 'zoom',
        showClusterText: true,
        textColor: '#FFFFFF',
        textSize: 14,
        labelOffset: [12, 0],
      };
  let layer = new ZenMap.Layers.OverlayLayer(name, isCluster);
  map && map.addLayer(layer);
  callback && callback(layer);
  return layer;
};

export const addImgToMap = ({ map, imgArr, callback }) => {
  map.addImages([...imgArr], () => callback());
};

export const validLatLon = (
  lat,
  lon,
  bounds = [
    [LMConfig.CustomConfig?.ZenMap?.mapOpts.restrictedExtent[1], LMConfig.CustomConfig?.ZenMap?.mapOpts.restrictedExtent[0]],
    [LMConfig.CustomConfig?.ZenMap?.mapOpts.restrictedExtent[3], LMConfig.CustomConfig?.ZenMap?.mapOpts.restrictedExtent[2]],
  ]
) => {
  //坐标是否合法
  return lat >= bounds[0][0] && lat <= bounds[1][0] && lon >= bounds[0][1] && lon <= bounds[1][1];
};

export const typeSwitch = (data) => {
  const { ZenMap } = window;
  if (data && typeof data === 'string') {
    let path = cloneDeep(data);
    let pathArr = [];
    if (!path) {
      return pathArr;
    }
    if (typeof path === 'string') {
      try {
        if (Array.isArray(JSON.parse(data))) {
          pathArr = JSON.parse(data);
        } else {
          pathArr = path.split('|').map((path2) =>
            path2.split(';').map((v) => {
              let arr = v.split(',');
              return new ZenMap.Geometry.Point(arr[0], arr[1]);
            })
          );
        }
      } catch (error) {
        pathArr = path.split('|').map((path2) =>
          path2.split(';').map((v) => {
            let arr = v.split(',');
            return new ZenMap.Geometry.Point(arr[0], arr[1]);
          })
        );
      }
    }
    if (Array.isArray(path)) {
      pathArr = path;
    }
    return pathArr;
  } else {
    let path = cloneDeep(data);
    let pathArr = [];
    if (path && Array.isArray(path)) {
      pathArr = path.map((v) => {
        if (Array.isArray(v)) {
          return new ZenMap.Geometry.Point(v[0], v[1]);
        }
        let arr = v.split(',');
        return new ZenMap.Geometry.Point(arr[0], arr[1]);
      });
    }
    return pathArr;
  }
};
