/*
 * 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 TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-schema"};ruleDef.prototype.getShortDescription=function(){return"Validate component json definition against the schema"};ruleDef.prototype.getDescription=function(){return"This rule checks verifies the component.json file included by or generated from the component against the schema supplied for this purpose"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context,obj){let issue;if(context.suppData.obj){const jsonToValidate=obj.obj.component?obj.obj.component:obj.obj;const schemaErrors=context.utils.metaLib.applySchema(jsonToValidate);if(schemaErrors){schemaErrors.forEach(error=>{issue=new context.Issue(`component.json schema violation: ${this._formatViolation(error)}`);context.reporter.addIssue(issue,context)})}}};ruleDef.prototype._doVComponentAudit=function(context){let lenient=context.rulePack.getRuleCustomOption("ignoreAdditionalProperties")??false;const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo&&componentInfo.compiledMetadata){const jsonToValidate=componentInfo.compiledMetadata;const schemaErrors=context.utils.metaLib.applySchema(jsonToValidate);if(schemaErrors){const encounteredErrors=new Set;schemaErrors.forEach(report=>{const skip=lenient&&report.error&&report.error.keyword==="additionalProperties";if(!skip){const formattedError=this._formatViolation(report);if(!encounteredErrors.has(formattedError)){encounteredErrors.add(formattedError);issue=new context.Issue(`Generated component.json has schema violation: ${formattedError}`);context.reporter.addIssue(issue,context)}}})}}};ruleDef.prototype._formatViolation=function(report){let formattedMsg;switch(report.error.keyword){case"required":const jsonErrorPath=report.error.dataPath?report.error.dataPath:report.error.instancePath;const formattedPath=this._transformViolationDataPath(jsonErrorPath);formattedMsg=`${formattedPath} ${report.msg}`;break;default:formattedMsg=report.msg}return formattedMsg};ruleDef.prototype._transformViolationDataPath=function(dataPath){let formatted=dataPath;;const parser=/\.(\w*)\['(\w*)'\]/g;const segparser=/\.(\w*)\['(\w*)'\]/;const instancePathParser=/\/(\w*)\/(\w*)/g;const instancePathSegParser=/\/(\w*)\/(\w*)/;let segments=dataPath.match(parser);let deconstructed;if(segments!==null){deconstructed=segments.map(seg=>{subParts=seg.match(segparser);return{segment:subParts[0],type:subParts[1],context:subParts[2]}})}else{segments=dataPath.match(instancePathParser);deconstructed=segments.map(seg=>{subParts=seg.match(instancePathSegParser);return{segment:subParts[0],type:subParts[1],context:subParts[2]}})}if(deconstructed&&deconstructed.length>0){if(deconstructed[0].type==="properties"){formatted=`Property ${deconstructed[0].context}`;if(deconstructed.length>1){for(let i=1;i<deconstructed.length;i++){if(deconstructed[i].type==="properties"){formatted+=`.${deconstructed[i].context}`}else{formatted+=` ${deconstructed[i].type}:${deconstructed[i].context}`}}}}}return formatted};module.exports=ruleDef;