import yaml from 'js-yaml';
import fs from 'node:fs';
import path from 'node:path';
import { ZodError } from 'zod';
export default class Config {
    filename;
    constructor(filename) {
        this.filename = filename;
    }
    isAvailable() {
        return fs.existsSync(this.filename);
    }
    parse() {
        const fileContent = fs.readFileSync(path.resolve(this.filename), 'utf8');
        try {
            const parsedObject = this.schema.parse(yaml.load(fileContent) || {});
            return parsedObject;
        }
        catch (error) {
            if (error instanceof ZodError) {
                const errorMessages = error.issues
                    .map((issue) => {
                    const currentPath = issue.path.join('.') || 'root';
                    const message = issue.message === 'Required' ? 'Required attribute missing' : issue.message;
                    return `${currentPath}: ${message}`;
                });
                throw new Error(`${errorMessages.length} ${errorMessages.length > 1 ? 'issues' : 'issue'} found in ${this.filename}:\n- ${errorMessages.join('\n- ')}`);
            }
            else {
                throw error;
            }
        }
    }
}
