import { exists } from '../../../shared/utils.js';
const getMinDate = (fields, workItem) => {
    const possibleEndDates = fields
        .map(field => workItem.fields[field])
        .filter(exists)
        .map(endDate => new Date(endDate).getTime());
    return possibleEndDates.length > 0
        ? new Date(Math.min(...possibleEndDates))
        : undefined;
};
const wasClosedOutsideQueryPeriod = (workItem, config, workItemConfig) => {
    const closeDate = getMinDate(workItemConfig.endDate, workItem);
    if (!closeDate)
        return false;
    return closeDate.getTime() < config.azure.queryFrom.getTime();
};
export const getOverviewData = (config, collection, workItemsForProject, byId, types, getWorkItemType, relations) => {
    const groupCache = new Map();
    const results = workItemsForProject.reduce((acc, workItem) => {
        var _a, _b, _c, _d;
        const wit = getWorkItemType(workItem);
        const workItemConfig = (_a = collection.workitems.types) === null || _a === void 0 ? void 0 : _a.find(wic => wic.type === wit.name);
        if (!workItemConfig)
            return acc;
        if (wasClosedOutsideQueryPeriod(workItem, config, workItemConfig))
            return acc;
        acc.reducedIds[workItem.id] = byId[workItem.id];
        acc.types[byId[workItem.id].typeId] = types[byId[workItem.id].typeId];
        acc.times[workItem.id] = {
            start: (_b = getMinDate(workItemConfig.startDate, workItem)) === null || _b === void 0 ? void 0 : _b.toISOString(),
            end: (_c = getMinDate(workItemConfig.endDate, workItem)) === null || _c === void 0 ? void 0 : _c.toISOString(),
            devComplete: (_d = getMinDate(workItemConfig.devCompletionDate, workItem)) === null || _d === void 0 ? void 0 : _d.toISOString(),
            workCenters: workItemConfig.workCenters
                .map(wc => {
                const wcStartDate = getMinDate(wc.startDate, workItem);
                const wcEndDate = getMinDate(wc.endDate, workItem);
                if (!wcStartDate)
                    return;
                return {
                    label: wc.label,
                    start: wcStartDate.toISOString(),
                    end: wcEndDate ? wcEndDate.toISOString() : undefined,
                };
            })
                .filter(exists),
        };
        if (workItemConfig.groupByField && workItem.fields[workItemConfig.groupByField]) {
            const groupName = workItem.fields[workItemConfig.groupByField] || 'no-group';
            const groupCacheKey = wit.name + groupName;
            if (!groupCache.has(groupCacheKey)) {
                groupCache.set(groupCacheKey, {
                    id: groupCacheKey,
                    witId: byId[workItem.id].typeId,
                    name: groupName,
                });
            }
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            const { id, ...matchingGroup } = groupCache.get(groupCacheKey);
            acc.groups[id] = matchingGroup;
            acc.reducedIds[workItem.id].groupId = id;
        }
        if (wit.name === 'Feature' && relations[workItem.id]) {
            acc.relations[workItem.id] = relations[workItem.id].filter(id => {
                var _a, _b;
                return ((_a = byId[id]) === null || _a === void 0 ? void 0 : _a.typeId) &&
                    types[(_b = byId[id]) === null || _b === void 0 ? void 0 : _b.typeId].name[0].toLowerCase().includes('bug');
            });
        }
        return acc;
    }, {
        reducedIds: {},
        types: {},
        groups: {},
        times: {},
        relations: {},
    });
    return {
        byId: results.reducedIds,
        types: results.types,
        groups: results.groups,
        times: results.times,
        relations: results.relations,
    };
};
