/*
 * Copyright (c) 2018, 2026, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const RULE_NAME="oj-html-cca-count";const SHORT_DESCRIPT="Pages should not use an excessive number of web components";const LONG_DESCRIPT="For optimal performance, a page should not contain an excessive number of web components, "+"since this can negatively affect the loading time of the page.  Refer to the Oracle JET "+"Toolkit Documention, section \"Add Performance Optimization to an Oracle JET Application\""+"<br>The threshold values are defined in the options for rule 'oj-html-cca-count'.";function Rule(){this._$total=0;this._thresholds=null};Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return SHORT_DESCRIPT};Rule.prototype.getDescription=function(){return LONG_DESCRIPT};Rule.prototype.register=function(regCtx){return this._getRuleThresholds(regCtx)?{"file":this._onFile}:false};Rule.prototype._onFile=function(ruleCtx){var tags=ruleCtx.utils.DomUtils.getComponentElems();var compCount=0;var tagName,threshTag;if(this._$total){for(tagName in tags){tagName=tags[tagName].name;if(tagName.toLowerCase().startsWith("oj-")){if(tagName!=="oj-option"&&tagName!=="oj-label"){compCount++}}}}for(threshTag in this._thresholds){this._checkComponentThreshold(ruleCtx,tags,threshTag)}if(compCount&&compCount>this._$total){let issue=new ruleCtx.Issue(`Performance - page exceeds recommended total threshold count (${this._$total}) of web components by ${compCount-this._$total}`);issue.setMsgKey(RULE_NAME+"_1");ruleCtx.reporter.addIssue(issue,ruleCtx)}};Rule.prototype._checkComponentThreshold=function(ruleCtx,tags,tagName){var tagThreshold,tagCount,issue;tagThreshold=this._thresholds[tagName];if(tagThreshold!==undefined){tagCount=_count(tags,tagName);if(tagCount>tagThreshold){issue=new ruleCtx.Issue(`Page exceeds recommended web component threshold count of ${tagThreshold} for <${tagName}> by ${tagCount-tagThreshold}`);ruleCtx.reporter.addIssue(issue,ruleCtx)}}};function _count(tags,tagName){var count=0,regex;if(tagName.indexOf("*")>=0){regex=new RegExp("^"+tagName.split("*").join(".*")+"$");tags.forEach(function(x){count+=regex.test(x.name)?1:0})}else{tags.forEach(function(x){count+=x.name===tagName?1:0})}return count};Rule.prototype._getRuleThresholds=function(regCtx){var opts,prop,n,bHaveThreshold=false;if(opts=regCtx.ruleOpts.customOpts){if(opts=opts.thresholds){if(typeof opts==="object"){this._thresholds={};for(prop in opts){n=opts[prop];if(isNaN(n)||n<0){continue}if(prop==="$total"){this._$total=n}else{this._thresholds[prop]=n}bHaveThreshold=true}}else{regCtx.utils.msgLib.error("Rule oj-html-cca-count registration : rule override 'thresholds' is  not an object.")}}}return bHaveThreshold};module.exports=Rule;