/*******************************************************************************
 * Copyright (C) 2026, Leo Galambos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
package org.egothor.stemmer.benchmark;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.egothor.stemmer.CompiledPatchCommand;
import org.egothor.stemmer.FrequencyTrie;

/**
 * Benchmark-only Radixor stemmer adapter for the canonical preferred-result
 * path over normalized benchmark tokens.
 *
 * <p>
 * The benchmark corpus is normalized during setup, so this adapter uses
 * {@link FrequencyTrie#getNormalizedString(String)} to avoid measuring
 * redundant lookup-time normalization. Patch commands are applied with the
 * traversal direction persisted in the trie metadata.
 * </p>
 *
 * <p>
 * Instances are mutable and intended for one JMH worker thread.
 * </p>
 */
final class RadixorBenchmarkStemmer {

    /**
     * Compiled Radixor patch trie with decoded patch-command values.
     */
    private final FrequencyTrie<CompiledPatchCommand> trie;

    /**
     * Creates a benchmark stemmer around one compiled Radixor trie.
     *
     * @param trie compiled Radixor patch trie
     */
    RadixorBenchmarkStemmer(final FrequencyTrie<CompiledPatchCommand> trie) {
        this.trie = Objects.requireNonNull(trie, "trie");
    }

    /**
     * Stems one benchmark token through the canonical trie lookup API.
     *
     * @param token input token
     * @return Radixor stem or the input token when no patch is stored
     */
    String stem(final String token) {
        final CompiledPatchCommand patch = this.trie.getNormalizedString(token);
        if (patch == null || patch.preservesAllSources()) {
            return token;
        }
        return patch.apply(token);
    }

    /**
     * Returns every distinct candidate stem from the ranked {@code getAll} path,
     * always including the deterministic primary output.
     *
     * @param token original input token
     * @return immutable candidate list in deterministic ranked order
     */
    List<String> stemAll(final String token) {
        final String primary = stem(token);
        final Set<String> candidates = new LinkedHashSet<>();
        candidates.add(primary);
        final CompiledPatchCommand[] patches = this.trie.getAll(token);
        for (CompiledPatchCommand patch : patches) {
            candidates.add(patch.preservesAllSources() ? token : patch.apply(token));
        }
        return List.copyOf(candidates);
    }
}
