'use strict';

var errors = require('@backstage/errors');
var util = require('./util.cjs.js');

const mapConditions = (criteria, getRule) => {
  if (util.isAndCriteria(criteria)) {
    return {
      allOf: criteria.allOf.map((child) => mapConditions(child, getRule))
    };
  } else if (util.isOrCriteria(criteria)) {
    return {
      anyOf: criteria.anyOf.map((child) => mapConditions(child, getRule))
    };
  } else if (util.isNotCriteria(criteria)) {
    return {
      not: mapConditions(criteria.not, getRule)
    };
  }
  const rule = getRule(criteria.rule);
  const result = rule.paramsSchema?.safeParse(criteria.params);
  if (result && !result.success) {
    throw new errors.InputError(`Parameters to rule are invalid`, result.error);
  }
  return rule.toQuery(criteria.params ?? {});
};
function createConditionTransformer(permissionRules) {
  const getRule = "getRuleByName" in permissionRules ? (n) => permissionRules.getRuleByName(n) : util.createGetRule(permissionRules);
  return (conditions) => mapConditions(conditions, getRule);
}

exports.createConditionTransformer = createConditionTransformer;
//# sourceMappingURL=createConditionTransformer.cjs.js.map
