import { isTruthy, checkValueIsFilled, checkValueIsEmpty } from '@angular-ru/cdk/utils';
import { takeFirstItem } from '@angular-ru/cdk/array';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { clean } from '@angular-ru/cdk/object';
import { DefaultUrlSerializer } from '@angular/router';

function getPathWithoutQueryParams(path) {
    var _a;
    return (_a = takeFirstItem(path.split('?'))) !== null && _a !== void 0 ? _a : '';
}

function isAbsolutePath(path, matcher = /^(http|https):\/\//) {
    const result = path.match(matcher);
    return isTruthy(result);
}

function replaceDoubleSlash(url) {
    return url === null || url === void 0 ? void 0 : url.replace(/(https?:\/\/)|(\/)+/g, '$1$2');
}

function buildUrl({ hostUrl, baseUrl, restUrl, pathUrl }) {
    let fullUrl;
    if (isAbsolutePath(pathUrl)) {
        fullUrl = pathUrl;
    }
    else {
        const clearPathUrl = getPathWithoutQueryParams(pathUrl);
        fullUrl = [hostUrl, baseUrl, restUrl, clearPathUrl].filter(checkValueIsFilled).join('/');
    }
    return replaceDoubleSlash(fullUrl);
}

function ensurePathByPathVariables(path, map) {
    var _a;
    let newPath = path.toString();
    const parsedUrls = (_a = newPath.match(/\{(.*?)\}/g)) !== null && _a !== void 0 ? _a : [];
    for (const variable of parsedUrls) {
        const key = variable.replace(/{|}/g, '');
        if (map.has(key)) {
            newPath = newPath.replace(variable, map.get(key));
        }
    }
    return newPath;
}

function getHttpHeader(params = {}) {
    return new HttpHeaders(clean(params));
}

function parseQueryParams(path) {
    return new DefaultUrlSerializer().parse(path).queryParams;
}

function getHttpParams(path, params) {
    const queryPath = parseQueryParams(path);
    const fromObject = clean(Object.assign(Object.assign({}, queryPath), (params !== null && params !== void 0 ? params : {})));
    return new HttpParams({ fromObject });
}

function isLocalhost(url = location.href) {
    return url.includes('://localhost') || url.includes('://127.0.0.1') || url.includes('://0.0.0.0');
}

function replaceLeadingAndTrailingSlashes(inputString) {
    const pattern = new RegExp('^\\/+|\\/+$', 'g');
    return checkValueIsEmpty(inputString) ? '' : inputString.trim().replace(pattern, '');
}

function makeUrlSegments({ hostUrl, baseUrl } = {}, restUrl = '', pathUrl = '') {
    const clearHostUrl = replaceLeadingAndTrailingSlashes(hostUrl) || `${window.location.protocol}//${window.location.host}`;
    return {
        hostUrl: clearHostUrl,
        baseUrl: replaceLeadingAndTrailingSlashes(baseUrl),
        restUrl: replaceLeadingAndTrailingSlashes(restUrl),
        pathUrl: replaceLeadingAndTrailingSlashes(pathUrl)
    };
}

/**
 * Generated bundle index. Do not edit.
 */

export { buildUrl, ensurePathByPathVariables, getHttpHeader, getHttpParams, getPathWithoutQueryParams, isAbsolutePath, isLocalhost, makeUrlSegments, parseQueryParams, replaceDoubleSlash, replaceLeadingAndTrailingSlashes };
//# sourceMappingURL=angular-ru-http-utils.js.map
