/*
 * 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 RULE_NAME="oj-html-attr-req";const SHORT_DESCR="Check that a webcomponent element contains all mandatory attributes";const LONG_DESCR="A Web Component attribute which is mandatory, must be declared on the web component element.";const DOUBLE_BRACE="{{";const DOUBLE_BRACKET="[[";const DOT=".";function getName(){return RULE_NAME};function getShortDescription(){return SHORT_DESCR};function getDescription(){return LONG_DESCR};function register(){return{webcomp:_fnTag}};function _fnTag(ruleCtx,tagName){var node,req,attribs,attrib,attrReq,sub,val,i,x,tmp,issue,n,found;req=ruleCtx.utils.metaLib.getRequiredAttrs(tagName);if(!req||!req.length){return}node=ruleCtx.tagNode;if(!ruleCtx.utils.utils.hasAnyProps(node.attribs)){val=req.length+" required attribute"+(req.length>1?"s":"")+": ";for(i=0;i<req.length;i++){val+=i?", ":"";val+="'"+req[i]+"'"}val+=" missing on <"+tagName+">";issue=new ruleCtx.Issue(val);ruleCtx.reporter.addIssue(issue,ruleCtx);return}attribs=node.attribs;n=0;_checkAttrs:for(i=0;i<req.length;i++){attrReq=req[i];found=false;for(attrib in attribs){attrib=attrib.charAt(0)===":"?attrib.substring(1):attrib;if(attrib===attrReq){n++;found=true;break}}if(n===req.length){return}if(found){continue}for(attrib in attribs){if((x=attrib.indexOf(DOT))>0){if(req.includes(attrib.substring(0,x))){n++;found=true;continue _checkAttrs}}}tmp=attrReq;while((x=tmp.lastIndexOf(DOT))>0){sub=tmp=tmp.substring(0,x);val=_isAttrMatch(node.attribs,sub);if(val){if(_isExpr(val)){return}if(_isJson(val)){if(_resolveWithObj(attrReq,sub,val)){return}}else{break}}}issue=new ruleCtx.Issue(`Required attribute '${attrReq}' is missing on <${tagName}>`);issue._msgEx={reqAttr:attrReq};ruleCtx.reporter.addIssue(issue,ruleCtx)}};function _isAttrMatch(attribs,attr){var attrib,ret=false;for(attrib in attribs){attrib=attrib.charAt(0)===":"?attrib.substring(1):attrib;if(attrib===attr){ret=attribs[attrib];break}}return ret};function _resolveWithObj(attrReq,sub,json){var obj,prop;try{obj=JSON.parse(json);for(prop in obj){if(attrReq===sub+"."+prop)return true}}catch(e){}return false};function _isExpr(s){s=s.trim();return s.startsWith("[[")||s.startsWith("{{")};function _isJson(s){var start=s.charAt(0);return(start==="{"||start!=="[")&&!_isExprStart(s)};function _isExprStart(s){return s.startsWith(DOUBLE_BRACKET)||s.startsWith(DOUBLE_BRACE)};module.exports={getName,getDescription,getShortDescription,register};