/*
 * 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 path=require("path");const Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-pack-children"};ruleDef.prototype.getShortDescription=function(){return"Ensure that all of the stated children of a pack are actually present and that we do not have any obvious orphans"};ruleDef.prototype.getDescription=function(){return"This rule inspects the physical structure of a pack and ensures that the declared child components are indeed present. We also check for un-registered children"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit}};ruleDef.prototype._doAudit=function(context){const componentMetadata=context.suppData.obj;if(componentMetadata){let issue;let emptyMembership=false;if(componentMetadata.type&&componentMetadata.type==="pack"){const packName=componentMetadata.name;const packRoot=path.dirname(context.filepath);if(componentMetadata.dependencies){const packMembers=Object.keys(componentMetadata.dependencies);if(packMembers.length>0){const packFileStructure=context.utils.fsUtils.readDir(packRoot);packMembers.forEach(fullName=>{if(!fullName.startsWith(packName)){issue=new context.Issue(`All members of the '${packName}' Pack should be named with the pack name prefix`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}if(!issue){const componentDir=fullName.slice(packName.length+1);const componentFolder=packFileStructure.filter(fileObj=>fileObj.name===componentDir&&!fileObj.file);if(componentFolder.length!==1){issue=new context.Issue(`Pack ${packName} dependency '${fullName}' should be defined in a folder called '${componentDir}' for referencing to work correctly`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context)}}});packFileStructure.map(child=>{if(!child.isFile){const matchesDeclaration=packMembers.filter(childComponent=>childComponent===`${packName}-${child.name}`);if(matchesDeclaration.length===0){const folderName=path.join(packRoot,child.name);const matchesComponent=Utils.matchComponentPath(context,folderName);if(matchesComponent){issue=new context.Issue(`Pack ${packName} seems to have a child component '${child.name}' that is not included in the dependencies of the pack`);issue.setMsgKey(context.ruleName+"_3");context.reporter.addIssue(issue,context,"major")}}}})}else{emptyMembership=true}}else{emptyMembership=true}if(emptyMembership){let vcomponentsPresent=false;const allVisibleComponents=context.rulePack.getExtension().allComponents;allVisibleComponents.forEach(componentInfo=>{if(componentInfo.pack&&componentInfo.pack===packName&&componentInfo.vcomponent){vcomponentsPresent=true}});if(!vcomponentsPresent){issue=new context.Issue(`Pack '${componentMetadata.name}' has no defined members`);issue.setMsgKey(context.ruleName+"_4");context.reporter.addIssue(issue,context,"major")}}}}};module.exports=ruleDef;