import { escapeRegExp } from 'lodash';
/**
 * Builds a URL query for given SearchOptions (without leading `?`)
 */
export function buildSearchURLQuery(options) {
    const searchParams = new URLSearchParams();
    searchParams.set('q', options.query);
    return searchParams
        .toString()
        .replace(/%2F/g, '/')
        .replace(/%3A/g, ':');
}
/**
 * Parses the SearchOptions out of URL search params. If neither the 'q' nor
 * 'sq' params are present, it returns undefined.
 */
export function parseSearchURLQuery(query) {
    const searchParams = new URLSearchParams(query);
    if (!searchParams.has('q') && !searchParams.has('sq')) {
        return undefined;
    }
    return {
        query: ['sq', 'q']
            .map(param => searchParams.get(param))
            .filter(s => s)
            .join(' '),
    };
}
export function searchQueryForRepoRev(repoPath, rev) {
    return `repo:${quoteIfNeeded(`^${escapeRegExp(repoPath)}$${rev ? `@${abbreviateOID(rev)}` : ''}`)} `;
}
function abbreviateOID(oid) {
    if (oid.length === 40) {
        return oid.slice(0, 7);
    }
    return oid;
}
export function quoteIfNeeded(s) {
    if (/["' ]/.test(s)) {
        return JSON.stringify(s);
    }
    return s;
}
//# sourceMappingURL=index.js.map