/*
 * 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 Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-property-nesting"};ruleDef.prototype.getShortDescription=function(){return"Checks for correct property nesting depth"};ruleDef.prototype.getDescription=function(){return"This rule determines the depth at which we are are happy to see nested properties.  The default depth is 3, e.g. parent-child-grandchild. Setting to 1 would catch any use of subProperties"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){const meta=context.suppData.obj;const fullName=Utils.getComponentFullName(meta);const componentType=Utils.getComponentType(meta);const maxDepth=context.rulePack.getRuleCustomOption("depth")??1;if(componentType==="composite"&&meta.properties){this._propertyDrill(context,fullName,meta,0,maxDepth)}}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo&&componentInfo.properties){const maxDepth=context.rulePack.getRuleCustomOption("depth")??1;this._propertyDrill(context,componentInfo.fullName,componentInfo,0,maxDepth)}};ruleDef.prototype._propertyDrill=function(context,fullName,metadata,depth,maxDepth,recursion){depth++;const propKeys=Object.keys(metadata.properties);const propDepthCount=new Map;propKeys.forEach(propertyName=>{const checkProperty=metadata.properties[propertyName];if(checkProperty.hasOwnProperty("properties")){propDepthCount.set(propertyName,this._propertyDrill(context,fullName,checkProperty,depth,maxDepth,true))}else{propDepthCount.set(propertyName,depth)}if(!recursion&&propDepthCount.get(propertyName)>maxDepth){issue=new context.Issue(`Property '${propertyName}' in component ${fullName} exceeds the defined maximum level of property nesting. Max: ${maxDepth}, encountered: ${propDepthCount.get(propertyName)}`);context.reporter.addIssue(issue,context)}});let maxChild=depth;propDepthCount.forEach(depth=>{if(depth>maxChild){maxChild=depth}});return maxChild};module.exports=ruleDef;