<?php

/**
 * Spanish Stemmer
 *
 * Lightweight suffix-based stemmer for Spanish.
 *
 * @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 SpanishStemmer extends Stemmer {
    public function __construct() {
        parent::__construct('es');
    }

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

        $suffixes = [
            'amientos', 'imientos', 'aciones', 'adores', 'adoras', 'ancia', 'mente',
            'idades', 'amiento', 'imiento', 'acion', 'adora', 'ador', 'ancia',
            'anza', 'icos', 'icas', 'ismo', 'ista', 'able', 'ible', 'osos', 'osas',
            'idad', 'ivas', 'ivos', 'iva', 'ivo', 'ico', 'ica', 'oso', 'osa',
            'es', 'as', 'os', 's',
        ];

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

        return $word;
    }
}
