/**
 * Utility functions for handling text justification and line width calculations
 * across PDF generation and UI previews.
 */

/**
 * Calculates the effective maximum width for content based on page constraints and configuration.
 * @param {number} pageWidth - Total width of the page/container
 * @param {object} margins - Margins object { left, right, ... }
 * @param {number} lineWidthPercent - Percentage of available width to use (0-100)
 * @returns {number} Effective width in pixels/points
 */
export const calculateMaxWidth = (pageWidth, margins, lineWidthPercent = 100) => {
    const safeLineWidth = Math.min(Math.max(parseFloat(lineWidthPercent) || 100, 10), 100);
    const availableSpace = pageWidth - (margins.left || 0) - (margins.right || 0);
    return availableSpace * (safeLineWidth / 100);
};

/**
 * Determines the text alignment for a block based on its type and configuration.
 * Body text defaults to 'justify' unless overridden. Headers default to 'left' or 'center'.
 * @param {string} blockType - The type of block (e.g., 'unified_text', 'heading')
 * @param {string} configuredAlign - Explicit alignment from block styles (optional)
 * @returns {string} One of 'left', 'center', 'right', 'justify'
 */
export const getTextAlignment = (blockType, configuredAlign) => {
    if (configuredAlign) return configuredAlign;
    
    // Default behaviors if no explicit alignment set
    switch (blockType) {
        case 'heading':
        case 'title':
        case 'subtitle':
            return 'left'; // or center, depending on design pref
        case 'unified_text':
        case 'paragraph':
        case 'body':
        case 'text_section':
            return 'justify'; // Default body text to justify
        default:
            return 'left';
    }
};

/**
 * Helper to determine if a line should be justified.
 * Typically the last line of a paragraph should NOT be justified.
 * @param {boolean} isLastLine - Is this the last line of the paragraph?
 * @param {string} alignment - The requested alignment
 * @returns {boolean} True if justification logic should be applied
 */
export const shouldJustifyLine = (isLastLine, alignment) => {
    return alignment === 'justify' && !isLastLine;
};