import semver from 'semver';
import { stringify } from './utils.js';
export function getCommitGroups(commits, options) {
    const { groupBy, commitGroupsSort, commitsSort } = options;
    const commitGroups = [];
    const commitGroupsObj = commits.reduce((groups, commit) => {
        const key = commit[groupBy] || '';
        if (groups[key]) {
            groups[key].push(commit);
        }
        else {
            groups[key] = [commit];
        }
        return groups;
    }, {});
    Object.entries(commitGroupsObj).forEach(([title, commits]) => {
        if (commitsSort) {
            commits.sort(commitsSort);
        }
        commitGroups.push({
            title,
            commits
        });
    });
    if (commitGroupsSort) {
        commitGroups.sort(commitGroupsSort);
    }
    return commitGroups;
}
export function getNoteGroups(notes, options) {
    const { noteGroupsSort, notesSort } = options;
    const retGroups = [];
    notes.forEach((note) => {
        const { title } = note;
        let titleExists = false;
        retGroups.forEach((group) => {
            if (group.title === title) {
                titleExists = true;
                group.notes.push(note);
            }
        });
        if (!titleExists) {
            retGroups.push({
                title,
                notes: [note]
            });
        }
    });
    if (noteGroupsSort) {
        retGroups.sort(noteGroupsSort);
    }
    if (notesSort) {
        retGroups.forEach((group) => {
            group.notes.sort(notesSort);
        });
    }
    return retGroups;
}
export function getExtraContext(commits, notes, options) {
    return {
        // group `commits` by `options.groupBy`
        commitGroups: getCommitGroups(commits, options),
        // group `notes` for footer
        noteGroups: getNoteGroups(notes, options)
    };
}
/**
 * Get final context with default values.
 * @param context
 * @param options
 * @returns Final context with default values.
 */
export function getFinalContext(context, options) {
    const finalContext = {
        commit: 'commits',
        issue: 'issues',
        date: options.formatDate(new Date()),
        ...context
    };
    if (typeof finalContext.linkReferences !== 'boolean'
        && (finalContext.repository || finalContext.repoUrl)
        && finalContext.commit
        && finalContext.issue) {
        finalContext.linkReferences = true;
    }
    return finalContext;
}
/**
 * Get context prepared for template.
 * @param keyCommit
 * @param commits
 * @param filteredCommits
 * @param notes
 * @param context
 * @param options
 * @returns Context prepared for template.
 */
export async function getTemplateContext(keyCommit, commits, filteredCommits, notes, context, options) {
    let templateContext = {
        ...context,
        ...keyCommit,
        ...getExtraContext(filteredCommits, notes, options)
    };
    if (keyCommit?.committerDate) {
        templateContext.date = keyCommit.committerDate;
    }
    if (templateContext.version && semver.valid(templateContext.version)) {
        templateContext.isPatch = templateContext.isPatch || semver.patch(templateContext.version) !== 0;
    }
    templateContext = await options.finalizeContext(templateContext, options, filteredCommits, keyCommit, commits);
    options.debug(`Your final context is:\n${stringify(templateContext)}`);
    return templateContext;
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 4308 characters]