/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { getSelectionAreaPoints } from "./getSelectionAreaPoints.js";
//#region src/components/DataSpreadsheet/utils/handleActiveCellInSelectionEnter.js
/**
* Copyright IBM Corp. 2022, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const handleActiveCellInSelectionEnter = ({ activeCellInsideSelectionArea, activeCellCoordinates, activeCellRef, selectionAreas, updateActiveCellCoordinates }) => {
	if (!activeCellInsideSelectionArea) return;
	const activeCellSelectionId = activeCellRef.current.getAttribute("data-selection-id");
	const activeCellIndexInSelectionAreas = selectionAreas.findIndex((item) => item.matcher === activeCellSelectionId);
	const selectionAreaToNavigate = selectionAreas[activeCellIndexInSelectionAreas];
	const { lowestColumnIndex, lowestRowIndex, greatestColumnIndex, greatestRowIndex } = getSelectionAreaPoints(selectionAreaToNavigate);
	const coordinatesClone = { ...activeCellCoordinates };
	if (activeCellCoordinates?.row < greatestRowIndex) updateActiveCellCoordinates({
		coords: coordinatesClone,
		updatedValue: { row: coordinatesClone?.row + 1 },
		optOutOfSelectionAreaUpdate: true
	});
	if (activeCellCoordinates?.row === greatestRowIndex) {
		if (activeCellCoordinates?.column < greatestColumnIndex) updateActiveCellCoordinates({
			coords: coordinatesClone,
			updatedValue: {
				column: coordinatesClone?.column + 1,
				row: lowestRowIndex
			},
			optOutOfSelectionAreaUpdate: true
		});
		if (activeCellCoordinates?.column === greatestColumnIndex) {
			if (selectionAreas.length > 1) if (selectionAreas[activeCellIndexInSelectionAreas + 1]) {
				activeCellRef.current.setAttribute("data-selection-id", selectionAreas[activeCellIndexInSelectionAreas + 1].matcher);
				const nextSelectionArea = selectionAreas[activeCellIndexInSelectionAreas + 1];
				const { lowestColumnIndex, lowestRowIndex } = getSelectionAreaPoints(nextSelectionArea);
				updateActiveCellCoordinates({
					coords: coordinatesClone,
					updatedValue: {
						column: lowestColumnIndex,
						row: lowestRowIndex
					},
					optOutOfSelectionAreaUpdate: true
				});
				return;
			} else {
				activeCellRef.current.setAttribute("data-selection-id", selectionAreas[0].matcher);
				const firstSelectionArea = selectionAreas[0];
				const { lowestColumnIndex, lowestRowIndex } = getSelectionAreaPoints(firstSelectionArea);
				updateActiveCellCoordinates({
					coords: coordinatesClone,
					updatedValue: {
						column: lowestColumnIndex,
						row: lowestRowIndex
					},
					optOutOfSelectionAreaUpdate: true
				});
			}
			if (selectionAreas.length === 1) return updateActiveCellCoordinates({
				coords: coordinatesClone,
				updatedValue: {
					column: lowestColumnIndex,
					row: lowestRowIndex
				},
				optOutOfSelectionAreaUpdate: true
			});
		}
	}
};
//#endregion
export { handleActiveCellInSelectionEnter };
