/*
 * 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 Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-nested-defaults"};ruleDef.prototype.getShortDescription=function(){return"Checks for incorrect definition of default values"};ruleDef.prototype.getDescription=function(){return"This rule checks that for complex properties (those with sub-properties) we do not have conflicting default values set e.g. a default on both a parent and a child."};ruleDef.prototype.register=function(context){return{compjson:this._doAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){let issue;const meta=context.suppData.obj;const fullName=Utils.getComponentFullName(meta);const componentType=Utils.getComponentType(meta);if(componentType==="composite"&&meta.properties){const expandedProperties=[];this._propertyDrill(context,meta,expandedProperties);expandedProperties.forEach(prop=>{if(prop.defaultCount>1){issue=new context.Issue(`Component ${fullName}, nested property ${prop.propertyPath} has multiple defaults defined at different levels`);context.reporter.addIssue(issue,context)}})}}};ruleDef.prototype._propertyDrill=function(context,metadata,accumulator,parentInfo){const levelPath=parentInfo?parentInfo.propertyPath+".":"";const defaultCount=parentInfo?parentInfo.defaultCount:0;const propKeys=Object.keys(metadata.properties);propKeys.forEach(propertyName=>{const propertyMeta=metadata.properties[propertyName];let runningDefault=defaultCount;if(propertyMeta.hasOwnProperty("value")){runningDefault++}const propertyInfo={propertyPath:levelPath+propertyName,defaultCount:runningDefault};if(propertyMeta.hasOwnProperty("properties")){this._propertyDrill(context,propertyMeta,accumulator,propertyInfo)}accumulator.push(propertyInfo)})};module.exports=ruleDef;