function decodeTcStringCoreMetadata(tcString) {
  if (!tcString || typeof tcString !== "string") {
    return null;
  }

  const coreSegment = tcString.split(".")[0];

  try {
    const bits = base64UrlToBits(coreSegment);

    return {
      version: bitsToInt(bits, 0, 6),
      cmpId: bitsToInt(bits, 78, 12),
      cmpVersion: bitsToInt(bits, 90, 12),
      consentScreen: bitsToInt(bits, 102, 6),
      consentLanguage: bitsToString(bits, 108, 12),
      vendorListVersion: bitsToInt(bits, 120, 12),
      tcfPolicyVersion: bitsToInt(bits, 132, 6),
      isServiceSpecific: bitsToBoolean(bits, 138),
      useNonStandardStacks: bitsToBoolean(bits, 139),
      purposeOneTreatment: bitsToBoolean(bits, 200),
      publisherCC: bitsToString(bits, 201, 12)
    };
  } catch (error) {
    console.warn("VG-Observe could not decode TC string core metadata", error);
    return null;
  }
}

function bitsToString(bits, start, length) {
  let result = "";

  for (let index = start; index < start + length; index += 6) {
    const charCode = bitsToInt(bits, index, 6) + 65;
    result += String.fromCharCode(charCode);
  }

  return result;
}
