import { valid as semverValid } from 'semver';
import { formatDate, createComparator } from './utils.js';
const HASH_SHORT_LENGTH = 7;
const HEADER_MAX_LENGTH = 100;
/**
 * Default commit transform function.
 * @param commit
 * @param _context
 * @param options
 * @param options.formatDate - Date formatter function.
 * @returns Patch object for commit.
 */
export function defaultCommitTransform(commit, _context, options) {
    const { hash, header, committerDate } = commit;
    return {
        hash: typeof hash === 'string'
            ? hash.substring(0, HASH_SHORT_LENGTH)
            : hash,
        header: typeof header === 'string'
            ? header.substring(0, HEADER_MAX_LENGTH)
            : header,
        committerDate: committerDate
            ? options.formatDate(committerDate)
            : committerDate
    };
}
/**
 * Get final options object.
 * @param options
 * @param templates
 * @returns Final options object.
 */
export function getFinalOptions(options, templates) {
    const prefinalOptions = {
        groupBy: 'type',
        commitsSort: 'header',
        noteGroupsSort: 'title',
        notesSort: 'text',
        transform: defaultCommitTransform,
        generateOn: (commit) => Boolean(semverValid(commit.version)),
        finalizeContext: (context) => context,
        debug: () => { },
        formatDate,
        reverse: false,
        ignoreReverted: true,
        doFlush: true,
        ...templates,
        ...options
    };
    const finalOptions = {
        ...prefinalOptions,
        commitGroupsSort: createComparator(prefinalOptions.commitGroupsSort),
        commitsSort: createComparator(prefinalOptions.commitsSort),
        noteGroupsSort: createComparator(prefinalOptions.noteGroupsSort),
        notesSort: createComparator(prefinalOptions.notesSort)
    };
    return finalOptions;
}
/**
 * Get final context object.
 * @param context
 * @param options
 * @returns Final context object.
 */
export function getGenerateOnFunction(context, options) {
    const { generateOn } = options;
    if (typeof generateOn === 'string') {
        return (commit) => typeof commit[generateOn] !== 'undefined';
    }
    else if (typeof generateOn !== 'function') {
        return () => false;
    }
    return (keyCommit, commitsGroup) => generateOn(keyCommit, commitsGroup, context, options);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3B0aW9ucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9vcHRpb25zLnRzIl0s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