"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveConflicts = exports.concatFiles = exports.getFilesList = exports.getName = exports.scale = exports.applyActionsForDeletedFiles = exports.HANDLE_DELETED = exports.COLORS_BY_STATUS = exports.MESSAGES_BY_STATUS = exports.RESOLVE_OPTIONS = exports.LOCAL_BRANCH = exports.UPSTREAM_BRANCH = exports.AutoResolve = void 0;
const ramda_1 = require("ramda");
const MergeTool_interface_1 = require("../../../interfaces/MergeTool.interface");
exports.AutoResolve = [MergeTool_interface_1.ResolveConflicts.ApplyLocal, MergeTool_interface_1.ResolveConflicts.ApplyRemote];
exports.UPSTREAM_BRANCH = 'upstream';
exports.LOCAL_BRANCH = 'local';
const LOCAL = '<<<<<<< HEAD';
const DIVIDER = '=======';
const REMOTE = `>>>>>>> ${exports.UPSTREAM_BRANCH}`;
exports.RESOLVE_OPTIONS = {
    [MergeTool_interface_1.ResolveConflicts.ApplyLocal]: { from: DIVIDER, to: REMOTE, remove: LOCAL },
    [MergeTool_interface_1.ResolveConflicts.ApplyRemote]: { from: LOCAL, to: DIVIDER, remove: REMOTE },
};
exports.MESSAGES_BY_STATUS = {
    'created': 'new file:   ',
    'deleted': 'deleted:    ',
    'modified': 'modified:   ',
    'conflicted': 'conflicted: ',
};
exports.COLORS_BY_STATUS = {
    'created': '#39BF7F',
    'deleted': '#DE3157',
    'modified': '#D8791F',
    'conflicted': '#DE3157',
};
exports.HANDLE_DELETED = {
    [MergeTool_interface_1.ResolveConflicts.ApplyRemote]: ({ meta, file }) => ({ file, action: meta.deleteRef === exports.UPSTREAM_BRANCH ? 'deleted' : 'created' }),
    [MergeTool_interface_1.ResolveConflicts.ApplyLocal]: ({ file }) => ({ file, action: 'skip' }),
    [MergeTool_interface_1.ResolveConflicts.Manually]: ({ file, meta }) => ({
        file,
        action: 'addMessage',
        message: `deleted in the ${meta.deleteRef === exports.UPSTREAM_BRANCH ? 'upstream' : 'local'} version`,
    })
};
function applyActionsForDeletedFiles(actions, conflicts) {
    return actions.reduce((statuses, { file, action, message }) => {
        switch (action) {
            case 'deleted': {
                statuses.deleted.push(file);
                statuses.conflicted = statuses.conflicted.filter(conflicted => conflicted !== file);
                break;
            }
            case 'created': {
                statuses.created.push(file);
                statuses.conflicted = statuses.conflicted.filter(conflicted => conflicted !== file);
                break;
            }
            case 'skip': {
                statuses.conflicted = statuses.conflicted.filter(conflicted => conflicted !== file);
                break;
            }
            case 'addMessage': {
                statuses.conflicted = statuses.conflicted.map(conflicted => conflicted === file ? { file, message } : conflicted);
                break;
            }
        }
        return statuses;
    }, (0, ramda_1.clone)(conflicts));
}
exports.applyActionsForDeletedFiles = applyActionsForDeletedFiles;
function scale(from, to) {
    return (progress) => from + progress * (to - from) / 100;
}
exports.scale = scale;
function getName(file) {
    return typeof file === 'string' ? file : file.file;
}
exports.getName = getName;
function getFilesList(files) {
    return files.map(getName);
}
exports.getFilesList = getFilesList;
function concatFiles(files) {
    return Object
        .values(files)
        .reduce((all, files) => all.concat(getFilesList(files)), []);
}
exports.concatFiles = concatFiles;
function resolveConflicts(file, resolve) {
    const { from, to, remove } = exports.RESOLVE_OPTIONS[resolve];
    return file
        .replace(new RegExp(`${from}(.|\r?\n)*?${to}\r?\n`, 'gm'), '')
        .replace(new RegExp(`${remove}\r?\n`, 'g'), '');
}
exports.resolveConflicts = resolveConflicts;
//# sourceMappingURL=mergeUtils.js.map