import { z } from 'zod';
import { configForProject } from '../config.js';
import { CombinedBranchPoliciesModel, RepoPolicyModel, } from './mongoose-models/RepoPoliciesModel.js';
export const getPolicyConfigurations = (collectionName, project) => RepoPolicyModel.find({ collectionName, project }).lean();
export const conformsToBranchPolicies = async ({ collectionName, project, repositoryId, refName, }) => {
    var _a;
    const branchPolicies = (_a = configForProject(collectionName, project)) === null || _a === void 0 ? void 0 : _a.branchPolicies;
    if (!branchPolicies)
        return;
    const match = await CombinedBranchPoliciesModel.findOne({
        collectionName,
        project,
        repositoryId,
        refName,
    }, { conforms: 1 }).lean();
    if (!match)
        return false;
    return match.conforms;
};
export const branchPoliciesInputParser = z.object({
    collectionName: z.string(),
    project: z.string(),
    repositoryId: z.string(),
    refName: z.string(),
});
export const getBranchPolicies = (options) => {
    return CombinedBranchPoliciesModel.findOne(options, { policies: 1 })
        .lean()
        .then(x => (x === null || x === void 0 ? void 0 : x.policies) || null);
};
export const isFileSizeRestrictionPolicy = (policy) => policy.type === 'File size restriction';
export const isPathLengthRestrictionPolicy = (policy) => policy.type === 'Path Length restriction';
export const isReservedNamesRestrictionPolicy = (policy) => policy.type === 'Reserved names restriction';
export const isMinimumNumberOfReviewersPolicy = (policy) => policy.type === 'Minimum number of reviewers';
export const isCommentRequirementsPolicy = (policy) => policy.type === 'Comment requirements';
export const isWorkItemLinkingPolicy = (policy) => policy.type === 'Work item linking';
export const isBuildPolicy = (policy) => policy.type === 'Build';
export const isRequiredReviewersPolicy = (policy) => policy.type === 'Required reviewers';
export const isRequireMergeStrategyPolicy = (policy) => policy.type === 'Require a merge strategy';
