import { sum } from 'rambda';
import { byNum, desc } from 'sort-lib';
const dateString = (date) => date.toISOString().split('T')[0];
const mergeCommit = (commit, aggregatedCommits) => {
    var _a, _b, _c;
    return ({
        name: commit.committer.name,
        imageUrl: commit.committer.imageUrl,
        changes: {
            add: ((aggregatedCommits === null || aggregatedCommits === void 0 ? void 0 : aggregatedCommits.changes.add) || 0) + (((_a = commit.changeCounts) === null || _a === void 0 ? void 0 : _a.add) || 0),
            delete: ((aggregatedCommits === null || aggregatedCommits === void 0 ? void 0 : aggregatedCommits.changes.delete) || 0) + (((_b = commit.changeCounts) === null || _b === void 0 ? void 0 : _b.delete) || 0),
            edit: ((aggregatedCommits === null || aggregatedCommits === void 0 ? void 0 : aggregatedCommits.changes.edit) || 0) + (((_c = commit.changeCounts) === null || _c === void 0 ? void 0 : _c.edit) || 0),
        },
        byDate: {
            ...aggregatedCommits === null || aggregatedCommits === void 0 ? void 0 : aggregatedCommits.byDate,
            [dateString(commit.committer.date)]: ((aggregatedCommits === null || aggregatedCommits === void 0 ? void 0 : aggregatedCommits.byDate[dateString(commit.committer.date)]) || 0) + 1,
        },
    });
};
export default (commits) => {
    const commitsByDev = commits.reduce((acc, commit) => {
        if (!commit.changeCounts)
            return acc;
        acc[commit.committer.name] = mergeCommit(commit, acc[commit.committer.name]);
        return acc;
    }, {});
    return {
        count: sum(Object.values(commitsByDev).map(c => sum(Object.values(c.byDate)))),
        byDev: Object.values(commitsByDev).sort(desc(byNum(x => sum(Object.values(x.byDate))))),
    };
};
