/**
 * Scroll the Blockora Morph inspector sidebar back to the top.
 * Works for the frontend shell and the Gutenberg complementary area.
 */
export function scrollInspectorToTop() {
	if ( typeof document === 'undefined' ) {
		return;
	}

	const feBody = document.querySelector( '.bm-fe-shell__body' );
	if ( feBody instanceof HTMLElement ) {
		feBody.scrollTop = 0;
		return;
	}

	const tabs = document.querySelector( '.bm-gutenberg-inspector-tabs' );
	if ( ! ( tabs instanceof HTMLElement ) ) {
		return;
	}

	let node = tabs;
	while ( node instanceof HTMLElement ) {
		const style = window.getComputedStyle( node );
		const overflowY = style.overflowY;
		if (
			( overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'overlay' ) &&
			node.scrollHeight > node.clientHeight + 1
		) {
			node.scrollTop = 0;
			return;
		}
		node = node.parentElement;
	}

	const sidebar = document.querySelector(
		'.interface-complementary-area, .editor-sidebar, .block-editor-block-inspector'
	);
	if ( sidebar instanceof HTMLElement ) {
		sidebar.scrollTop = 0;
	}
}
