/**
 * 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.
 */

const require_runtime = require('../../../_virtual/_rolldown/runtime.js');
const require_settings = require('../../../settings.js');
const require_moveColumnIndicatorLine = require('../utils/moveColumnIndicatorLine.js');
let react = require("react");
let _carbon_layout = require("@carbon/layout");

//#region src/components/DataSpreadsheet/hooks/useSpreadsheetMouseMove.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 useSpreadsheetMouseMove = ({ ref, blockClass = `${require_settings.pkg.prefix}--data-spreadsheet`, headerCellHoldActive, defaultColumn }) => {
	(0, react.useEffect)(() => {
		const handleMouseMove = (event) => {
			const clonedSelectionElement = ref.current.querySelector(`.${blockClass}__selection-area--element-cloned`);
			if (clonedSelectionElement) ref.current.addEventListener("mousemove", handleMouseMove);
			const spreadsheetCoords = ref.current.getBoundingClientRect();
			const listContainer = ref.current.querySelector(`.${blockClass}__list--container`);
			const scrollAmount = listContainer.scrollLeft;
			require_moveColumnIndicatorLine.moveColumnIndicatorLine({
				clonedSelectionElement,
				ref,
				spreadsheetCoords,
				leftScrollAmount: scrollAmount
			});
			ref.current.getBoundingClientRect();
			const xPositionRelativeToSpreadsheet = event.clientX - spreadsheetCoords.left;
			const offsetXValue = clonedSelectionElement?.getAttribute("data-clone-offset-x");
			const totalSpreadsheetScrollingWidth = listContainer.scrollWidth;
			const clonedSelectionWidth = clonedSelectionElement.offsetWidth;
			const clonePlacement = Math.max(xPositionRelativeToSpreadsheet - offsetXValue, defaultColumn?.rowHeaderWidth);
			const leftPosition = totalSpreadsheetScrollingWidth - clonedSelectionWidth >= clonePlacement ? clonePlacement + scrollAmount : totalSpreadsheetScrollingWidth - clonedSelectionWidth;
			if (leftPosition < spreadsheetCoords.right - 40) clonedSelectionElement.style.left = (0, _carbon_layout.px)(leftPosition);
		};
		if (headerCellHoldActive) ref.current.addEventListener("mousemove", handleMouseMove);
		const spreadsheetRef = ref.current;
		if (!headerCellHoldActive) spreadsheetRef?.removeEventListener("mousemove", handleMouseMove);
		return () => {
			spreadsheetRef?.removeEventListener("mousemove", handleMouseMove);
		};
	});
};

//#endregion
exports.useSpreadsheetMouseMove = useSpreadsheetMouseMove;