/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

'use strict';

const upath = require( 'upath' );
const fs = require( 'fs' );
const { globSync } = require( 'glob' );
const DOC_FORMATS = require( '../helpers/doc-formats' );

/**
 * Reads API data (usually generated by JSDoc) from given sourcePath. This data is used by DataProvider and ApiBuilder.
 *
 * @param sourcePath
 * @param type
 * @returns {Array}
 */
module.exports = ( sourcePath, type ) => {
	const fileNames = globSync( upath.join( sourcePath, `**/*.${ DOC_FORMATS[ type ] }` ) ).map( path => upath.normalize( path ) );
	const files = [];

	for ( const fileName of fileNames ) {
		const file = fs.readFileSync( fileName, 'utf8' );

		files.push( file );
	}

	return files;
};
