/*
 * 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 TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-well-formed-description"};ruleDef.prototype.getShortDescription=function(){return"Ensure that description text is defined as complete sentences"};ruleDef.prototype.getDescription=function(){return"This rule checks that any description text defined as part of a components metadata is expressed as well formed sentences with a leading capital and terminating period / full stop"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){this._checkAllDescriptions(context,context.suppData.obj)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkAllDescriptions(context,componentInfo)}};ruleDef.prototype._checkAllDescriptions=function(context,meta){const descriptionInstances=this._gatherDescriptions(meta);descriptionInstances.forEach(({description,locator})=>{if(!this._isValidSentenceForm(description)){issue=new context.Issue(`${locator} description is not well formed. Sentences should begin with a capital and be terminated with a period / full stop.`);context.reporter.addIssue(issue,context)}})};ruleDef.prototype._gatherDescriptions=function(meta){let allDescriptions=[];if(meta.description){allDescriptions.push({description:meta.description,locator:"Component level"})}const propertyList=Utils.flattenComponentProperties(meta,"description");allDescriptions=allDescriptions.concat(propertyList.map(({key,value})=>{return{description:value,locator:`Property ${key}`}}));if(meta.events){Object.entries(meta.events).forEach(([key,value])=>{if(value?.description?.length>0){allDescriptions.push({description:value.description,locator:`Event ${key}`})}if(value?.detail){Object.entries(value.detail).forEach(([detailKey,detailValue])=>{if(detailValue?.description?.length>0){allDescriptions.push({description:detailValue.description,locator:`Event ${key} detail entry ${detailKey}`})}})}})}if(meta.methods){Object.entries(meta.methods).forEach(([key,value])=>{if(value?.description?.length>0){allDescriptions.push({description:value.description,locator:`Method ${key}`})}if(value?.params){value.params.forEach(parameter=>{if(parameter.description?.length>0){allDescriptions.push({description:parameter.description,locator:`Method ${key} ${parameter.name} parameter`})}})}})}if(meta.slots){Object.entries(meta.slots).forEach(([key,value])=>{if(value?.description?.length>0){const slotId=key.length===0?"Default slot":`Slot ${key}`;allDescriptions.push({description:value.description,locator:slotId})}if(value?.data){Object.entries(value.data).forEach(([dataKey,dataValue])=>{if(dataValue?.description?.length>0){allDescriptions.push({description:dataValue.description,locator:`Template Slot ${key} data entry ${dataKey}`})}})}})}return allDescriptions};ruleDef.prototype._isValidSentenceForm=function(description){let wellFormed=true;if(description){const baseDesc=description.trim();const decomposed=baseDesc.split(/\.(?=\s)/).reverse();if(decomposed[0].endsWith(".")){decomposed.forEach(description=>{if(!description.trimStart().match(/^[A-Z].*/)){wellFormed=false}})}else{wellFormed=false}}return wellFormed};module.exports=ruleDef;