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

'use strict';

const KNOWN_BADGES = {
	premium: {
		name: 'premium',
		label: 'Premium feature'
	}
};

/**
 * Normalizes badged in page configuration
 *
 *		badges: [ premium, badgeFoo, unknownBadge ]
 *
 * to Pug-friendly format with unknown badges filtered out:
 *
 *		[
 *			{ name: 'premium', label: 'Premium feature' },
 *			{ name: 'foo', label: 'A foo page label' }
 *		]
 *
 * @protected
 * @param {Object} data
 * @return {Array.<Object>}
 */
module.exports = function normalizeBadges( data ) {
	if ( !data.badges ) {
		return;
	}

	return data.badges
		.filter( badgeName => {
			if ( !KNOWN_BADGES[ badgeName ] ) {
				console.warn(
					`Warning: Unknown badge "${ badgeName }" set for the page "${ data.title }". ` +
					`Known badges are: ${ Object.keys( KNOWN_BADGES ).map( badgeName => `"${ badgeName }"` ).join( ', ' ) }`
				);

				return false;
			}

			return true;
		} )
		.map( badgeName => KNOWN_BADGES[ badgeName ] );
};
