import ms from 'ms';
import { pastDate } from '../utils.js';
const parseCollection = (config) => (collection) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41;
    const workitems = {
        label: (_d = (_b = (_a = collection.workitems) === null || _a === void 0 ? void 0 : _a.label) !== null && _b !== void 0 ? _b : (_c = config.azure.workitems) === null || _c === void 0 ? void 0 : _c.label) !== null && _d !== void 0 ? _d : 'Features & Bugs',
        getWorkItems: (_h = (_f = (_e = collection.workitems) === null || _e === void 0 ? void 0 : _e.getWorkItems) !== null && _f !== void 0 ? _f : (_g = config.azure.workitems) === null || _g === void 0 ? void 0 : _g.getWorkItems) !== null && _h !== void 0 ? _h : ['Feature', 'Bug'],
        groupUnder: (_m = (_k = (_j = collection.workitems) === null || _j === void 0 ? void 0 : _j.groupUnder) !== null && _k !== void 0 ? _k : (_l = config.azure.workitems) === null || _l === void 0 ? void 0 : _l.groupUnder) !== null && _m !== void 0 ? _m : ['Feature', 'Bug'],
        skipChildren: (_r = (_p = (_o = collection.workitems) === null || _o === void 0 ? void 0 : _o.skipChildren) !== null && _p !== void 0 ? _p : (_q = config.azure.workitems) === null || _q === void 0 ? void 0 : _q.skipChildren) !== null && _r !== void 0 ? _r : ['Test Case', 'Test Scenario'],
        environmentField: (_t = (_s = collection.workitems) === null || _s === void 0 ? void 0 : _s.environmentField) !== null && _t !== void 0 ? _t : (_u = config.azure.workitems) === null || _u === void 0 ? void 0 : _u.environmentField,
        ignoredWorkItemsForFlowAnalysis: (_w = (_v = collection.workitems) === null || _v === void 0 ? void 0 : _v.ignoredWorkItemsForFlowAnalysis) !== null && _w !== void 0 ? _w : (_x = config.azure.workitems) === null || _x === void 0 ? void 0 : _x.ignoredWorkItemsForFlowAnalysis,
        ignoreStates: (_z = (_y = collection.workitems) === null || _y === void 0 ? void 0 : _y.ignoreStates) !== null && _z !== void 0 ? _z : (_0 = config.azure.workitems) === null || _0 === void 0 ? void 0 : _0.ignoreStates,
        filterBy: (((_1 = collection.workitems) === null || _1 === void 0 ? void 0 : _1.filterBy) ||
            ((_2 = config.azure.workitems) === null || _2 === void 0 ? void 0 : _2.filterBy) ||
            []).map(filter => ({
            label: filter.label,
            fields: Array.isArray(filter.field) ? filter.field : [filter.field],
            delimiter: filter.delimiter,
        })),
        types: (_6 = ((_4 = (_3 = collection.workitems) === null || _3 === void 0 ? void 0 : _3.types) !== null && _4 !== void 0 ? _4 : (_5 = config.azure.workitems) === null || _5 === void 0 ? void 0 : _5.types)) === null || _6 === void 0 ? void 0 : _6.map(type => ({
            type: type.type,
            groupByField: type.groupByField,
            groupLabel: type.groupLabel,
            startDate: Array.isArray(type.startDate)
                ? type.startDate
                : [type.startDate || 'System.CreatedDate'],
            endDate: Array.isArray(type.endDate)
                ? type.endDate
                : [type.endDate || 'Microsoft.VSTS.Common.ClosedDate'],
            devCompletionDate: Array.isArray(type.devCompletionDate)
                ? type.devCompletionDate
                : type.devCompletionDate
                    ? [type.devCompletionDate]
                    : [],
            rootCause: Array.isArray(type.rootCause)
                ? type.rootCause
                : type.rootCause
                    ? [type.rootCause]
                    : [],
            track: type.track,
            workCenters: (type.workCenters || []).map(workCenter => ({
                label: workCenter.label,
                startDate: 'startDate' in workCenter
                    ? Array.isArray(workCenter.startDate)
                        ? workCenter.startDate
                        : [workCenter.startDate]
                    : ['System.CreatedDate'],
                endDate: 'endDate' in workCenter
                    ? Array.isArray(workCenter.endDate)
                        ? workCenter.endDate
                        : [workCenter.endDate]
                    : ['Microsoft.VSTS.Common.ClosedDate'],
            })),
        })),
    };
    const changeProgram = collection.changeProgram || config.azure.changeProgram
        ? {
            name: (_10 = (_8 = (_7 = collection.changeProgram) === null || _7 === void 0 ? void 0 : _7.name) !== null && _8 !== void 0 ? _8 : (_9 = config.azure.changeProgram) === null || _9 === void 0 ? void 0 : _9.name) !== null && _10 !== void 0 ? _10 : 'Change program',
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            workItemTypeName: ((_12 = (_11 = collection.changeProgram) === null || _11 === void 0 ? void 0 : _11.workItemTypeName) !== null && _12 !== void 0 ? _12 : (_13 = config.azure.changeProgram) === null || _13 === void 0 ? void 0 : _13.workItemTypeName),
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            teamNameField: ((_15 = (_14 = collection.changeProgram) === null || _14 === void 0 ? void 0 : _14.teamNameField) !== null && _15 !== void 0 ? _15 : (_16 = config.azure.changeProgram) === null || _16 === void 0 ? void 0 : _16.teamNameField),
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            themeNameField: ((_18 = (_17 = collection.changeProgram) === null || _17 === void 0 ? void 0 : _17.themeNameField) !== null && _18 !== void 0 ? _18 : (_19 = config.azure.changeProgram) === null || _19 === void 0 ? void 0 : _19.themeNameField),
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            startedState: ((_21 = (_20 = collection.changeProgram) === null || _20 === void 0 ? void 0 : _20.startedState) !== null && _21 !== void 0 ? _21 : (_22 = config.azure.changeProgram) === null || _22 === void 0 ? void 0 : _22.startedState),
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            doneState: ((_24 = (_23 = collection.changeProgram) === null || _23 === void 0 ? void 0 : _23.doneState) !== null && _24 !== void 0 ? _24 : (_25 = config.azure.changeProgram) === null || _25 === void 0 ? void 0 : _25.doneState),
            ignoreStates: (_29 = (_27 = (_26 = collection.changeProgram) === null || _26 === void 0 ? void 0 : _26.ignoreStates) !== null && _27 !== void 0 ? _27 : (_28 = config.azure.changeProgram) === null || _28 === void 0 ? void 0 : _28.ignoreStates) !== null && _29 !== void 0 ? _29 : [],
            plannedStartDateField: (_31 = (_30 = collection.changeProgram) === null || _30 === void 0 ? void 0 : _30.plannedStartDateField) !== null && _31 !== void 0 ? _31 : (_32 = config.azure.changeProgram) === null || _32 === void 0 ? void 0 : _32.plannedStartDateField,
            plannedCompletionDateField: (_34 = (_33 = collection.changeProgram) === null || _33 === void 0 ? void 0 : _33.plannedCompletionDateField) !== null && _34 !== void 0 ? _34 : (_35 = config.azure.changeProgram) === null || _35 === void 0 ? void 0 : _35.plannedCompletionDateField,
            actualStartDateField: (_37 = (_36 = collection.changeProgram) === null || _36 === void 0 ? void 0 : _36.actualStartDateField) !== null && _37 !== void 0 ? _37 : (_38 = config.azure.changeProgram) === null || _38 === void 0 ? void 0 : _38.actualStartDateField,
            actualCompletionDateField: (_40 = (_39 = collection.changeProgram) === null || _39 === void 0 ? void 0 : _39.actualCompletionDateField) !== null && _40 !== void 0 ? _40 : (_41 = config.azure.changeProgram) === null || _41 === void 0 ? void 0 : _41.actualCompletionDateField,
        }
        : null;
    return {
        name: collection.name,
        workitems,
        changeProgram,
        projects: collection.projects.map(project => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5;
            if (typeof project === 'string') {
                return {
                    name: project,
                    releasePipelines: (_b = (_a = collection.releasePipelines) !== null && _a !== void 0 ? _a : config.azure.releasePipelines) !== null && _b !== void 0 ? _b : { stagesToHighlight: [] },
                    workitems: {
                        groupUnder: workitems.groupUnder,
                        label: workitems.label,
                        ignoreForWIP: (_f = (_d = (_c = collection.workitems) === null || _c === void 0 ? void 0 : _c.ignoreForWIP) !== null && _d !== void 0 ? _d : (_e = config.azure.workitems) === null || _e === void 0 ? void 0 : _e.ignoreForWIP) !== null && _f !== void 0 ? _f : [],
                    },
                    environments: (_g = collection.environments) !== null && _g !== void 0 ? _g : config.azure.environments,
                    templateRepoName: (_h = collection.templateRepoName) !== null && _h !== void 0 ? _h : config.azure.templateRepoName,
                    branchPolicies: (_k = (_j = collection.branchPolicies) !== null && _j !== void 0 ? _j : config.azure.branchPolicies) !== null && _k !== void 0 ? _k : {},
                };
            }
            return {
                name: project.name,
                releasePipelines: (_o = (_m = (_l = project.releasePipelines) !== null && _l !== void 0 ? _l : collection.releasePipelines) !== null && _m !== void 0 ? _m : config.azure.releasePipelines) !== null && _o !== void 0 ? _o : { stagesToHighlight: [] },
                workitems: {
                    groupUnder: (_q = (_p = project.workitems) === null || _p === void 0 ? void 0 : _p.groupUnder) !== null && _q !== void 0 ? _q : workitems.groupUnder,
                    label: (_s = (_r = project.workitems) === null || _r === void 0 ? void 0 : _r.label) !== null && _s !== void 0 ? _s : workitems.label,
                    ignoreForWIP: (_y = (_w = (_u = (_t = project.workitems) === null || _t === void 0 ? void 0 : _t.ignoreForWIP) !== null && _u !== void 0 ? _u : (_v = collection.workitems) === null || _v === void 0 ? void 0 : _v.ignoreForWIP) !== null && _w !== void 0 ? _w : (_x = config.azure.workitems) === null || _x === void 0 ? void 0 : _x.ignoreForWIP) !== null && _y !== void 0 ? _y : [],
                },
                environments: (_0 = (_z = project.environments) !== null && _z !== void 0 ? _z : collection.environments) !== null && _0 !== void 0 ? _0 : config.azure.environments,
                groupRepos: project.groupRepos,
                templateRepoName: (_2 = (_1 = project.templateRepoName) !== null && _1 !== void 0 ? _1 : collection.templateRepoName) !== null && _2 !== void 0 ? _2 : config.azure.templateRepoName,
                branchPolicies: (_5 = (_4 = (_3 = project.branchPolicies) !== null && _3 !== void 0 ? _3 : collection.branchPolicies) !== null && _4 !== void 0 ? _4 : config.azure.branchPolicies) !== null && _5 !== void 0 ? _5 : {},
            };
        }),
    };
};
export default (config) => {
    var _a;
    return ({
        port: config.port,
        mongoUrl: config.mongoUrl,
        cacheTimeMs: ms(config.cacheToDiskFor),
        azure: {
            host: config.azure.host,
            token: config.azure.token,
            verifySsl: (_a = config.azure.verifySsl) !== null && _a !== void 0 ? _a : true,
            queryFrom: pastDate(config.azure.lookAtPast),
            collections: config.azure.collections.map(parseCollection(config)),
            summaryPageGroups: config.azure.summaryPageGroups,
        },
        sonar: Array.isArray(config.sonar) ? config.sonar : config.sonar ? [config.sonar] : [],
    });
};
