import * as Cesium from 'cesium';

const provider = new Cesium.UrlTemplateImageryProvider({
  maximumLevel: 18,
  minimumLevel: 3,
  url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
});

export function configureMapTile(viewer: Cesium.Viewer) {
  if (viewer.baseLayerPicker) {
    // 如果有底图选择器
    const customLayerViewModel = new Cesium.ProviderViewModel({
      category: 'Cesium ion', // 或 'Other 、Cesium ion'、'Bing Maps' 等
      creationFunction() {
        return provider;
      },
      iconUrl: 'gaodeImage.png',
      name: '高德地图',
      tooltip: '高德地图',
    });
    // 设置高德地图为默认图层
    viewer.baseLayerPicker.viewModel.imageryProviderViewModels.unshift(customLayerViewModel);
    const selectedViewModel = viewer.baseLayerPicker.viewModel.imageryProviderViewModels[0];
    if (!selectedViewModel) {
      console.error('未找到默认底图');
      return;
    }
    viewer.baseLayerPicker.viewModel.selectedImagery = selectedViewModel;
  } else {
    // 如果没有底图选择器
    viewer.imageryLayers.removeAll();
    viewer.imageryLayers.addImageryProvider(provider);
  }
}
