/*
 * 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 SCHEMA_NAME="component-schema.json";const COMPONENT_JSON="component.json";const COMP_TYPES=["composite","core","pack","reference","resource","vbcs-pattern","vbcs-template"];const SCHEMA_PATH="schema";const RULE_INIT_SCHEMA="jaf-init-schema";const JAF_RSI=0;const RE_FWDSLASHIFY=/\\/g;function ComponentSvcs(nd,metaPath,cfg,tmpDir,appCtx){this._nd=nd;this._error=appCtx.error;this._warn=appCtx.warn;this._debug=appCtx.debug;this._reporter=appCtx.reporter;this._Issue=appCtx.Issue;this._flushIssues=appCtx.flushIssues;this._getMsgId=appCtx.getMsgId;this._rules=appCtx.rules;this._compOptions=cfg.getComponentOptions();this._tempDir=tmpDir;this._schemaName=SCHEMA_NAME;this._schemaPath=nd.path.dirname(metaPath);this._schemaPath=nd.path.dirname(this._schemaPath);this._schemaPath=nd.path.join(this._schemaPath,SCHEMA_PATH,SCHEMA_NAME);this._schemaExists=nd.fs.existsSync(this._schemaPath);this._schemaChecked=true;if(!this._schemaExists&&!this._schemaMsgEmitted){this._error("Schema '"+this._schemaPath+"' not found!  Component schema checking disabled.");this._schemaMsgEmitted=true}if(appCtx.debugMode){this._debug("Schema -> "+this._schemaPath)}this._fsUtils=appCtx.fsUtils;this._zipUtils=require("./ZipUtils");this._zipUtils=new this._zipUtils(nd,this._fsUtils,appCtx.assert)};ComponentSvcs.prototype.loadZip=function(zipPath){return this._zipUtils.loadZip(zipPath)};ComponentSvcs.prototype.getZipContents=function(zipPath){return this._zipUtils.getContents(zipPath)};ComponentSvcs.prototype.extractFileFromZip=function(filePath,zipPath){return this._zipUtils.extractFileFromZip(filePath,zipPath)};ComponentSvcs.prototype.validateComponentJson=function(obj,fromLocation){var name,results,result,issue,ctx,opts,i;if(!obj.name){this._error("component definition at '"+fromLocation+"' has no 'name' property!");return false}else{if(typeof obj.name!=="string"){this._error("custom component at '"+fromLocation+"' has invalid 'name' property!");return false}}opts=this._rules.getRuleOptions(RULE_INIT_SCHEMA,JAF_RSI);if(!opts.enabled){return true}if(!this._schemaExists){return false}name=obj.pack?obj.pack+"-"+obj.name:obj.name;if(!this._compOptions||this._compOptions.applySchema){results=this.validateComponentJsonWithSchema(obj);if(results){fromLocation=fromLocation.replace(/\\/g,"/");this._warn("\nSchema validation of component <"+name+"> : "+results.length+" issue"+(results.length===1?" was":"s were")+" found!");for(i=0;i<results.length;i++){result=results[i];issue=new this._Issue(result.msg.length?result.msg:JSON.stringify(result.error,null,4));ctx=this._makeIssueContext(fromLocation,RULE_INIT_SCHEMA);issue.msgId=this._getMsgId(ctx,RULE_INIT_SCHEMA);this._rules.getRulePack(ctx._rsi)._setCBCtx(ctx);this._reporter.addIssue(issue,ctx)}this._flushIssues()}}return true};ComponentSvcs.prototype.extractComponentJsonFromZip=function(fpath){var text,obj,ret=null;this._debug("extracting component.json from '"+fpath+"'");ret={obj:null,data:null,msg:null,error:false};text=this._zipUtils.extractFileFromZip(COMPONENT_JSON,fpath);if(typeof text==="string"){if(text.length){ret.data=text;try{ret.obj=JSON.parse(text)}catch(e){ret.msg=e.message}}}else{obj.error=true}return ret};ComponentSvcs.prototype.validateComponentJsonWithSchema=function(obj){return this._nd.schema.validateComponentJson(obj)};ComponentSvcs.prototype.loadUrl=function(url){if(!this._netUtils){this._netUtils=require("./NetUtils");this._netUtils=new this._netUtils}return this._netUtils.loadUrl(url)};ComponentSvcs.prototype.writeZipBufferToFile=function(buffer){var ret;try{this._tmpName=this._zipUtils.createTempZipFilename();ret=this._zipUtils.writeBufferToFile(buffer,this._nd.path.join(this._tempDir,this._tmpName))}catch(e){ret=e}return ret};ComponentSvcs.prototype.getLastZipFilePath=function(){return this._zipUtils.getLastFilepath()};ComponentSvcs.prototype.deleteZipFile=function(filePath){this._zipUtils.deleteFile(filePath)};ComponentSvcs.prototype.isValidType=function(type){return type?COMP_TYPES.indexOf(type)>=0:true};ComponentSvcs.prototype.getSchemaName=function(){return this._schemaName};ComponentSvcs.prototype.stripUrlQuery=function(url){return this._netUtils.stripQuery(url)};ComponentSvcs.prototype._makeIssueContext=function(fromLocation,ruleName){return{filepath:_fwdSlashify(this._nd.path.join(fromLocation,"component.json")),opts:this._rules.getRuleOptions(ruleName,JAF_RSI),rulePack:this._rules.getRulePack(JAF_RSI),_rsi:JAF_RSI,ruleName:ruleName,position:{line:0,col:0,start:0,end:0}}};function _fwdSlashify(path){return path.replace(RE_FWDSLASHIFY,"/")};module.exports=ComponentSvcs;