/**
 * @see {@link RulesetsInjectorInterface}
 */ class RulesetsInjector {
    /**
     * @inheritdoc
     * @throws Error if manifest already contains ruleset with the specified ids
     * and {@link options.forceUpdate} is `false` or if manifest file or filters directory are not found.
     */ applyRulesets(generateRulesetPath, manifest, filterNames, options) {
        if (!manifest.declarative_net_request) {
            manifest.declarative_net_request = {};
        }
        if (!manifest.declarative_net_request.rule_resources) {
            manifest.declarative_net_request.rule_resources = [];
        }
        const ruleResources = manifest.declarative_net_request.rule_resources;
        for (const filterName of filterNames){
            const rulesetIndexMatch = filterName.match(/\d+/);
            if (!rulesetIndexMatch) {
                continue;
            }
            const rulesetIndex = rulesetIndexMatch[0];
            if (Array.isArray(options?.ids) && options.ids.length > 0 && !options.ids.includes(rulesetIndex)) {
                continue;
            }
            const rulesetId = `${options?.rulesetPrefix ?? RulesetsInjector.DEFAULT_RULESET_PREFIX}${rulesetIndex}`;
            const isEnabled = Array.isArray(options?.enable) && options.enable.length > 0 && options.enable.includes(rulesetIndex);
            const ruleset = {
                id: rulesetId,
                enabled: isEnabled,
                path: generateRulesetPath(rulesetId)
            };
            const existingRulesetIndex = ruleResources.findIndex(({ id })=>id === rulesetId);
            if (existingRulesetIndex !== -1) {
                if (options?.forceUpdate) {
                    ruleResources[existingRulesetIndex] = ruleset;
                } else {
                    throw new Error(`Duplicate ruleset ID: ${rulesetId}`);
                }
            } else {
                ruleResources.push(ruleset);
            }
        }
        return manifest;
    }
}
/**
     * Default prefix for ruleset id.
     */ RulesetsInjector.DEFAULT_RULESET_PREFIX = 'ruleset_';

export { RulesetsInjector };
