import { add, multiply, range } from 'rambda';
import { maybe } from './maybe.js';
export const oneSecondInMs = 1000;
export const oneMinuteInMs = 60 * oneSecondInMs;
export const oneHourInMs = 60 * oneMinuteInMs;
export const oneDayInMs = 24 * oneHourInMs;
export const oneWeekInMs = 7 * oneDayInMs;
export const oneFortnightInMs = 15 * oneDayInMs;
export const oneMonthInMs = 30 * oneDayInMs;
export const oneYearInMs = 365 * oneDayInMs;
export const unique = (xs) => [...new Set(xs)];
export const exists = (x) => x !== null && x !== undefined;
export const divide = (numerator, denominator) => maybe(numerator / denominator);
export const toPercentage = (value) => `${Math.round(value * 100)}%`;
export const combineColumnsInArray = (combiner) => (rows) => rows.reduce((acc, row) => {
    row.forEach((val, index) => {
        acc[index] = combiner(acc[index], val);
    });
    return acc;
}, []);
export const addColumnsInArray = combineColumnsInArray((a, b) => add(a || 0, b));
export const mapObj = (xform) => (obj) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, xform(value)]));
export const merge = (obj1, obj2) => ({ ...obj1, ...obj2 });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const debounce = (fn, ms = 250) => {
    let timeoutId;
    return (...args) => {
        clearTimeout(timeoutId);
        timeoutId = setTimeout(() => fn(...args), ms);
    };
};
export const weightedQualityGate = (qualityGateStatus) => {
    if (qualityGateStatus.length === 0)
        return -1;
    const qualityGatesPassed = qualityGateStatus.filter(status => status !== 'fail');
    if (qualityGatesPassed.length === qualityGateStatus.length)
        return 100;
    return divide(qualityGatesPassed.length, qualityGateStatus.length)
        .map(multiply(100))
        .getOr(0);
};
export const shouldNeverReachHere = (x) => {
    throw new Error(`Should never reach here: ${x}`);
};
export const capitalizeFirstLetter = (string) => {
    return string.charAt(0).toUpperCase() + string.slice(1);
};
export const toUrlFilter = (filter) => filter
    .filter(({ tags }) => tags.length)
    .map(({ label, tags }) => `${label}:${tags.join(',')}`)
    .join(';') || undefined;
export const fromUrlFilter = (urlParam = '') => urlParam
    ? urlParam
        .split(';')
        .map(part => part.split(':'))
        .map(([label, tags]) => ({ label, tags: tags.split(',') }))
    : [];
export const chunkArray = (array, chunkSize) => range(0, Math.ceil(array.length / chunkSize)).map(i => array.slice(i * chunkSize, (i + 1) * chunkSize));
