/**
 * 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_deepCloneObject = require('../../../global/js/utils/deepCloneObject.js');
const require_getSelectionAreaPoints = require('./getSelectionAreaPoints.js');
let _carbon_layout = require("@carbon/layout");

//#region src/components/DataSpreadsheet/utils/createCellSelectionArea.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 createCellSelectionArea = ({ ref, area, blockClass, defaultColumn, selectionAreas, setSelectionAreas, setActiveCellInsideSelectionArea, visibleColumns }) => {
	const { lowestColumnIndex, lowestRowIndex, greatestColumnIndex, greatestRowIndex } = require_getSelectionAreaPoints.getSelectionAreaPoints(area);
	if (greatestRowIndex - lowestRowIndex > 0 || greatestColumnIndex - lowestColumnIndex > 0) {
		setActiveCellInsideSelectionArea(true);
		ref.current.querySelector(`.${blockClass}__active-cell--highlight`).setAttribute("data-selection-id", area.matcher);
	}
	let selectionAreaVariableWidth = 0;
	visibleColumns.forEach((item, index) => {
		if (index >= lowestColumnIndex && index <= greatestColumnIndex) selectionAreaVariableWidth += item?.width || defaultColumn?.width;
	});
	const spreadsheetSelector = ref.current ?? document;
	const selectionAreaCellHeight = (spreadsheetSelector.querySelector(`[data-row-index="${area.point1.row}"][data-column-index="${area.point1.column}"]`) || spreadsheetSelector.querySelector(`.${blockClass}__body--td`)).offsetHeight;
	const selectionAreaTotalHeight = selectionAreaCellHeight * (greatestRowIndex - lowestRowIndex + 1);
	const bodyContainer = spreadsheetSelector.querySelector(`.${blockClass}__list--container`).firstElementChild;
	const placementElement = bodyContainer.querySelector(`[data-row-index="${lowestRowIndex}"][data-column-index="${lowestColumnIndex}"]`);
	const relativePosition = {
		top: placementElement ? placementElement.getBoundingClientRect().top - bodyContainer.getBoundingClientRect().top : lowestRowIndex === 0 ? 0 : selectionAreaCellHeight * lowestRowIndex,
		left: placementElement ? placementElement.getBoundingClientRect().left - bodyContainer.getBoundingClientRect().left : lowestColumnIndex === 0 ? 0 + (defaultColumn.rowHeaderWidth - 4) : defaultColumn.width * lowestColumnIndex + (defaultColumn.rowHeaderWidth - 4)
	};
	const selectionAreaElement = (ref ? ref.current.querySelector(`[data-matcher-id="${area.matcher}"]`) : document.querySelector(`[data-matcher-id="${area.matcher}"]`)) || document.createElement("div");
	selectionAreaElement.classList.add(`${blockClass}__selection-area--element`);
	selectionAreaElement.setAttribute("data-matcher-id", area.matcher);
	selectionAreaElement.style.width = (0, _carbon_layout.px)(selectionAreaVariableWidth);
	selectionAreaElement.style.height = (0, _carbon_layout.px)(selectionAreaTotalHeight);
	selectionAreaElement.style.left = (0, _carbon_layout.px)(relativePosition.left);
	selectionAreaElement.style.top = (0, _carbon_layout.px)(relativePosition.top);
	bodyContainer.appendChild(selectionAreaElement);
	const selectionAreasClone = require_deepCloneObject.deepCloneObject(selectionAreas);
	const indexOfCurrentArea = selectionAreasClone.findIndex((item) => item.matcher === area.matcher);
	selectionAreasClone[indexOfCurrentArea].areaCreated = true;
	setSelectionAreas(selectionAreasClone);
};

//#endregion
exports.createCellSelectionArea = createCellSelectionArea;