/** @license MIT | @cube-dev/ui-kit v0.147.0 | Cube Dev Team */
import { cloneLayout, correctBounds } from "./grid-core/layout.js";

//#region src/components/layout/Board/responsive-utils.ts
/**
* Responsive helpers for `Board.Responsive`.
*
* Framework-agnostic breakpoint math adapted (MIT) from react-grid-layout's
* `responsiveUtils`, rewritten to use this package's grid-core primitives. See
* ./grid-core/NOTICE.md for the upstream attribution and license.
*/
/** Breakpoint names sorted ascending by their minimum width. */
function sortBreakpoints(breakpoints) {
	return Object.keys(breakpoints).sort((a, b) => breakpoints[a] - breakpoints[b]);
}
/**
* The largest breakpoint whose minimum width is at or below `width`. Falls back
* to the smallest breakpoint when `width` is narrower than all of them.
*/
function getBreakpointFromWidth(breakpoints, width) {
	const sorted = sortBreakpoints(breakpoints);
	let matching = sorted[0];
	for (let i = 1; i < sorted.length; i++) {
		const name = sorted[i];
		if (width >= breakpoints[name]) matching = name;
	}
	return matching;
}
/** Column count for a breakpoint. */
function getColsFromBreakpoint(breakpoint, cols) {
	const value = cols[breakpoint];
	if (value == null) throw new Error(`Board.Responsive: no columns defined for breakpoint "${breakpoint}".`);
	return value;
}
/**
* The layout for `breakpoint`, or one synthesized from the nearest available
* breakpoint (preferring `lastBreakpoint`, then breakpoints at or above the
* target) corrected into bounds and compacted for the target column count.
*/
function findOrGenerateResponsiveLayout(layouts, breakpoints, breakpoint, lastBreakpoint, cols, compactor) {
	if (layouts[breakpoint]) return cloneLayout(layouts[breakpoint]);
	let layout = layouts[lastBreakpoint];
	const sorted = sortBreakpoints(breakpoints);
	const above = sorted.slice(sorted.indexOf(breakpoint));
	for (const name of above) if (layouts[name]) {
		layout = layouts[name];
		break;
	}
	layout = cloneLayout(layout || []);
	return [...compactor.compact(correctBounds(layout, { cols }), cols)];
}

//#endregion
export { findOrGenerateResponsiveLayout, getBreakpointFromWidth, getColsFromBreakpoint };
//# sourceMappingURL=responsive-utils.js.map