import { AppNodeData } from '../types/node';

const DEFAULT_NODE_WIDTH = 420;
const INFO_NODE_HEIGHT = 560;
const AUDIO_NODE_HEIGHT = 200;
const DEFAULT_FIXED_HEIGHT = 360;

export const useNodeDimensions = (data: AppNodeData) => {
    const { type } = data;

    const getDefaultAspectRatio = () => {
        if (type === 'INFO_DISPLAY') return '3:4';
        if (type === 'PROMPT_GENERATOR' || type === 'PROMPT_INPUT' || type === 'LYRICS_GENERATOR') return '4:3';
        return '16:9';
    };

    const ratio = data.aspectRatio || getDefaultAspectRatio();
    const [aspectW, aspectH] = ratio.split(':').map(Number);
    const isVideoCut = type === 'VIDEO_GENERATOR' && data.generationMode === 'CUT';
    const extraHeight = isVideoCut ? 36 : 0;
    const shouldKeepRatio = extraHeight === 0;

    const getNodeDimensions = () => {
        if (type === 'VIDEO_ANALYZER' || type === 'IMAGE_EDITOR') return { width: DEFAULT_NODE_WIDTH, height: DEFAULT_FIXED_HEIGHT };
        if (type === 'AUDIO_GENERATOR') return { width: DEFAULT_NODE_WIDTH, height: AUDIO_NODE_HEIGHT };
        
        let width = data.width || DEFAULT_NODE_WIDTH;
        if (!data.width && (ratio === '9:16' || ratio === '3:4') && type !== 'INFO_DISPLAY') {
            width = 280;
        }
        
        return { width, height: (width * aspectH / aspectW) + extraHeight };
    };

    const { width: nodeWidth, height: nodeHeight } = getNodeDimensions();
    const minHeight = (200 * aspectH / aspectW) + extraHeight;

    return {
        nodeWidth,
        nodeHeight,
        minHeight,
        ratio,
        aspectW,
        aspectH,
        extraHeight,
        shouldKeepRatio
    };
};
