"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformPlainTextValueToValueWithMentions = exports.createCommentUri = exports.createEditedReplyUri = exports.createEditedCommentUri = exports.createNewReplyUri = exports.createNewCommentUri = exports.processMentions = exports.formatDate = exports.getRelatedEntityUri = exports.getAbbreviationFromUserName = void 0;
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var getAbbreviationFromUserName = function (username) {
    return username
        .split('.')
        .map(function (item) { return item[0].toUpperCase(); })
        .join('');
};
exports.getAbbreviationFromUserName = getAbbreviationFromUserName;
var getRelatedEntityUri = function (comment, entityUri) {
    var index = comment.relatedObjectUris.indexOf(entityUri);
    if (index >= 0)
        return comment.relatedObjectUris[1 - index];
};
exports.getRelatedEntityUri = getRelatedEntityUri;
var formatDate = function (time) { return "".concat(ui_i18n_1.default.date(time, 'LT'), " ").concat(ui_i18n_1.default.date(time, 'LL')); };
exports.formatDate = formatDate;
var processMentions = function (text, mentions, processor, trigger) {
    if (processor === void 0) { processor = function (value) { return value; }; }
    if (trigger === void 0) { trigger = '+'; }
    var arr = [];
    var currentStr = '';
    var process = false;
    for (var i = 0; i <= text.length; i++) {
        var currentCharacter = text[i];
        var nextCharacter = text[i + 1];
        var isLastCharacter = i === text.length - 1;
        currentStr += currentCharacter;
        if (currentCharacter === trigger && i === 0) {
            process = true;
        }
        if (process && mentions.includes(currentStr.slice(trigger.length))) {
            process = false;
            arr.push(processor(currentStr));
            currentStr = '';
        }
        if (nextCharacter === trigger) {
            process = true;
            arr.push(currentStr);
            currentStr = '';
        }
        if (isLastCharacter) {
            arr.push(currentStr);
        }
    }
    return arr;
};
exports.processMentions = processMentions;
var createNewCommentUri = function () { return 'comment/new'; };
exports.createNewCommentUri = createNewCommentUri;
var createNewReplyUri = function (comment) { return "comment/".concat(comment.commentId, "/reply/new"); };
exports.createNewReplyUri = createNewReplyUri;
var createEditedCommentUri = function (comment) { return "comment/".concat(comment.commentId); };
exports.createEditedCommentUri = createEditedCommentUri;
var createEditedReplyUri = function (comment, reply) {
    return "comment/".concat(comment.commentId, "/reply/").concat(reply.replyId);
};
exports.createEditedReplyUri = createEditedReplyUri;
var createCommentUri = function (_a) {
    var comment = _a.comment, reply = _a.reply, isEditingComment = _a.isEditingComment, isEditingReply = _a.isEditingReply;
    if ((0, ramda_1.isNil)(comment)) {
        return (0, exports.createNewCommentUri)();
    }
    if (isEditingComment) {
        return (0, exports.createEditedCommentUri)(comment);
    }
    if (isEditingReply) {
        return (0, exports.createEditedReplyUri)(comment, reply);
    }
    return (0, exports.createNewReplyUri)(comment);
};
exports.createCommentUri = createCommentUri;
var transformPlainTextValueToValueWithMentions = function (plainTextValue, mentions) {
    var value = plainTextValue;
    for (var _i = 0, mentions_1 = mentions; _i < mentions_1.length; _i++) {
        var mention = mentions_1[_i];
        value = value.split("+".concat(mention)).join("@[".concat(mention, "](").concat(mention, ")"));
    }
    return value;
};
exports.transformPlainTextValueToValueWithMentions = transformPlainTextValueToValueWithMentions;
