import { hasNoItems } from '@angular-ru/cdk/array';
import { isFunctionLike } from '@angular-ru/cdk/function';

function capitalize(value) {
    return value.charAt(0).toUpperCase() + value.slice(1);
}

// noinspection SuspiciousTypeOfGuard
function filter(value, predicate = []) {
    if (Array.isArray(predicate)) {
        return filterWithCharacters(value, predicate);
    }
    if (isFunctionLike(predicate)) {
        return filterWithFunction(value, predicate);
    }
    if (predicate instanceof RegExp) {
        return filterWithRegExp(value, predicate);
    }
    return value;
}
function filterWithCharacters(value, predicate = []) {
    if (hasNoItems(predicate)) {
        return value;
    }
    else {
        return Array.from(value)
            .filter((char) => predicate.includes(char))
            .join('');
    }
}
function filterWithFunction(value, predicate) {
    return Array.from(value)
        .filter((element, index, array) => predicate(element, index, array))
        .join('');
}
function filterWithRegExp(value, predicate) {
    return (value.match(predicate) ?? []).join('');
}

function generateQuickGuid() {
    const radix = 36;
    const startWith = 2;
    const endWith = 15;
    return (Math.random().toString(radix).substring(startWith, endWith) +
        Math.random().toString(radix).substring(startWith, endWith));
}

function getByteSize(value) {
    return new Blob([value]).size;
}

function getCountSpacesOnString(value) {
    return value?.match(/\s/g)?.length ?? 0;
}

function getFirstSymbol(value) {
    return value?.slice(0, 1);
}

function getLastSymbol(value) {
    return value?.slice(-1);
}

function isString(value) {
    return typeof value === 'string';
}

function removeLastSymbol(value) {
    return value?.slice(0, -1);
}

function removeNonNumericSymbols(value) {
    return value?.replace(/[^\d,.-]/g, '').replace(/^\.|^,/, '') ?? '';
}

function replaceEveryCommaOnDot(value) {
    return value?.replace(/,/g, '.') ?? '';
}

function splitOnUniqueValues(value, separator = /[,;]|(\s+)/g) {
    const uniqueValues = new Set();
    const parsedValues = value?.split(separator) ?? [];
    for (const item of parsedValues) {
        const prepared = item?.trim();
        if (prepared) {
            uniqueValues.add(prepared);
        }
    }
    return Array.from(uniqueValues);
}

const SPACE_INDENT = 4;
function stringify(value, spaceIndent = SPACE_INDENT) {
    return String(JSON.stringify(value, null, spaceIndent)).toString();
}

function toStringValue(value, converter) {
    return isFunctionLike(converter) ? converter(value) : value?.toString() ?? '';
}

function trim(value) {
    // note: don't use isString for preserve circular dependencies
    return (typeof value === 'string' ? value?.trim() : value);
}

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

export { capitalize, filter, generateQuickGuid, getByteSize, getCountSpacesOnString, getFirstSymbol, getLastSymbol, isString, removeLastSymbol, removeNonNumericSymbols, replaceEveryCommaOnDot, splitOnUniqueValues, stringify, toStringValue, trim };
//# sourceMappingURL=angular-ru-cdk-string.mjs.map
