/**
 * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
 */

/**
 * @module paste-from-office/filters/bookmark
 */

import {
	type ViewUpcastWriter,
	type ViewDocumentFragment
} from '@ckeditor/ckeditor5-engine';

/**
 * Transforms `<a>` elements which are bookmarks by moving their children after the element.
 *
 * @internal
 */
export function transformBookmarks(
	documentFragment: ViewDocumentFragment,
	writer: ViewUpcastWriter
): void {
	const elementsToChange = [];

	for ( const value of writer.createRangeIn( documentFragment ) ) {
		const element = value.item;

		if (
			element.is( 'element', 'a' ) &&
			!element.hasAttribute( 'href' ) &&
			( element.hasAttribute( 'id' ) || element.hasAttribute( 'name' ) ) )
		{
			elementsToChange.push( element );
		}
	}

	for ( const element of elementsToChange ) {
		const index = element.parent!.getChildIndex( element ) + 1;
		const children = element.getChildren();

		writer.insertChild( index, children, element.parent! );
	}
}
