/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { q as queryExtent } from "./queryUtils.js";
async function getMapLayerHash(mapView, showOnlyUpdatableLayers) {
  let layerHash;
  await mapView.when(() => {
    layerHash = mapView.map.allLayers.toArray().reduce((prev, cur) => {
      if (cur.type === "feature" || cur?.type === "subtype-group") {
        prev[cur.id] = {
          name: cur.title,
          supportsUpdate: void 0
        };
      }
      return prev;
    }, {});
  });
  return _getFinalHash(showOnlyUpdatableLayers, layerHash, mapView);
}
async function getMapTableHash(mapView, onlyShowUpdatableTables) {
  let tableHash;
  await mapView.when(() => {
    tableHash = mapView.map.allTables.toArray().reduce((prev, cur) => {
      prev[cur.id] = {
        name: cur.title,
        supportsUpdate: void 0
      };
      return prev;
    }, {});
  });
  return _getFinalHash(onlyShowUpdatableTables, tableHash, mapView);
}
async function _getFinalHash(onlyShowUpdatable, hash, mapView) {
  const resultHash = {};
  const keys = Object.keys(hash);
  for (const id of keys) {
    try {
      const layer = await getLayerOrTable(mapView, id);
      await layer.load();
      await layer.when();
      const supportsUpdate = layer.editingEnabled && layer.capabilities?.operations.supportsUpdate;
      if (!onlyShowUpdatable || onlyShowUpdatable && supportsUpdate) {
        resultHash[id] = {
          name: hash[id].name,
          supportsUpdate
        };
      }
    } catch (error) {
      console.log(error);
      continue;
    }
  }
  return resultHash;
}
async function getFeatureLayerView(mapView, id) {
  const layer = await getLayerOrTable(mapView, id);
  return layer ? await mapView.whenLayerView(layer) : void 0;
}
async function getLayerOrTable(mapView, id) {
  let layers = [];
  await mapView?.when(() => {
    layers = [...mapView.map.allLayers.toArray(), ...mapView.map.allTables.toArray()].filter((l) => {
      return l.id === id;
    });
  });
  return layers.length > 0 ? layers[0] : void 0;
}
async function getAllFeatureLayers(mapView) {
  const layers = mapView.map.allLayers.filter((l) => l.type === "feature").toArray().map((l) => l);
  let layerViewPromises = [];
  await mapView.when(() => {
    layerViewPromises = layers.map((l) => mapView.whenLayerView(l));
  });
  await Promise.allSettled(layerViewPromises);
  return layers;
}
async function getAllTables(mapView) {
  const tables = mapView.map.allTables.toArray();
  let layerViewPromises = [];
  await mapView.when(() => {
    layerViewPromises = tables.map((t) => t.load());
  });
  await Promise.allSettled(layerViewPromises);
  return tables;
}
async function highlightFeatures(ids, layerView, mapView, updateExtent = false, zoomToScale = 0) {
  if (updateExtent) {
    await goToSelection(ids, layerView, mapView, false, void 0, zoomToScale);
    await new Promise((resolve) => setTimeout(resolve, 1e3));
  }
  return layerView.highlight(ids);
}
async function highlightAllFeatures(selectionSets) {
  const highlightInfos = getIdSets(selectionSets);
  return Object.keys(highlightInfos).reduce((prev, cur) => {
    const highlightInfo = highlightInfos[cur];
    prev.push(highlightInfo.layerView.highlight(highlightInfo.ids));
    return prev;
  }, []);
}
function getIdSets(selectionSets) {
  return selectionSets.reduce((prev, cur) => {
    const lv = cur.layerView;
    const id = lv?.layer.id;
    if (id && Object.keys(prev).indexOf(id) > -1) {
      prev[id].ids = [.../* @__PURE__ */ new Set([...cur.selectedIds, ...prev[id].ids])];
    } else if (id) {
      prev[id] = {
        layerView: lv,
        ids: cur.selectedIds
      };
    }
    if (cur.workflowType === "REFINE") {
      Object.keys(cur.refineInfos).forEach((k) => {
        const refineInfo = cur.refineInfos[k];
        if (Object.keys(prev).indexOf(k) > -1) {
          prev[k].ids = [.../* @__PURE__ */ new Set([...refineInfo.addIds, ...prev[k].ids])];
          prev[k].ids = prev[k].ids.filter((_id) => refineInfo.removeIds.indexOf(_id) < 0);
        }
      });
    }
    return prev;
  }, {});
}
async function flashSelection(ids, layerView, featureEffect) {
  const filter = {
    objectIds: ids
  };
  layerView.featureEffect = {
    ...featureEffect,
    filter
  };
  setTimeout(() => {
    layerView.featureEffect = void 0;
  }, 1300);
}
async function goToSelection(ids, layerView, mapView, flashFeatures = true, featureEffect, zoomToScale = 0) {
  const result = await queryExtent(ids, layerView.layer);
  const goToParams = { target: result.extent };
  if (result.count === 1 && layerView.layer.geometryType === "point" && zoomToScale) {
    goToParams.scale = zoomToScale;
  }
  await mapView.goTo(goToParams);
  if (flashFeatures) {
    await flashSelection(ids, layerView, featureEffect);
  }
}
export {
  getAllFeatureLayers as a,
  getAllTables as b,
  getFeatureLayerView as c,
  getMapLayerHash as d,
  goToSelection as e,
  getMapTableHash as f,
  getLayerOrTable as g,
  highlightFeatures as h,
  getIdSets as i,
  highlightAllFeatures as j
};
