src/vs/base/common/strings.ts/getOffsetBeforeLastEmojiComponent
function getOffsetBeforeLastEmojiComponent(offset: number, str: string): number | undefined {
	// See https://www.unicode.org/reports/tr51/tr51-14.html#EBNF_and_Regex for the
	// structure of emojis.
	let codePoint = getPrevCodePoint(str, offset);
	offset -= getUTF16Length(codePoint);

	// Skip modifiers
	while ((isEmojiModifier(codePoint) || codePoint === CodePoint.emojiVariantSelector || codePoint === CodePoint.enclosingKeyCap)) {
		if (offset === 0) {
			// Cannot skip modifier, no preceding emoji base.
			return undefined;
		}
		codePoint = getPrevCodePoint(str, offset);
		offset -= getUTF16Length(codePoint);
	}

	// Expect base emoji
	if (!isEmojiImprecise(codePoint)) {
		// Unexpected code point, not a valid emoji.
		return undefined;
	}

	if (offset >= 0) {
		// Skip optional ZWJ code points that combine multiple emojis.
		// In theory, we should check if that ZWJ actually combines multiple emojis
		// to prevent deleting ZWJs in situations we didn't account for.
		const optionalZwjCodePoint = getPrevCodePoint(str, offset);
		if (optionalZwjCodePoint === CodePoint.zwj) {
			offset -= getUTF16Length(optionalZwjCodePoint);
		}
	}

	return offset;
}