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

'use strict';

const AbstractParser = require( './abstractparser' );
const ReflectionKind = require( './reflectionkind' );

module.exports = class FunctionParser extends AbstractParser {
	/**
	 * @param {BaseReflection} item
	 * @returns {Boolean}
	 */
	canParse( item ) {
		return item.kind === ReflectionKind.Function;
	}

	/**
	 * @param {FunctionReflection} item
	 * @param {String} parentName
	 * @returns {Array.<Object>}
	 */
	parse( item, parentName ) {
		return item.signatures.map( ( signature, index ) => {
			const doclet = {
				// Needed for post-processing once all project reflections are converted.
				_signature: signature,

				id: item.id,
				name: item.name,
				memberof: parentName,
				longname: this.getLongName( item, parentName ),

				scope: this.getScope( signature ),
				access: this.getVisibility( signature ),
				kind: this.getKind( item ),
				extraId: this.getExtraId( item ),
				description: this.getComment( signature ),
				file: this.getFile( item, index ),
				skipSource: this.shouldSkipSource( signature ),
				deprecated: this.isDeprecated( signature ),
				see: this.getRelated( signature, parentName ),

				isOverloaded: item.signatures.length > 1,
				label: this._getLabelName( signature ),

				// These properties will be filled in the post-processing phase.
				returns: null,
				params: null,
				typeParameters: null
			};

			// Add a label as suffix for overloaded members.
			if ( doclet.label ) {
				doclet.extraId += ':' + doclet.label;
			}

			return doclet;
		} );
	}
};
