/*
 * 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 reMs=new RegExp(/\s+/,"g");const reInsertSp=new RegExp(/([\,\>])/,"g");const DOT=".";const COMMA=",";const GT=">";const STAR="*";const COLON_CLASS=":class";const OJ_BIND_FOR_EACH="oj-bind-for-each";const OJ_BIND_IF="oj-bind-if";const TEMPLATE="template";const SCRIPT="script";var _assertMsg;var _breakingTag=null;function match(ruleCtx,ss){var a,isGroup;if(Array.isArray(ss)){ss=ss.join(COMMA)}ss=_prepSS(ss);isGroup=ss.indexOf(COMMA)>0;a=ss.split(" ");if(a.length===0){return true}return a.length>1?_multi(ruleCtx,ss,a,isGroup):_single(ruleCtx,a[0])};function _single(ruleCtx,s){var elem,a,i;elem=ruleCtx.tag;if(s.charAt(0)===DOT){if(s.includes(DOT,1)){a=s.split(DOT);for(i=0;i<a.length;i++){if(!_testStyleOnTag(ruleCtx.tagNode.attribs,a[i])){return false}}return true}return _testStyleOnTag(ruleCtx.tagNode.attribs,s)}else{if(!s.includes(DOT,1)){return s===elem||s===STAR}a=s.split(DOT);if(a.length===2){return(elem===a[0]||elem===STAR)&&_testStyleOnTag(ruleCtx.tagNode.attribs,a[1])}_assertMsg(`UNHANDLED SS1 -> ${s} - '${ruleCtx.filepath}'`);return false}};function _multi(ruleCtx,ss,a,isGroup){if(isGroup){return _handleMultiGroup(ruleCtx,ss)}if(a.length===2){if(!a[0].startsWith(DOT)&&!a[1].startsWith(DOT)){if(a[0].includes(DOT,1)){if(ruleCtx.tag!==a[1]){return false}let s=a[0].split(DOT);return _findTagWithChainStyle(ruleCtx,ss,a)}if(a[1]!==STAR){if(ruleCtx.tag!==a[1]){return false}}return _findChildElem(ruleCtx,a[0]);_assertMsg(`UNHANDLED SS2 MULTI -> ${a.join(" ")} - '${ruleCtx.filepath}'`);return false}if(a[0].startsWith(DOT)&&!a[1].startsWith(DOT)){return(ruleCtx.tag===a[1]||a[1]===STAR)&&_findTagWithStyle(ruleCtx,"*",a[0])}else if(!a[0].startsWith(DOT)&&a[1].startsWith(DOT)){return _testStyleOnTag(ruleCtx.tagNode.attribs,a[1])&&_findParentTag(ruleCtx,a[0])}}if(a.length===3&&a[1]===GT){if(a[0].startsWith(DOT)&&!a[2].startsWith(DOT)){return(ruleCtx.tag===a[2]||a[2]===STAR)&&_findTagWithStyle(ruleCtx,"*",a[0],true)}else if(!a[0].startsWith(DOT)&&a[2].startsWith(DOT)){return _testStyleOnTag(ruleCtx.tagNode.attribs,a[0])&&_findParentTag(ruleCtx,a[2],true)}else if(!a[0].startsWith(DOT)&&!a[2].startsWith(DOT)){if(ruleCtx.tagNode.name===a[2]||a[2]===STAR){return _findChildElem(ruleCtx,a[0],true)}return false}_assertMsg(`UNHANDLED SS3 MULTI -> ${a.join(" ")} - '${ruleCtx.filepath}'`);return false}_assertMsg(`"UNHANDLED SS4 MULTI -> ${a.join(" ")} - '${ruleCtx.filepath}'`)};function _handleMultiGroup(ruleCtx,ss){var a,i;a=ss.split(COMMA);for(i=0;i<a.length;i++){a[i]=a[i].trim();if(match(ruleCtx,a[i])){return true}}return false};function _testStyleOnTag(attribs,style){var a,i;if(attribs){a=attribs.class||attribs[COLON_CLASS];if(!a){return false}if(_isExpr(a)){return true}a=a.replace(reMs," ");a=a.split(" ");style=style.charAt(0)===DOT?style.substring(1):style;for(i=0;i<a.length;i++){if(a[i]===style){return true}}}return false};function _findTagWithStyle(ruleCtx,tag,style,direct){return _walkElemStackForTag(ruleCtx,tag,function(o){return _testStyleOnTag(o.attribs,style)},direct)};function _findTagWithChainStyle(ruleCtx,ss,a){return _walkElemStackForTag(ruleCtx,a[0],function(o){return _testStyleOnTag(o.attribs,a[1])},false)};function _findParentTag(ruleCtx,tag,direct){return _walkElemStackForTag(ruleCtx,tag,function(o){return true},direct)};function _findChildElem(ruleCtx,elem,direct){return _walkElemStackForTag(ruleCtx,elem,function(o){return o.name===elem||o.name===SCRIPT},direct)};function _walkElemStackForTag(ruleCtx,tag,cb,direct){var i,b,elem,name;var stack=ruleCtx.elemStack;_breakingTag=null;if(stack&&stack.length){b=false;for(i=stack.length-2;i>=0;i--){if(direct&&b){break}elem=stack[i];name=elem.name;if(name===OJ_BIND_IF||name===OJ_BIND_FOR_EACH||name===TEMPLATE){continue}if(elem.name===tag||tag===STAR||elem.name===SCRIPT){if(cb(elem)){_breakingTag=name;return true}}b=true}}return false};function _isExpr(s){var c=s.charAt(0);return c==="["||c==="{"};function _prepSS(ss){ss=ss.replace(reInsertSp," $1").trim();return ss.replace(reMs," ")};function setAssertMsg(fn){_assertMsg=fn};module.exports.match=match;module.exports.setAssertMsg=setAssertMsg;