/*
 * 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 RULE_NAME="oj-tsx-attr-req";const SHORT_DESCR="Check that a Web Component element contains all mandatory properties";const LONG_DESCR="A Web Component property which is mandatory, must be declared on the web component element.";function Rule(){};Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return SHORT_DESCR};Rule.prototype.getDescription=function(){return LONG_DESCR};Rule.prototype.register=function(regCtx){this._metaLib=regCtx.utils.metaLib;this._tsxUtils=regCtx.utils.tsxUtils;return{TsxRenderComponent:this._onTsxRC}};Rule.prototype._onTsxRC=function(ruleCtx,tsxRC){tsxRC.components.forEach(tsxTag=>{this._checkComp(ruleCtx,tsxTag)})};Rule.prototype._checkComp=function(ruleCtx,tsxComp){if(tsxComp.isWCTag){let tagName=tsxComp.name;let req=this._metaLib.getRequiredAttrs(tagName);if(req&&req.length){if(tsxComp.properties&&tsxComp.properties.length){this._flushReqdAttribs(ruleCtx,tsxComp,req)}else{this._flushAllReqdAttribs(ruleCtx,tsxComp,req)}}}if(tsxComp.children)tsxComp.children.forEach(child=>{this._checkComp(ruleCtx,child)})};Rule.prototype._flushAllReqdAttribs=function(ruleCtx,tsxComp,req){var val,issue,loc,i;val=req.length+" required property"+(req.length>1?"s":"")+": ";for(i=0;i<req.length;i++){val+=i?", ":"";val+="'"+req[i]+"'"}val+=" missing on <"+tsxComp.name+">";issue=new ruleCtx.Issue(val);loc=tsxComp.loc;issue.setPosition(loc.start.line,loc.start.column,tsxComp.range[0],tsxComp.range[1]);ruleCtx.reporter.addIssue(issue,ruleCtx)};Rule.prototype._flushReqdAttribs=function(ruleCtx,tsxComp,req){var tsxProps,tsxProp,aReq,reqMajor,aProp,propMajor,exprObj,name,attrReq,issue,found,i,j,k,n,issue;tsxProps=tsxComp.properties;n=0;for(i=0;i<req.length;i++){attrReq=req[i];aReq=attrReq.split(".");reqMajor=aReq[0];found=false;for(j=0;j<tsxProps.length;j++){tsxProp=tsxProps[j];if(!(name=tsxProp.name)){continue}aProp=name.split(".");propMajor=aProp[0];if(name===reqMajor){if(aReq.length===1){n++;found=true;break}if(!(exprObj=this._tsxUtils.getExpressionObject(tsxProp))){break}let soFar=exprObj;for(k=1;k<aReq.length;k++){let keys;if((keys=Object.keys(soFar))&&keys.length){if(keys.indexOf(aReq[k])>=0){soFar=soFar[aReq[k]];continue}break}}if(found=k>=aReq.length){n++};}}if(n===req.length){return}if(!found){let loc=tsxComp.loc;issue=new ruleCtx.Issue(`Required property '${attrReq}' is missing on <${tsxComp.name}>`);issue._msgEx={reqAttr:attrReq};issue.setPosition(loc.start.line,loc.start.column,tsxComp.range[0],tsxComp.range[1]);ruleCtx.reporter.addIssue(issue,ruleCtx)}}};module.exports=Rule;