// File: src/data/dependency.js

/**
 * لود وابستگی‌ها در state.dependencies
 * @param {Object} state
 * @param {Object} projectData
 */
export function loadDependencies(state, projectData) {
    if (!Array.isArray(projectData.dependencies)) {
        return;
    }

    state.dependencies.byId = {};
    state.dependencies.bySuccessorId = {};

    projectData.dependencies.forEach(d => {
        const id = d.id;
        if (!id) return;

        const dep = {
            id,
            // پذیرش هم camelCase هم snake_case
            predecessorId: d.predecessorId ?? d.predecessor_id ?? null,
            successorId: d.successorId ?? d.successor_id ?? null,
            type: d.type ?? 'FS',
            lag: d.lag ?? 0
        };

        state.dependencies.byId[id] = dep;

        if (dep.successorId != null) {
            if (!state.dependencies.bySuccessorId[dep.successorId]) {
                state.dependencies.bySuccessorId[dep.successorId] = [];
            }
            state.dependencies.bySuccessorId[dep.successorId].push(id);
        }
    });
}
