const dayInMs = 24 * 60 * 60 * 1000;
const join = (ss) => ss.map(s => `'${s}'`).join(', ');
const skipChildren = (collectionConfig) => {
    var _a;
    return ((_a = collectionConfig.workitems.skipChildren) === null || _a === void 0 ? void 0 : _a.length)
        ? `AND NOT [Target].[System.WorkItemType] IN (${join(collectionConfig.workitems.skipChildren)}) `
        : '';
};
const ignoreStates = (collectionConfig) => {
    var _a;
    return ((_a = collectionConfig.workitems.ignoreStates) === null || _a === void 0 ? void 0 : _a.length)
        ? `
      AND NOT [Source].[System.State] IN (${join(collectionConfig.workitems.ignoreStates)})
      AND NOT [Target].[System.State] IN (${join(collectionConfig.workitems.ignoreStates)}) 
    `
        : '';
};
const additionalClausesForWIPWorkItems = (collectionConfig) => {
    const clause = (collectionConfig.workitems.types || [])
        .filter(type => type.startDate.length && type.endDate.length)
        .map(type => `(
      [Source].[System.WorkItemType] = '${type.type}'
      AND (${type.startDate
        .map(startDate => `[Source].[${startDate}] <> ''`)
        .join(' OR ')})
      AND ${type.endDate.map(endDate => `[Source].[${endDate}] = ''`).join(' AND ')}
    )`)
        .join(' OR ');
    return clause ? `OR ${clause}` : '';
};
export const queryForCollectionWorkItems = (queryFrom, collectionConfig) => {
    const daysToLookup = Math.round((Date.now() - queryFrom.getTime()) / dayInMs);
    return `
    SELECT [Id]
    FROM workitemLinks
    WHERE
      [Source].[System.WorkItemType] IN (${join(collectionConfig.workitems.getWorkItems)})
      AND (
        [Source].[Microsoft.VSTS.Common.StateChangeDate] >= @today-${daysToLookup}
        ${additionalClausesForWIPWorkItems(collectionConfig)}
      )
      ${skipChildren(collectionConfig)}
      ${ignoreStates(collectionConfig)}
      AND [Source].[System.TeamProject] IN (${join(collectionConfig.projects.map(p => p.name))})
      AND [Target].[System.TeamProject] IN (${join(collectionConfig.projects.map(p => p.name))})
    ORDER BY [System.CreatedDate] ASC
    MODE (MustContain)
  `;
};
