<?php

namespace SMW\DataModel;

use SMW\DIProperty;
use SMW\Services\ServicesFactory;

/**
 * Holds the annotation value input order by property.
 *
 * @license GPL-2.0-or-later
 * @since 3.1
 *
 * @author mwjames
 */
class SequenceMap {

	/**
	 * @var
	 */
	private static $canMap = [];

	/**
	 * @since 3.2
	 *
	 * @param DIProperty $property
	 *
	 * @param boolean
	 */
	public function hasSequenceMap( DIProperty $property ): bool {
		return self::canMap( $property );
	}

	/**
	 * @since 3.1
	 *
	 * @param boolean
	 */
	public static function canMap( DIProperty $property ) {
		$key = $property->getKey();

		if ( isset( self::$canMap[$key] ) ) {
			return self::$canMap[$key];
		}

		$schemaFactory = ServicesFactory::getInstance()->singleton( 'SchemaFactory' );
		$schemaFinder = $schemaFactory->newSchemaFinder();

		$schemaList = $schemaFinder->newSchemaList(
			$property,
			new DIProperty( '_PROFILE_SCHEMA' )
		);

		if ( $schemaList === null ) {
			return self::$canMap[$key] = false;
		}

		$profile = $schemaList->get( 'profile' );

		return self::$canMap[$key] = $profile['sequence_map'] ?? false;
	}

}
