"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeIssueSearchQuery = normalizeIssueSearchQuery;
exports.matchesIssueSearchTitle = matchesIssueSearchTitle;
exports.shouldFilterIssueSearchTitle = shouldFilterIssueSearchTitle;
exports.isIssueSearchLimitReached = isIssueSearchLimitReached;
exports.limitIssueSearchResults = limitIssueSearchResults;
exports.sortIssueSearchResultsByUpdatedAtDesc = sortIssueSearchResultsByUpdatedAtDesc;
const types_1 = require("./types");
function normalizeIssueSearchQuery(query) {
    const text = normalizeIssueSearchText(query.text);
    const searchTerm = normalizeIssueSearchApiTerm(query.text);
    const limit = normalizeIssueSearchLimit(query.limit);
    if (text.length === 0 || searchTerm.length === 0 || limit === 0) {
        return null;
    }
    return { mode: query.mode, text, searchTerm, limit };
}
function matchesIssueSearchTitle(title, query) {
    return normalizeIssueSearchText(title).includes(query.text);
}
function shouldFilterIssueSearchTitle(query) {
    return query.mode === types_1.TRACKER_ISSUE_SEARCH_MODES.title;
}
function isIssueSearchLimitReached(query, resultCount) {
    return query.limit !== null && resultCount >= query.limit;
}
function limitIssueSearchResults(results, query) {
    if (query.limit === null) {
        return [...results];
    }
    return results.slice(0, query.limit);
}
function sortIssueSearchResultsByUpdatedAtDesc(results) {
    return [...results].sort((left, right) => right.updatedAt.localeCompare(left.updatedAt));
}
function normalizeIssueSearchText(value) {
    return value.trim().toLowerCase();
}
function normalizeIssueSearchApiTerm(value) {
    return value.replace(/[#()]/gu, " ").replace(/\s+/gu, " ").trim();
}
function normalizeIssueSearchLimit(limit) {
    if (limit === null) {
        return null;
    }
    if (limit === undefined) {
        return types_1.DEFAULT_TRACKER_ISSUE_SEARCH_LIMIT;
    }
    return Math.max(0, Math.floor(limit));
}
