/*
 * 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 PROFILE_DIR="profiles";const JSON=".json";const NL="\n";var clr;var fsUtils;var utils;var profile;function show(nd,arg,appCtx){clr=appCtx.colors;utils=appCtx.utils;fsUtils=appCtx.fsUtils;_display(nd,arg)};function _display(nd,arg){var dir,content,title,obj;var dir=nd.path.join(__dirname,"../"+PROFILE_DIR);if(!fsUtils.fileExists(dir,true)){_error(1);return}if(arg){_displayProfile(nd,dir,arg);return}if(!(content=fsUtils.readDirSync(dir))){_error(2);return}if(!content.length){console.log("[Error]: no profiles found!");return}console.log(clr.BRIGHTYELLOW("\n--- JAF Profiles ---"));for(let i=0;i<content.length;i++){let prof=content[i];if(prof.isFile&&prof.name.endsWith(JSON)){obj=_readConfigProps(nd,dir,prof.name);console.log(clr.BRIGHTYELLOW("*")+" "+clr.BRIGHTGREEN(prof.name.replace(JSON,""))+(obj&&obj.extends?" (extends '"+obj.extends+"')":""));if(obj&&obj.title){title=utils.lineWrap(obj.title);title.forEach(line=>{console.log("   "+clr.BRIGHTCYAN(line))})}}}console.log("")};function _displayProfile(nd,dir,name){var fp,text;profile=name;fp=nd.path.join(dir,name+JSON);if(!fsUtils.fileExists(nd.path.join(fp))){_error(3,name);return}console.log(`\n${clr.BRIGHTYELLOW("--- Profile")} ${clr.BRIGHTGREEN(profile)} ${clr.BRIGHTYELLOW("---")}`);if(!(text=fsUtils.readFileSync(fp,_readError))){return}_displayFile(text)};function _displayFile(txt){var a=txt.split(NL);a.forEach(line=>{console.log(line.trimRight())})};function _readConfigProps(nd,dir,profile){var fp,cfg,title,ext;fp=nd.path.join(dir,profile);if(cfg=fsUtils.readJsonSync(fp,true)){title=cfg.title;if(ext=cfg.extends){ext=ext.profile}}return{title:title,extends:ext}};function _error(n,v1){var msg;if(n===1){msg="no profiles found"}else if(n===2){msg="error reading profiles"}else if(n===3){msg=`unknown profile '${v1}'`}console.log(`[Error]: ${msg}`)};function _readError(msg){console.log(`Cannot read profile '${profile}' : ${msg}`)};module.exports.show=show;