import _default, * as esriLoader from 'esri-loader';

const options = {
  url: '/arcgisApi/4.7/init.js',
};
var mapObject = null;
export function addOneMapLayer(map, item, leftLayersAddedList) {
  mapObject = map;
  esriLoader
    .loadModules(
      ['esri/layers/WebTileLayerForSuperMap', 'dojo/domReady!'],
      options,
    )
    .then(([WebTileLayerForSuperMap]) => {
      if (item.checked == false) {
        var str = item.tileMatrixSet.substring(7);
        var option = {
          id: item.id,
          label: item.name,
          visible: true,
          opacity: 1,
          style: 'default',
          wmtslayer: 'vec',
          tileMatrixSet: item.tileMatrixSet,
          format: 'image/png',
          urlTemplate:
            'http://{subDomain}.1.58.34:80' +
            item.url +
            '&SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=' +
            str +
            '&STYLE=Default&FORMAT=image/png&TILEMATRIXSET=' +
            item.tileMatrixSet +
            '&TILEMATRIX={level}&TILEROW={row}&TILECOL={col}',
          copyright: 'TiandituWMTS',
          subDomains: ['10'],
        };
        var oneMapLayer = new WebTileLayerForSuperMap({
          urlTemplate: option.urlTemplate,
          id: item.id,
          subDomains: ['10'],
        });
        mapObject.add(oneMapLayer);
        if (item.type && item.type != '') {
          leftLayersAddedList.push(item.type);
        }
      } else {
        var lyr = mapObject.findLayerById(item.id);
        if (lyr) {
          mapObject.remove(lyr);
          for (var i = 0; i < leftLayersAddedList.length; i++) {
            if (leftLayersAddedList[i] == item.type) {
              leftLayersAddedList.splice(i, 1);
            }
          }
        }
      }
      item.checked = !item.checked;
    })
    .catch((err) => {
      console.error(err);
    });
}
