/*
 * 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 RULENAME="oj-css-var-override";const DESCRIPTION="CSS Vars may be available for use on the client, but they cannot be overriden in this project.";const SHORT_DESCRIPTION="CSS Vars may be available for use on the client, but they cannot be overriden in this project.";const VAR_PREFIX="--";const COLON=":";class Rule{getName(){return RULENAME}getDescription(){return DESCRIPTION}getShortDescription(){return SHORT_DESCRIPTION}register(regCtx){if(!this._checkRuleOptions(regCtx)){return false}return{"css-var":this._onVarDecl,"style":this._onStyle}}_onVarDecl(ruleCtx,node){if(this._okProp(node)){let issue=new ruleCtx.Issue(`CSS var '${node.property}' should not be overridden`);issue.setPosition(ruleCtx.utils.CssUtils.getPosition(ruleCtx,node.loc));ruleCtx.reporter.addIssue(issue,ruleCtx)}}_onStyle(ruleCtx,attr,attrVal){var csv,issue,i,x;for(i=0;i<attrVal.length;i++){csv=attrVal[i];if(csv.startsWith(VAR_PREFIX)){x=csv.indexOf(COLON);if(x>0){csv=csv.substring(2,x).trimRight();issue=new ruleCtx.Issue(`CSS var '${csv}' in 'style' attribute should not be overridden`);issue.setMsgKey(RULENAME+"_1");ruleCtx.reporter.addIssue(issue,ruleCtx)}}}}_okProp(decl){var cvar,list,i;cvar=decl.property.substring(2);list=this._ignore||this._accept;if(!list){return true}for(i=0;i<list.length;i++){if(list[i].test(cvar)){return!!this._accept}}return this._accept?false:true}_checkRuleOptions(regCtx){var customOpts,cvars,accept,ign,b;if(!(customOpts=regCtx.ruleOpts.customOpts)){return true}if(cvars=customOpts["vars"]){if(!regCtx.utils.utils.isObject(cvars)){regCtx.utils.msgLib.error(`Rule ${regCtx.ruleName} :  option 'vars' is not an object`);this._fail=true;return false}accept=cvars.accept;ign=cvars.ignore;if(accept&&ign){regCtx.utils.msgLib.error(`Rule ${regCtx.ruleName} :  option 'vars' - sub-properties 'accept' and 'ignore are mutually exclusive`);b=true}if(!b){if(accept){this._accept=this._getOptionVars(regCtx,"accept",accept)}else if(ign){this._ignore=this._getOptionVars(regCtx,"ignore",ign)}}}return this._fail?false:!b}_getOptionVars(regCtx,prop,val){var i;if(typeof val==="string"){val=[val]}else if(!Array.isArray(val)){return null}for(i=0;i<val.length;i++){if(typeof val[i]==="string"){try{val[i]=new RegExp(val[i])}catch(e){regCtx.utils.msgLib.error(`Rule ${regCtx.ruleName} '${prop}' :  invalid reg. expression '${val[i]}' at position ${i}`);this._fail=true}}}return this._fail?null:val}}module.exports=Rule;