/**
 * @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 page-break/pagebreakcommand
 */

import { Command } from '@ckeditor/ckeditor5-core';
import { findOptimalInsertionRange } from '@ckeditor/ckeditor5-widget';
import type { ModelDocumentSelection, ModelElement, Model, ModelSchema } from '@ckeditor/ckeditor5-engine';

/**
 * The page break command.
 *
 * The command is registered by {@link module:page-break/pagebreakediting~PageBreakEditing} as `'pageBreak'`.
 *
 * To insert a page break at the current selection, execute the command:
 *
 *		editor.execute( 'pageBreak' );
 */
export class PageBreakCommand extends Command {
	/**
	 * @inheritDoc
	 */
	public override refresh(): void {
		const model = this.editor.model;
		const schema = model.schema;
		const selection = model.document.selection;

		this.isEnabled = isPageBreakAllowedInParent( selection, schema, model );
	}

	/**
	 * Executes the command.
	 *
	 * @fires execute
	 */
	public override execute(): void {
		const model = this.editor.model;

		model.change( writer => {
			const pageBreakElement = writer.createElement( 'pageBreak' );

			model.insertObject( pageBreakElement, null, null, {
				setSelection: 'after'
			} );
		} );
	}
}

/**
 * Checks if a page break is allowed by the schema in the optimal insertion parent.
 */
function isPageBreakAllowedInParent( selection: ModelDocumentSelection, schema: ModelSchema, model: Model ): boolean {
	const parent = getInsertPageBreakParent( selection, model );

	return schema.checkChild( parent, 'pageBreak' );
}

/**
 * Returns a node that will be used to insert a page break with `model.insertContent` to check if the page break can be placed there.
 */
function getInsertPageBreakParent( selection: ModelDocumentSelection, model: Model ): ModelElement {
	const insertionRange = findOptimalInsertionRange( selection, model );
	const parent = insertionRange.start.parent;

	if ( parent.isEmpty && !parent.is( 'element', '$root' ) ) {
		return parent.parent as ModelElement;
	}

	return parent as ModelElement;
}
