/*
 * 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";const SHORT="A 'class' style prefixed with 'oj-*' must be valid and applicable to the element on which it is declared";const LONG="A 'class' style prefixed with 'oj-*' must be a known JET style and applicable to the element on which it is declared, or defined in a styleset.";const ch=[">",","];const SS_IGNORE_WILD=["oj-ux-","oj-sample-","oj-typography-"];const SS_IGNORE_LIST=["oj-label","oj-applayout-content","oj-visited","oj-disabled","oj-enabled","oj-read-only","oj-invalid","oj-warning","oj-info","oj-confirmation","oj-required","oj-indeterminate","oj-active","oj-selected","oj-hover","oj-focus","oj-focus-only","oj-focus-highlight","oj-default","oj-draggable","oj-drag","oj-drop","oj-active-drop","oj-valid-drop","oj-invalid-drop","oj-expanded","oj-collapsed","oj-right","oj-center","oj-left","oj-start","oj-end","oj-top","oj-middle","oj-bottom","oj-full-width","oj-hicontrast","oj-component","oj-component-initnode","oj-complete","oj-animate-open","oj-animate-open-active","oj-animate-close","oj-animate-close-active","oj-animate-add","oj-animate-add-active","oj-animate-remove","oj-animate-remove-active","oj-animate-update","oj-animate-update-active"];class Rule{constructor(){this._ignore=null;this._metaLib=null}getName(){return RULE_NAME}getShortDescription(){return SHORT}getDescription(){return LONG}register(regCtx){var customOpts,i;if(regCtx.rulePack.isRuleEnabled("oj-html-stylesel")){return false}if(customOpts=regCtx.ruleOpts.customOpts){if(this._ignore=customOpts.ignore){for(i=0;i<this._ignore.length;i++){this._ignore[i]=new RegExp(this._ignore[i])}}}this._metaLib=regCtx.utils.metaLib;StyleSelector.setAssertMsg(regCtx.utils.msgLib.assert);return{"class":this._onClass}}_onClass(ruleCtx,attrName,attrValue){var tagName,i,j,classes,style,sType;tagName=ruleCtx.tag;classes=getStyles(attrValue);for(i=0;i<classes.length;i++){style=classes[i];if(this._ignore){for(j=0;j<this._ignore.length;j++){if(this._ignore[j].test(style)){break}}if(j<this._ignore.length){continue}}if(_isIgnoreListed(style)){continue}sType=this._metaLib.getStyleOrigin(style);if(!sType){if(style.startsWith("oj-")){if(!this._metaLib.isStylesetStyle(style)){this._emitIssue(1,style,tagName,ruleCtx)}continue}}if(sType==="c"){continue}this._handleStyle(ruleCtx,style,sType)}}_handleStyle(ruleCtx,style,sType){var ss,tags,tagName,i,simple;tagName=ruleCtx.tag;if(sType==="j"||sType==="b"){ss=this._metaLib.getStyleSelector(style,sType==="j"||sType==="b");if(ss){if(StyleSelector.match(ruleCtx,ss)){return true}}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"){if(ss){simple=_isSimpleElemOrStyle(ss);simple=!simple?3:simple==="e"?5:6;this._emitIssue(simple,style,tagName,ruleCtx,ss)}else if(tags){this._emitIssue(2,style,tagName,ruleCtx,tags)}return false}tags=null;ss=this._metaLib.getStyleSelector(style,false);if(ss){if(StyleSelector.match(ruleCtx,ss)){return true}}if(ss){simple=_isSimpleElemOrStyle(ss);simple=!simple?3:simple==="e"?5:6;this._emitIssue(simple,style,tagName,ruleCtx,ss);return false}return true}_emitIssue(num,style,tag,ruleCtx,extra1,extra2){var msg,ss,i;switch(num){case 1:msg=`Unknown style '${style}' found on <${tag}>`;break;case 2:msg=`'${style}' cannot be applied to <${tag}> - only ${_fmt(num,extra1)}`;break;case 3:if(Array.isArray(extra1)){ss="";for(i=0;i<extra1.length;i++){if(i){ss+=", or "}ss+="'"+extra1[i]+"'"}}else{ss="'"+extra1+"'"}msg=`'${style}' on <${tag}> is not matched by ${ss}`;break;case 5:msg=`'${style}' can only be applied to <${extra1}> - not <${tag}>`;break;case 6:msg=`'${style}' - <${tag}> must also contain style '${extra1.substring(1)}'`;break;case 7:msg=`'${style}' cannot be applied to <${tag}> : no style selector match (${extra1}) and no elem match (${_fmt(num,extra2)})`;break}if(msg){ruleCtx.reporter.addIssue(new ruleCtx.Issue(msg),ruleCtx)}}};function _isIgnoreListed(style){var ret=false,i;for(i=0;i<SS_IGNORE_WILD.length;i++){if(style.startsWith(SS_IGNORE_WILD[i])){ret=true;break}}if(!ret){ret=SS_IGNORE_LIST.includes(style)}return ret};function _isSimpleElemOrStyle(ss){const COMMA=",";const DOT=".";const SPACE=" ";const GT=">";if(Array.isArray(ss)){if(ss.length===1){ss=ss[0]}else{return null}}ss=ss.trim();if(ss.includes(COMMA)||ss.includes(DOT,1)||ss.includes(SPACE)||ss.includes(GT)){return null}return ss.startsWith(DOT)?"s":"e"};function _fmt(n,extra){if(n===2||n===7){let s="";for(let i=0;i<extra.length;i++){if(i){s+=", ";if(extra.length>=2){if(i===extra.length-1){s+="or "}}}s+="<"+extra[i]+">"}return s}return"<"+extra[0]+">"};module.exports=Rule;