/**
 * @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 PropertyParser extends AbstractParser {
	/**
	 * @param {BaseReflection} item
	 * @returns {Boolean}
	 */
	canParse( item ) {
		return item.kind === ReflectionKind.Property;
	}

	/**
	 * @param {PropertyReflection} item
	 * @param {String} parentName
	 * @returns {Object}
	 */
	parse( item, parentName ) {
		return {
			// Needed for post-processing once all project reflections are converted.
			_signature: [ item, {} ],

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

			scope: this.getScope( item ),
			readonly: this.isReadonly( item ),
			observable: this.isObservable( item ),
			access: this.getVisibility( item ),
			optional: this.isOptional( item ),
			kind: this.getKind( item ),
			extraId: this.getExtraId( item ),
			description: this.getComment( item ),
			file: this.getFile( item ),
			skipSource: this.shouldSkipSource( item ),
			deprecated: this.isDeprecated( item ),
			inherited: !!item.inheritedFrom,
			see: this.getRelated( item, parentName ),
			defaultvalue: this.getDefault( item ),

			// This property will be filled in the post-processing phase.
			type: null
		};
	}

	/**
	 * Returns the default value of an item defined by the `@defaultValue` or `@default` tags.
	 *
	 * @param {PropertyReflection} item
	 * @returns {String}
	 */
	getDefault( item ) {
		if ( !item.comment || !item.comment.blockTags ) {
			return null;
		}

		const defaultTag = item.comment.blockTags.find( blockTag => blockTag.tag === '@defaultValue' || blockTag.tag === '@default' );

		if ( !defaultTag ) {
			return null;
		}

		return defaultTag.content[ 0 ].text;
	}
};
