"use strict";
/**
 * Responsive utility functions for BFUI
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.responsiveStyle = responsiveStyle;
exports.responsiveStyles = responsiveStyles;
exports.getValueForBreakpoint = getValueForBreakpoint;
const react_1 = require("@chakra-ui/react");
/**
 * Creates a responsive styles object from a responsive value
 * @param value The responsive value or object
 * @param property The CSS property to apply
 * @param transform Optional function to transform the value
 * @returns An object with CSS properties for different breakpoints
 */
function responsiveStyle(value, property, transform) {
    const transformFn = transform || ((val) => val);
    // If it's not an object, it's a single value for all breakpoints
    if (typeof value !== 'object' || value === null) {
        return {
            [property]: transformFn(value),
        };
    }
    // Handle responsive object with breakpoints using Chakra theme
    const bps = react_1.theme.breakpoints;
    return Object.entries(value).reduce((acc, [breakpoint, breakpointValue]) => {
        if (breakpoint === 'base') {
            acc[property] = transformFn(breakpointValue);
        }
        else {
            const minWidth = bps[breakpoint] || '0px';
            acc[`@media screen and (min-width: ${minWidth})`] = {
                [property]: transformFn(breakpointValue),
            };
        }
        return acc;
    }, {});
}
/**
 * Creates responsive styles for multiple properties at once
 * @param values Object with responsive values for each property
 * @returns Object with CSS properties for different breakpoints
 */
function responsiveStyles(values) {
    return Object.entries(values).reduce((acc, [property, value]) => {
        const styles = responsiveStyle(value, property);
        // Merge the styles, using any casts to satisfy TypeScript
        Object.entries(styles).forEach(([key, val]) => {
            if (key.startsWith('@media')) {
                // Merge media query styles
                const existing = acc[key] || {};
                const toMerge = typeof val === 'object' && val !== null ? val : {};
                acc[key] = {
                    ...existing,
                    ...toMerge,
                };
            }
            else {
                // Assign non-media styles directly
                acc[key] = val;
            }
        });
        return acc;
    }, {});
}
/**
 * Get value for the current breakpoint
 * @param values Object with values for each breakpoint
 * @param currentBreakpoint Current breakpoint
 * @returns The value for the current breakpoint
 */
function getValueForBreakpoint(values, currentBreakpoint) {
    // Order breakpoints from smallest to largest
    const breakpointOrder = ['base', 'sm', 'md', 'lg', 'xl', '2xl'];
    // Get the index of the current breakpoint
    const currentIndex = breakpointOrder.indexOf(currentBreakpoint);
    // Look for the closest defined breakpoint that's less than or equal to the current one
    for (let i = currentIndex; i >= 0; i--) {
        // Non-null assertion because index is within known range
        const bp = breakpointOrder[i];
        if (values[bp] !== undefined) {
            return values[bp];
        }
    }
    // Default to the base value if available
    return values['base'];
}
//# sourceMappingURL=responsiveUtils.js.map