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

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

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

	let resultOffset = iterator.offset;

	if (resultOffset > 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 = iterator.prevCodePoint();
		if (optionalZwjCodePoint === CodePoint.zwj) {
			resultOffset = iterator.offset;
		}
	}

	return resultOffset;
}