/*
 * 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 cssHelper=require("./helpers/cssHelper");const RULE_NAME="oj-html-style-bp-color";const SHORT_DESCRIPT="For best practices, CSS color values should not be used - color classes such as 'oj-text-color-primary' are preferred.";const DESCRIPT="For best practices, CSS color values should not be used - color classes such as 'oj-text-color-primary' are preferred."+" See the Colors demo category in the JET cookbook under the Framework "+"<a href='https://www.oracle.com/webfolder/technetwork/jet/jetCookbook.html?component=colors&demo=bgcolors'>Utility CSS</a> category.";function Rule(){this._checkList=null;this._isAccept=false};Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return SHORT_DESCRIPT};Rule.prototype.getDescription=function(){return DESCRIPT};Rule.prototype.register=function(regCtx){if(!this._checkAcceptReject(regCtx)){return false}return{"style":this._onStyle,"stylemember":this._onStyle}};Rule.prototype._onStyle=function(ruleCtx,attr,value){var prop,val,s,x,i;this._CssUtils=ruleCtx.utils.CssUtils;if(ruleCtx.type==="stylemember"){i=attr.charAt(0)===":"?7:6;prop=attr.substring(i);this._checkValue(ruleCtx,prop,value)}else{for(i=0;i<value.length;i++){s=value[i];x=s.indexOf(":");if(x>=0){prop=s.substring(0,x).trimEnd();val=s.substring(x+1).trimStart();this._checkValue(ruleCtx,prop,val)}}}};Rule.prototype._checkValue=function(ruleCtx,prop,value){if(cssHelper.COLOR_PROPS.includes(prop)||cssHelper.COLOR_MEMBERS.includes(prop)){if(this._isColorValFound(value)){if(this._okProp(prop)){this._emitIssue(ruleCtx,prop,value)}}}else if(cssHelper.COLOR_PROPS_MULTI.includes(prop)||cssHelper.COLOR_MEMBERS_MULTI.includes(prop)){if(this._okProp(prop)){this._parseMulti(ruleCtx,prop,value)}}};Rule.prototype._parseMulti=function(ruleCtx,prop,val){var a,v,i;a=val.split(" ");for(i=0;i<a.length;i++){v=a[i].trim();if(v.charAt(0)==="#"||_isColorFunc(v)||this._CssUtils.isColorName(v)){this._emitIssue(ruleCtx,prop,v)}}};Rule.prototype._emitIssue=function(ruleCtx,prop,val){var issue;issue=new ruleCtx.Issue("CSS color '"+val+"' - best practices: use the utility CSS selectors available (see 'Text', 'Icons', and 'Link' pages)");ruleCtx.reporter.addIssue(issue,ruleCtx)};Rule.prototype._isColorValFound=function(s){var i;s=s.toLowerCase();if(_isColorFunc(s)||this._CssUtils.isColorName(s)){return true}i=s.indexOf("#");if(i>=0){s=s.substring(i);if(/[0-9A-F]{6}/i.test(s)||/[0-9A-F]{3}/i.test(s)){return true}}return false};Rule.prototype._okProp=function(prop){var i;if(this._checkList){for(i=0;i<this._checkList.length;i++){if(this._checkList[i]===prop){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]=this._checkList[i].trim()}}}return true};function _isColorFunc(s){s=s.toLowerCase();return s.startsWith("rgb")||s.startsWith("hsl")};module.exports=Rule;