pages/ValuePartnership/PartnershipsPage/qrCodeLink.jsjavascript
/**
 * Normalizes the `webLink` field found on partnership/ppt/filter-option
 * entries (value-partnership-partnerships.json) into a URL the QR code can
 * encode. `webLink` is stored inconsistently as either a plain string or a
 * one-element array, and is sometimes an empty string when no link exists.
 *
 * @param {string|string[]} webLink
 * @returns {string|null} The resolved URL, or `null` if none is set.
 */
export const resolveWebLink = (webLink) => {
  const value = Array.isArray(webLink) ? webLink[0] : webLink;
  return value || null;
};

/**
 * Builds the "save to conference bag" deep link encoded into the collect QR
 * code, ported from value-slideview(-filter).js's concateQrCodeLink/showQRCode
 * (the old `/favorites` endpoint is now `/bookmarks`). Each collected item is
 * encoded as "type;link;event;label;assetType" (content.labels.type/assetType
 * are fixed per-deployment constants), joined with "~" for multiple items.
 *
 * @param {Object} labels - `content.valuePartnership.labels`.
 * @param {Array<{label: string, link: string|string[]}>} items - Collected items to encode.
 * @returns {string} The full bookmarks URL. The event is the current URL's `event` query param if set, else `labels.event`, else "Congress App".
 */
export const buildBookmarksUrl = (labels, items) => {
  const event =
    new URL(window.location.href).searchParams.get("event") || labels.event || "Congress App";
  const encoded = items
    .map(({ label, link }) =>
      encodeURIComponent(
        `${labels.type};${resolveWebLink(link)};${event};${label};${labels.assetType}`
      )
    )
    .join("~");
  return `${labels.webLink}bookmarks?${labels.action}${encoded}&${labels.stc}`;
};