/**
 * @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 list/todolist/todocheckboxchangeobserver
 */

import { DomEventObserver, type ViewDocumentDomEventData } from '@ckeditor/ckeditor5-engine';

/**
 * Observes all to-do list checkboxes state changes.
 *
 * Note that this observer is not available by default. To make it available it needs to be added to
 * {@link module:engine/view/view~EditingView} by {@link module:engine/view/view~EditingView#addObserver} method.
 *
 * @internal
 */
export class TodoCheckboxChangeObserver extends DomEventObserver<'change'> {
	/**
	 * @inheritDoc
	 */
	public readonly domEventType = [ 'change' ] as const;

	/**
	 * @inheritDoc
	 */
	public onDomEvent( domEvent: Event ): void {
		if ( domEvent.target ) {
			const viewTarget = this.view.domConverter.mapDomToView( domEvent.target as HTMLElement );

			if (
				viewTarget &&
				viewTarget.is( 'element', 'input' ) &&
				viewTarget.getAttribute( 'type' ) == 'checkbox' &&
				viewTarget.findAncestor( { classes: 'todo-list__label' } )
			) {
				this.fire( 'todoCheckboxChange', domEvent );
			}
		}
	}
}

/**
 * Fired when the to-do list checkbox is toggled.
 *
 * Introduced by {@link module:list/todolist/todocheckboxchangeobserver~TodoCheckboxChangeObserver}.
 *
 * Note that this event is not available by default. To make it available,
 * {@link module:list/todolist/todocheckboxchangeobserver~TodoCheckboxChangeObserver}
 * needs to be added to {@link module:engine/view/view~EditingView} by the {@link module:engine/view/view~EditingView#addObserver} method.
 *
 * @see module:list/todolist/todocheckboxchangeobserver~TodoCheckboxChangeObserver
 * @eventName module:engine/view/document~ViewDocument#todoCheckboxChange
 * @param data The event data.
 */
export type ViewDocumentTodoCheckboxChangeEvent = {
	name: 'todoCheckboxChange';
	args: [ data: ViewDocumentDomEventData<Event> ];
};
