<?php

/**
 * Italian Stemmer
 *
 * Lightweight suffix-based stemmer for Italian.
 *
 * @package ProRank\SEO\Modules\Content\InternalLinking\Parser\MultiLanguage\Languages
 * @since   1.0.0
 */

declare(strict_types=1);

namespace ProRank\SEO\Modules\Content\InternalLinking\Parser\MultiLanguage\Languages;

use ProRank\SEO\Modules\Content\InternalLinking\Parser\MultiLanguage\Stemmer;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class ItalianStemmer extends Stemmer {
    public function __construct() {
        parent::__construct('it');
    }

    public function stem(string $word): string {
        $word = $this->normalize($word);
        if (strlen($word) <= 3) {
            return $word;
        }

        $suffixes = [
            'azioni', 'uzione', 'uzioni', 'mente', 'amenti', 'amento', 'atrice',
            'atori', 'atore', 'trice', 'trici', 'ità', 'ismi', 'ista', 'isti',
            'iche', 'ichi', 'ico', 'ica', 'oso', 'osa', 'osi', 'ose',
            'are', 'ere', 'ire', 'zione', 'zioni', 'mente', 'i', 'e', 'a', 'o',
        ];

        foreach ($suffixes as $suffix) {
            if (str_ends_with($word, $suffix) && strlen($word) > strlen($suffix) + 2) {
                return substr($word, 0, -strlen($suffix));
            }
        }

        return $word;
    }
}
