<?PHP
/**
 * Identifies the type of tag relation to an object based on known suffixes.
 *
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 */
declare(strict_types = 1);

/**
 * Contains static functions for identifying uncertainty or blocking
 * completely uncertain inputs for actors, times, and places.
 */
final class NodaTagRelationIdentifier {

    private const SUFFIXES = [
        'de' => [
            ' (Motiv)' => MDTagRelationType::display_subject,
            ' [Motiv]' => MDTagRelationType::display_subject,
            ' <Motiv>' => MDTagRelationType::display_subject,

            ' (Material)' => MDTagRelationType::material,
            ' [Material]' => MDTagRelationType::material,
            ' <Material>' => MDTagRelationType::material,

            ' (Technik)' => MDTagRelationType::technique,
            ' [Technik]' => MDTagRelationType::technique,
            ' <Technik>' => MDTagRelationType::technique,
        ]
    ];

    public readonly string $name;
    public readonly MDTagRelationType|false $relation;

    /**
     * Constructor: Removes identifiers for well-known tag relations and determines cleaned name and relation type.
     *
     * @param string $lang         Current language.
     * @param string $input_string Input string to clean.
     *
     * @return void
     */
    public function __construct(string $lang, string $input_string) {

        if (empty(self::SUFFIXES[$lang])) {
            $this->name = $input_string;
            $this->relation = false;
            return;
        }

        $relation = false;

        $suffixes = self::SUFFIXES[$lang];
        foreach (array_keys($suffixes) as $suffix) {
            if (\mb_substr($input_string, \mb_strlen($suffix) * -1) === "$suffix") {
                $input_string = \mb_substr($input_string, 0, \mb_strlen($suffix) * -1);
                $relation = $suffixes[$suffix];
            }
        }

        $this->name = $input_string;
        $this->relation = $relation;

    }
}
