/*
 * Copyright (c) 2018, 2025, 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 getStyles=require("./helpers/extractStylesFromClass");const StyleSelector=require("./helpers/style-selector");const RULE_NAME="oj-html-style-deprecated";const SHORT="Deprecated 'class' attributes with 'oj-' prefix should not be used.";const LONG="Deprecated 'class' attribute styles prefixed with 'oj-' should not be used.  Refer to the diagnostic for a suggested replacement.";function Rule(){this._checkList=null;this._isAccept=false};Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return SHORT};Rule.prototype.getDescription=function(){return LONG};Rule.prototype.register=function(regCtx){if(!this._checkAcceptReject(regCtx)){return false}this._metaLib=regCtx.utils.metaLib;StyleSelector.setAssertMsg(regCtx.utils.msgLib.assert);return{"class":this._onClass}};Rule.prototype._onClass=function(ruleCtx,attrName,attrValue){var i,dataBindChecked=false,classes,style,deps,dep,component,comps,styleSelMatched;if(!this._domUtils){this._domUtils=ruleCtx.utils.DomUtils}classes=getStyles(attrValue);for(i=0;i<classes.length;i++){style=classes[i];if(!dataBindChecked){component=this._domUtils.getDataBindComponent(ruleCtx);dataBindChecked=true}if(component){comps=this._metaLib.getClassesFromStyle(style);if(comps&&comps.length&&comps.indexOf(component)>=0){continue}}if(!this._OkStyle(style)){continue}if(!(deps=this._metaLib.isStyleDeprecatedEx(style))){continue}let sType=this._metaLib.getStyleOrigin(style);if(!sType){continue}for(let j=0;j<deps.length;j++){dep=deps[j];if(dep.styleselector){if(!(styleSelMatched=this._handleStyleSelector(ruleCtx,style,sType,dep.styleselector))){continue}if(dep.styleselector!==styleSelMatched){continue}}this._emitIssue(ruleCtx,dep.dep,style)}}};Rule.prototype._handleStyleSelector=function(ruleCtx,style,sType,ss){var ss,tags,tagName,i;tagName=ruleCtx.tag;if(sType==="c"){return StyleSelector.match(ruleCtx,ss)?ss:false}if(sType==="j"||sType==="b"){if(ss){if(StyleSelector.match(ruleCtx,ss)){return ss}}else{tags=this._metaLib.getNonSSStyleElems(style,true);if(tags){for(i=0;i<tags.length;i++){if(tags[i]===tagName){return true}}}}}if(sType==="j"){return false}if(ss=this._metaLib.getStyleSelector(style,false)){if(StyleSelector.match(ruleCtx,ss)){return ss}}return ss?false:true};Rule.prototype._emitIssue=function(ruleCtx,dep,style){if(dep.description&&!dep.description.endsWith(".")){dep.description=dep.description+"."}var issue=new ruleCtx.Issue("Deprecated style class '"+style+"'"+(dep.since?" (since "+dep.since+").":"")+(dep.description?" "+dep.description:""));ruleCtx.reporter.addIssue(issue,ruleCtx)};Rule.prototype._OkStyle=function(style){if(this._checkList){for(let i=0;i<this._checkList.length;i++){if(this._checkList[i].test(style)){return this._isAccept}}return!this._isAccept}return true};Rule.prototype._checkAcceptReject=function(regCtx){var customOpts,accept,reject;if(!(customOpts=regCtx.ruleOpts.customOpts)){return true}accept=customOpts.accept;reject=customOpts.reject;if(accept||reject){if(accept&&reject){regCtx.utils.msgLib.error("Rule "+RULE_NAME+":  options 'accept' and 'reject' are mutually exclusive");return false}if(accept){this._checkList=accept;this._isAccept=true}else{this._checkList=reject}if(!Array.isArray(this._checkList)){regCtx.utils.msgLib.error("Rule "+RULE_NAME+":  option '"+(accept?"accept":"reject")+"' is not an array");return false}for(let i=0;i<this._checkList.length;i++){if(typeof this._checkList[i]==="string"){this._checkList[i]=new RegExp(this._checkList[i].trim())}}}return true};module.exports=Rule;