/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
import JSZip from 'jszip';
import yaml from 'js-yaml';
import { addErrorToResponse } from '../helpers/helper.js';
import { YamlValidatorInterface } from '../models/interface.js';
import {LogWrapper} from '../service/log-wrapper.js';
export class YamlValidator implements YamlValidatorInterface {
	async validateYamlFiles(buffer: Buffer): Promise<boolean> {
		const zip = new JSZip();
		let allValid = true;
		const zipContent = await zip.loadAsync(buffer);
		for (const [fileName, file] of Object.entries(zipContent.files)) {
			if (fileName.endsWith('.yaml') || fileName.endsWith('.yml')) {
				const content = await file.async('string');
				try {
					yaml.loadAll(content);
				} catch (err) {
					addErrorToResponse('STU-VAL_ERR', 'YAML_FILE', `Invalid YAML in file ${fileName}:${err}`);
					LogWrapper.logError('0003', `Invalid YAML in file ${fileName}:${err}`);
					allValid = false;
				}
			}
		}
		return allValid;
	}
}