import type { Loads, LoadCombination, DesignMethod, LoadCombinationOption } from '../types/connection';

export function calculateLoadCombinations(
    loads: Loads,
    designMethod: DesignMethod,
    option: LoadCombinationOption
): LoadCombination[] {
    if (option === 'pre-combined') {
        // User provides single pre-combined load
        return [{
            id: 1,
            equation: '1.4D',
            pu: 1.4 * loads.dead,
            passes: true,
        }];
    }

    // ASCE 7 load combinations
    const D = loads.dead;
    const L = loads.live;
    const Lr = loads.rLive;
    const S = loads.snow;
    const W = loads.wind;
    const E = loads.seismic;

    if (designMethod === 'LRFD') {
        return [
            {
                id: 1,
                equation: '1.4D',
                pu: 1.4 * D,
                passes: true,
            },
            {
                id: 2,
                equation: '1.2D+1.6L+0.5Lr',
                pu: 1.2 * D + 1.6 * L + 0.5 * Lr,
                passes: true,
            },
            {
                id: 3,
                equation: '1.2D+1.6L+0.5S',
                pu: 1.2 * D + 1.6 * L + 0.5 * S,
                passes: true,
            },
            {
                id: 4,
                equation: '1.2D+0.5L+1.6Lr',
                pu: 1.2 * D + 0.5 * L + 1.6 * Lr,
                passes: true,
            },
            {
                id: 5,
                equation: '1.2D+0.5L+1.6S',
                pu: 1.2 * D + 0.5 * L + 1.6 * S,
                passes: true,
            },
            {
                id: 6,
                equation: '1.2D+1.6Lr+0.5W',
                pu: 1.2 * D + 1.6 * Lr + 0.5 * W,
                passes: true,
            },
            {
                id: 7,
                equation: '1.2D+1.6S+0.5W',
                pu: 1.2 * D + 1.6 * S + 0.5 * W,
                passes: true,
            },
            {
                id: 8,
                equation: '1.2D+0.5L+0.5Lr+W',
                pu: 1.2 * D + 0.5 * L + 0.5 * Lr + W,
                passes: true,
            },
            {
                id: 9,
                equation: '1.2D+0.5L+0.5S+W',
                pu: 1.2 * D + 0.5 * L + 0.5 * S + W,
                passes: true,
            },
            {
                id: 10,
                equation: '1.2D+0.5L+0.2S+E',
                pu: 1.2 * D + 0.5 * L + 0.2 * S + E,
                passes: true,
            },
            {
                id: 11,
                equation: '0.9D+W',
                pu: 0.9 * D + W,
                passes: true,
            },
            {
                id: 12,
                equation: '0.9D+E',
                pu: 0.9 * D + E,
                passes: true,
            },
        ].filter(combo => combo.pu > 0);
    } else {
        // ASD combinations
        return [
            {
                id: 1,
                equation: 'D',
                pu: D,
                passes: true,
            },
            {
                id: 2,
                equation: 'D+L',
                pu: D + L,
                passes: true,
            },
            {
                id: 3,
                equation: 'D+Lr',
                pu: D + Lr,
                passes: true,
            },
            {
                id: 4,
                equation: 'D+S',
                pu: D + S,
                passes: true,
            },
            {
                id: 5,
                equation: 'D+0.75L+0.75Lr',
                pu: D + 0.75 * L + 0.75 * Lr,
                passes: true,
            },
            {
                id: 6,
                equation: 'D+0.75L+0.75S',
                pu: D + 0.75 * L + 0.75 * S,
                passes: true,
            },
            {
                id: 7,
                equation: 'D+0.6W',
                pu: D + 0.6 * W,
                passes: true,
            },
            {
                id: 8,
                equation: 'D+0.75L+0.75(0.6W)',
                pu: D + 0.75 * L + 0.75 * 0.6 * W,
                passes: true,
            },
            {
                id: 9,
                equation: 'D+0.7E',
                pu: D + 0.7 * E,
                passes: true,
            },
            {
                id: 10,
                equation: '0.6D+0.6W',
                pu: 0.6 * D + 0.6 * W,
                passes: true,
            },
        ].filter(combo => combo.pu > 0);
    }
}
