import { pipe, prop, T } from 'rambda';
import { count, incrementBy } from './reducer-utils.js';
export const noRCAValue = '(empty)';
export const noGroup = 'no-group';
export const timeDifference = ({ start, end }) => (end ? new Date(end) : new Date()).getTime() - new Date(start).getTime();
export const cycleTime = (workItemTimes) => (wi) => {
    const wit = workItemTimes(wi);
    if (!wit.start || !wit.end)
        return;
    return new Date(wit.end).getTime() - new Date(wit.start).getTime();
};
export const workCenterTime = (workItemTimes) => pipe(workItemTimes, prop('workCenters'), count(incrementBy(wc => (wc.end ? timeDifference(wc) : 0))));
export const totalCycleTime = (workItemTimes) => count(incrementBy(wi => cycleTime(workItemTimes)(wi) || 0));
export const totalWorkCenterTime = (workItemTimes) => count(incrementBy(workCenterTime(workItemTimes)));
export const flowEfficiency = (workItemTimes) => {
    const tct = totalCycleTime(workItemTimes);
    const twct = totalWorkCenterTime(workItemTimes);
    return (workItems) => {
        const totalTime = tct(workItems);
        if (totalTime === 0)
            return 0;
        return (twct(workItems) * 100) / totalTime;
    };
};
export const isWIPInTimeRange = (workItemTimes, statesToIgnore) => (isWithinTimeRange) => (wi) => {
    if (statesToIgnore === null || statesToIgnore === void 0 ? void 0 : statesToIgnore.includes(wi.state))
        return false;
    const { start, end } = workItemTimes(wi);
    if (!start)
        return false;
    if (!isWithinTimeRange(new Date(start), 'start'))
        return false;
    if (!end)
        return true;
    if (isWithinTimeRange(new Date(end), 'end'))
        return false;
    return true;
};
export const isWIP = (workItemTimes, statesToIgnore) => isWIPInTimeRange(workItemTimes, statesToIgnore)(T);
export const isBugLike = (workItemType) => workItemType.toLowerCase().includes('bug');
export const isBug = (workItemType) => isBugLike(workItemType.name[0]);
export const isNewInTimeRange = (workItemType, workItemTimes) => (isWithinTimeRange) => (wi) => isBug(workItemType(wi.typeId))
    ? isWithinTimeRange(new Date(wi.created.on))
    : Boolean(workItemTimes(wi).start) &&
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        isWithinTimeRange(new Date(workItemTimes(wi).start));
