"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyTransformations = void 0;
const index_1 = require("../index");
const pre_ua_applicator_1 = require("./pre-ua-applicator");
const on_ua_applicator_1 = require("./on-ua-applicator");
const supportedOperations = new Set(Object.values(index_1.Experimentation.Operations));
function applyTransformations(rewriter, transformations) {
    // Apply Pre-UA transformations.
    let clientTransformCount = 0;
    for (const [flags, selector, op, ...rest] of transformations) {
        if (!supportedOperations.has(op))
            continue;
        if (flags & index_1.Experimentation.Flags.PreUA) {
            rewriter.on(selector, new pre_ua_applicator_1.default(op, ...rest));
        }
        if (flags & index_1.Experimentation.Flags.OnUA)
            clientTransformCount++;
    }
    // If On-UA transforms exist, involve ClientTransformApplicator
    // that serializes all ON_UA transforms + applicator script into
    // HEAD of the document.
    if (clientTransformCount) {
        rewriter.on('head', new on_ua_applicator_1.default(transformations));
    }
    return rewriter;
}
exports.applyTransformations = applyTransformations;
