Source: src/js/models/maps/featureIdHelpers.js

"use strict";

/**
 * Pure helpers for extracting stable identifiers from feature property objects.
 * Used by map asset models (CesiumVectorData, Cesium3DTileset) to encode feature
 * IDs in the URL and restore them on page load.
 * @module featureIdHelpers
 * @classcategory Models/Maps
 * @since 2.40.0
 */
define([], () => {
  /** Property keys checked in priority order when deriving a stable feature ID. */
  const FEATURE_ID_KEYS = [
    "id",
    "identifier",
    "uuid",
    "name",
    "title",
    "label",
  ];

  /**
   * Convert all keys in an object to lowercase.
   * @param {object} props The object to convert.
   * @returns {object} copy with all keys lowercased
   */
  function toLowerCaseProps(props) {
    const lower = {};
    Object.keys(props || {}).forEach((k) => {
      lower[k.toLowerCase()] = props[k];
    });
    return lower;
  }

  /**
   * Return the first non-empty string value found under a FEATURE_ID_KEYS key.
   * @param {object} props Feature properties (any key casing).
   * @returns {string|null} The feature ID, or null if none is found.
   */
  function getIdFromProperties(props) {
    const lower = toLowerCaseProps(props);
    const key = FEATURE_ID_KEYS.find((k) => {
      const val = lower[k];
      return val != null && String(val).trim().length > 0;
    });
    return key != null ? String(lower[key]).trim() : null;
  }

  /**
   * Return true if props resolves to the same canonical ID as id.
   * @param {object} props Feature properties (any key casing).
   * @param {string} id The feature ID to match.
   * @returns {boolean} True if a match is found, false otherwise.
   */
  function propertyMatchesId(props, id) {
    const normalizedId = id != null ? String(id).trim() : "";
    if (!normalizedId) {
      return false;
    }

    return getIdFromProperties(props) === normalizedId;
  }

  return {
    FEATURE_ID_KEYS,
    getIdFromProperties,
    propertyMatchesId,
  };
});