const fs = require('fs');
const YAML = require('yaml');
const flattenQCDefinitions = require('../../../utils/flattenQCDefinitions');
const { translatePath } = require('../../../utils/logicalPath');
const project = require('../../project'); // lib/db/project


async function get (projectId, opts = {}) {
	const qcConfig = (await project.configuration.get(projectId))?.qc;
	if (qcConfig?.definitions) {
		try {
			const definitions = YAML.parse(fs.readFileSync(translatePath(qcConfig.definitions)).toString());

			return opts.flat ? flattenQCDefinitions(definitions) : definitions;
		} catch (err) {
			if (err.code == "ENOENT") {
				throw { status: 404, message: "Not found" };
			} else {
				throw err;
			}
		}
	}
}

module.exports = get;
