services/tracking/area.jsjavascript
import { resolveModeForPath } from "../presentationModes";

export const APP_SELECTION_AREA = "app-selection";

/**
 * The tracking `area` of a route: the presentation mode's route segment
 * ("value-partnerships", "value-advisory-services", "radenablement-services"),
 * or "app-selection" for "/". Deliberately the route segment, not the mode's
 * `key` - the keys ("value-partnership", "rad-enablement") differ from the
 * routes, and two spellings of the same area in the data read as typos.
 *
 * @param {string} pathname router pathname, e.g. "/radenablement-services/cancer-2"
 * @returns {string}
 */
export const areaForPath = (pathname) =>
  resolveModeForPath(pathname)?.path.slice(1) ?? APP_SELECTION_AREA;

/**
 * The area of the current URL, for code outside the router (HashRouter: the
 * route lives in the hash, "#/value-partnerships/globe?stageDevice=ipad").
 *
 * @returns {string}
 */
export const currentArea = () =>
  areaForPath(window.location.hash.replace(/^#/, "").split("?")[0] || "/");