import { prop } from 'rambda';
import { getChangeProgramTasks } from '../queries/change-program-tasks.js';
const createChangeProgramTask = (collectionConfig) => (wi) => {
    var _a, _b, _c, _d;
    return ({
        id: wi.id,
        title: wi.fields['System.Title'],
        url: wi.url.replace('_apis/wit/workItems', '_workitems/edit'),
        state: wi.fields['System.State'],
        project: wi.fields['System.TeamProject'],
        collection: collectionConfig.name,
        created: {
            on: new Date(wi.fields['System.CreatedDate']).toISOString(),
        },
        updated: {
            on: new Date(wi.fields['System.ChangedDate']).toISOString(),
        },
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        team: wi.fields[collectionConfig.changeProgram.teamNameField],
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        theme: wi.fields[collectionConfig.changeProgram.themeNameField],
        plannedStart: ((_a = collectionConfig.changeProgram) === null || _a === void 0 ? void 0 : _a.plannedStartDateField)
            ? wi.fields[collectionConfig.changeProgram.plannedStartDateField]
            : undefined,
        plannedCompletion: ((_b = collectionConfig.changeProgram) === null || _b === void 0 ? void 0 : _b.plannedCompletionDateField)
            ? wi.fields[collectionConfig.changeProgram.plannedCompletionDateField]
            : undefined,
        actualStart: ((_c = collectionConfig.changeProgram) === null || _c === void 0 ? void 0 : _c.actualStartDateField)
            ? wi.fields[collectionConfig.changeProgram.actualStartDateField]
            : undefined,
        actualCompletion: ((_d = collectionConfig.changeProgram) === null || _d === void 0 ? void 0 : _d.actualCompletionDateField)
            ? wi.fields[collectionConfig.changeProgram.actualCompletionDateField]
            : undefined,
    });
};
const queryName = 'change-program-tasks';
const changeProgramTasks = async (collectionConfig, getCollectionWorkItemIdsForQuery, getCollectionWorkItems) => {
    const query = getChangeProgramTasks(collectionConfig);
    const queryResult = await getCollectionWorkItemIdsForQuery(collectionConfig.name, query, queryName);
    const wis = getCollectionWorkItems(collectionConfig.name, queryResult.workItems.map(prop('id')), queryName);
    return (await wis).map(createChangeProgramTask(collectionConfig));
};
export default (getCollectionWorkItemIdsForQuery, getCollectionWorkItems) => async (collectionConfig) => {
    if (!collectionConfig.changeProgram)
        return [];
    return changeProgramTasks(collectionConfig, getCollectionWorkItemIdsForQuery, getCollectionWorkItems);
};
