/*
 * 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 path=require("path");const Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const componentFinder=require("../jetwc-lib/jetwcutils-componentFinder");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-bootstrap"};ruleDef.prototype.getShortDescription=function(){return"Bootstrap processing for the JETWC rulset"};ruleDef.prototype.getDescription=function(){return"This rule is for internal use by the ruleset and should not be disabled"};ruleDef.prototype.register=function(context){this.componentRoots=context.config.components;this.auditBase=context.config.base;this.typeScriptCompile=context.config.typescript&&context.config.typescript.compile;return{startupRP:this._getAllVisibleComponents,closedownRP:this._reportDiagnostics}};ruleDef.prototype._getAllVisibleComponents=function(context){const{msgLib}=context.utils;msgLib.info(`builtinJetWcRules (${context.rulePack.getPackInfo().prefix}): ${context.rulePack.getPackInfo().version}`);const projectPathInfo=Utils.getKeyPathInfoForRulepack(context,this.auditBase);const componentRootFolder=context.rulePack.getRuleCustomOption("componentRoot")||projectPathInfo.componentRootFolder;const masterComponentList=componentFinder.findAllComponents(context,this.auditBase,projectPathInfo,this.componentRoots);if(masterComponentList&&masterComponentList.length>0){const allComponentsMap=masterComponentList.reduce((wmap,component)=>{if(!projectPathInfo.isBuiltOutput||projectPathInfo.isBuiltOutput&&!wmap.has(component.fullName)){wmap.set(component.fullName,component)}return wmap},new Map);const referenceComponents=new Map;const componentPaths=new Map;allComponentsMap.forEach(component=>{component.language=Utils.detectComponentSourceType(context,component.rootFolder,component.vcomponent);switch(component.type){case"reference":referenceComponents.set(component.fullName,component.ba);break;case"composite":case"resource":if(component.local){if(component.dependencies){Object.values(component.dependencies).forEach(dependency=>{if(allComponentsMap.has(dependency.name)){const proxyPaths=allComponentsMap.get(dependency.name).requirePaths;proxyPaths.forEach(path=>{component.requirePaths.add(path)})}else{msgLib.debug(`JETWC: missing dependency lookup for ${dependency.name} when resolving requirePaths`)}})}componentPaths.set(component.rootFolder,component.fullName)}break}});context.rulePack.setExtension({rulepackVersion:context.rulePack.getPackInfo().version,bootStrap:true,platform:Utils.isWindows?"Windows":"Unix-like",precompiled:this.typeScriptCompile,componentHome:`/${componentRootFolder}/`,auditBuildOutput:projectPathInfo.isBuiltOutput,discoveredPaths:projectPathInfo,componentRoots:this.componentRoots,allReferenceComponents:referenceComponents,allComponents:allComponentsMap,auditableComponentPaths:componentPaths,allAuditableFilePaths:new Map})}else{msgLib.error(`No components have been discovered from the JAF configuration for the ${context.rulePack.getPackInfo().prefix} ruleset to process. You will need to configure the "components" array to point at your component root folder`);Utils.disableRulePack(context)}};ruleDef.prototype._reportDiagnostics=function(context){const msgLib=context.utils.msgLib;const opts=context.rulePack.getRuleCustomOptions();const diagnostics=context.rulePack.getExtension();if(opts.debug){msgLib.msg("The custom component specific audits discovered the following components:");diagnostics.allComponents.forEach(component=>{msgLib.msg(`  ${component.fullName}`)})}if(!opts.testMode){let foundVMMapping=0;let candidateLocal=0;if(diagnostics&&diagnostics.allComponents&&diagnostics.allComponents.length>0){diagnostics.allComponents.forEach(component=>{if(component.local&&component.type==="composite"){candidateLocal++;if(component.viewModel&&component.viewModel.resolved){foundVMMapping++}}});if(foundVMMapping!==candidateLocal){msgLib.error(`Error: Audit was only able to map ${foundVMMapping} out of ${candidateLocal} components to their viewModels. Some audits will be unable to operate.`)}}}if(opts.debug&&opts.diagnosticFile&&diagnostics){msgLib.msg(`Writing JETWC diagnostics to ${opts.diagnosticFile}.json`);const dFile=Utils.toJAFPath(path.join(path.resolve("."),opts.diagnosticFile+".json"));context.utils.fsUtils.writeJsonSync(diagnostics,dFile,{spaces:2,replacer:Utils.diagnosticReplacer})}};module.exports=ruleDef;