/** @license MIT | @cube-dev/ui-kit v0.147.0 | Cube Dev Team */
//#region src/components/layout/Board/grid-core/calculate.ts
/** Calculate the width of a single grid column in pixels. */
function calcGridColWidth(positionParams) {
	const { margin, containerPadding, containerWidth, cols } = positionParams;
	return (containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols;
}
/**
* Calculate the pixel size for a grid unit dimension (width or height).
*/
function calcGridItemWHPx(gridUnits, colOrRowSize, marginPx) {
	if (!Number.isFinite(gridUnits)) return gridUnits;
	return Math.round(colOrRowSize * gridUnits + Math.max(0, gridUnits - 1) * marginPx);
}
/**
* Calculate pixel position for a grid item. Returns left, top, width, height.
*/
function calcGridItemPosition(positionParams, x, y, w, h, dragPosition, resizePosition) {
	const { margin, containerPadding, rowHeight } = positionParams;
	const colWidth = calcGridColWidth(positionParams);
	let width;
	let height;
	let top;
	let left;
	if (resizePosition) {
		width = Math.round(resizePosition.width);
		height = Math.round(resizePosition.height);
	} else {
		width = calcGridItemWHPx(w, colWidth, margin[0]);
		height = calcGridItemWHPx(h, rowHeight, margin[1]);
	}
	if (dragPosition) {
		top = Math.round(dragPosition.top);
		left = Math.round(dragPosition.left);
	} else if (resizePosition) {
		top = Math.round(resizePosition.top);
		left = Math.round(resizePosition.left);
	} else {
		top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]);
		left = Math.round((colWidth + margin[0]) * x + containerPadding[0]);
	}
	if (!dragPosition && !resizePosition) {
		if (Number.isFinite(w)) {
			const actualMarginRight = Math.round((colWidth + margin[0]) * (x + w) + containerPadding[0]) - left - width;
			if (actualMarginRight !== margin[0]) width += actualMarginRight - margin[0];
		}
		if (Number.isFinite(h)) {
			const actualMarginBottom = Math.round((rowHeight + margin[1]) * (y + h) + containerPadding[1]) - top - height;
			if (actualMarginBottom !== margin[1]) height += actualMarginBottom - margin[1];
		}
	}
	return {
		top,
		left,
		width,
		height
	};
}
/** Translate pixel coordinates to grid units without clamping. */
function calcXYRaw(positionParams, top, left) {
	const { margin, containerPadding, rowHeight } = positionParams;
	const colWidth = calcGridColWidth(positionParams);
	return {
		x: Math.round((left - containerPadding[0]) / (colWidth + margin[0])),
		y: Math.round((top - containerPadding[1]) / (rowHeight + margin[1]))
	};
}
/** Calculate grid units from pixel dimensions (clamped by handle direction). */
function calcWH(positionParams, width, height, x, y, handle) {
	const { margin, maxRows, cols, rowHeight } = positionParams;
	const colWidth = calcGridColWidth(positionParams);
	const w = Math.round((width + margin[0]) / (colWidth + margin[0]));
	const h = Math.round((height + margin[1]) / (rowHeight + margin[1]));
	let _w = clamp(w, 0, cols - x);
	let _h = clamp(h, 0, maxRows - y);
	if (handle === "sw" || handle === "w" || handle === "nw") _w = clamp(w, 0, cols);
	if (handle === "nw" || handle === "n" || handle === "ne") _h = clamp(h, 0, maxRows);
	return {
		w: _w,
		h: _h
	};
}
/** Clamp a number between bounds. */
function clamp(num, lowerBound, upperBound) {
	return Math.max(Math.min(num, upperBound), lowerBound);
}

//#endregion
export { calcGridColWidth, calcGridItemPosition, calcWH, calcXYRaw };
//# sourceMappingURL=calculate.js.map