/*
 * 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 ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-methods"};ruleDef.prototype.getShortDescription=function(){return"Checks for the existence of declared methods"};ruleDef.prototype.getDescription=function(){return"This rule ensures that any methods declared in the component.json actually have an implementation in the associated viewModel"};ruleDef.prototype.register=function(context){return{FunctionExpression:Utils.setupFileMethodMapping.bind(Utils),ArrowFunctionExpression:Utils.setupFileMethodMapping.bind(Utils),MethodDefinition:Utils.setupFileMethodMappingFromMethodDefintion.bind(Utils),CallExpression:Utils.deriveViewModels.bind(Utils),closedownRP:this._doAudit}};ruleDef.prototype._doAudit=function(context){let issue;if(context.rulePack.getExtension()){context.rulePack.getExtension().allComponents.forEach(compInfo=>{if(compInfo.methods){let reportSource=compInfo.fullName;if(compInfo.viewModel.resolved&&!compInfo.viewModel.skip){reportSource=compInfo.viewModel.path}const vmMethods=Utils.getVMMethodsForLocalComposite(context,compInfo);if(vmMethods){Object.keys(compInfo.methods).forEach(methodName=>{const methodMeta=compInfo.methods[methodName];const testName=methodMeta.internalName?methodMeta.internalName:methodName;if(!vmMethods.has(testName)){issue=new context.Issue(`Component '${compInfo.fullName}' declares a API method '${methodName}' which does not have an implementation in its viewModel file`);issue.setMsgKey(context.ruleName+"_1");issue.setReportedFilePath(reportSource);context.reporter.addIssue(issue,context)}else{const implementedParamCount=vmMethods.get(testName).parameters;const declaredParamCount=methodMeta.params?methodMeta.params.length:0;if(implementedParamCount!==declaredParamCount){issue=new context.Issue(`The implementation of Component '${compInfo.fullName}' API method '${testName}' does not match the declared API.  The parameter lists differ in length`);issue.setMsgKey(context.ruleName+"_2");issue.setReportedFilePath(reportSource);context.reporter.addIssue(issue,context)}}})}}})}};module.exports=ruleDef;