import { isRequireMergeStrategyPolicy, isCommentRequirementsPolicy, isBuildPolicy, isWorkItemLinkingPolicy, isMinimumNumberOfReviewersPolicy, } from '../../models/policy-configuration.js';
export default (policyConfigurations) => {
    const policyConfigByRepoAndBranch = policyConfigurations.reduce((acc, policyConfiguration) => {
        if (policyConfiguration.isDeleted)
            return acc;
        if (!policyConfiguration.isEnabled)
            return acc;
        const addBranchPolicy = (repoId, branch, policy) => {
            acc[repoId] = acc[repoId] || {};
            acc[repoId][branch] = acc[repoId][branch] || {};
            acc[repoId][branch] = { ...acc[repoId][branch], ...policy };
        };
        if (isMinimumNumberOfReviewersPolicy(policyConfiguration)) {
            addBranchPolicy(policyConfiguration.repositoryId, policyConfiguration.refName, {
                minimumNumberOfReviewers: {
                    count: policyConfiguration.settings.minimumApproverCount,
                    isOptional: !policyConfiguration.isBlocking,
                },
            });
        }
        else if (isWorkItemLinkingPolicy(policyConfiguration)) {
            addBranchPolicy(policyConfiguration.repositoryId, policyConfiguration.refName, {
                workItemLinking: { isOptional: !policyConfiguration.isBlocking },
            });
        }
        else if (isBuildPolicy(policyConfiguration)) {
            addBranchPolicy(policyConfiguration.repositoryId, policyConfiguration.refName, {
                builds: { isOptional: !policyConfiguration.isBlocking },
            });
        }
        else if (isCommentRequirementsPolicy(policyConfiguration)) {
            addBranchPolicy(policyConfiguration.repositoryId, policyConfiguration.refName, {
                commentRequirements: { isOptional: !policyConfiguration.isBlocking },
            });
        }
        else if (isRequireMergeStrategyPolicy(policyConfiguration)) {
            addBranchPolicy(policyConfiguration.repositoryId, policyConfiguration.refName, {
                requireMergeStrategy: { isOptional: !policyConfiguration.isBlocking },
            });
        }
        return acc;
    }, {});
    return (repoId, branch) => { var _a; return ((_a = policyConfigByRepoAndBranch[repoId]) === null || _a === void 0 ? void 0 : _a[branch]) || {}; };
};
