"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePaginationParams = exports.extractQueryParamsFromPath = exports.getQueryParam = exports.normalizeUrl = exports.computeRestfulQueryParams = void 0;
const errors_1 = require("../errors");
const dateQueryParams = ['fromDate', 'toDate', 'onDate'];
const authorQueryParams = ['username', 'authorId'];
const toRestfulQueryString = (paramName) => `${paramName}=:${paramName}`;
const computeRestfulQueryParams = (options) => {
    const { isDateParamsIncluded, isAuthorParamsIncluded } = options;
    const queryParams = [];
    if (isDateParamsIncluded) {
        queryParams.push(...dateQueryParams);
    }
    if (isAuthorParamsIncluded) {
        queryParams.push(...authorQueryParams);
    }
    return queryParams.map(toRestfulQueryString).join('&');
};
exports.computeRestfulQueryParams = computeRestfulQueryParams;
const normalizeUrl = (url) => url.replace(/([^:])(\/\/+)/g, '$1/');
exports.normalizeUrl = normalizeUrl;
const getQueryParam = (queryParams, paramName) => {
    const param = queryParams === null || queryParams === void 0 ? void 0 : queryParams[paramName];
    if (!param) {
        throw new errors_1.BadParamsError(`Query param ${paramName} not specified`, [
            { name: paramName, message: 'Should be specified in url' },
        ]);
    }
    return param;
};
exports.getQueryParam = getQueryParam;
const extractQueryParamsFromPath = (relativePath) => {
    const parsedUrl = new URL(relativePath, 'https://base/');
    return Object.fromEntries(parsedUrl.searchParams || {});
};
exports.extractQueryParamsFromPath = extractQueryParamsFromPath;
const parsePaginationParam = (query, name) => {
    const param = query === null || query === void 0 ? void 0 : query[name];
    if (param === undefined) {
        return undefined;
    }
    if (typeof param === 'number') {
        return param;
    }
    if (typeof param === 'string') {
        const parsedParam = parseInt(param);
        if (Number.isNaN(parsedParam)) {
            throw new Error(`Malformed integer ${name}: got ${param}`);
        }
        return parsedParam;
    }
    throw new Error(`Param ${name} should be either a string or not defined`);
};
const getAndValidatePaginationParam = (query, name) => {
    try {
        const value = parsePaginationParam(query, name);
        if (value !== undefined && value < 1) {
            throw new Error(`${name} should be bigger than 0`);
        }
        return value;
    }
    catch (error) {
        throw new errors_1.BadParamsError('please ensure value is string representation of integers and are greater than zero', [
            { name: name, message: error.message },
        ]);
    }
};
const parsePaginationParams = (query) => {
    return {
        pageSize: getAndValidatePaginationParam(query, 'pageSize'),
        pageNumber: getAndValidatePaginationParam(query, 'pageNumber'),
    };
};
exports.parsePaginationParams = parsePaginationParams;
//# sourceMappingURL=query-params.js.map