// seq_validator.c
#include "seq_validator.h"
#include <string.h>

void seq_validator_init(seq_validator_t *v, const char *const *expected, size_t count)
{
    memset(v, 0, sizeof(*v));
    if (count > SEQ_MAX_STEPS) count = SEQ_MAX_STEPS;
    v->count = count;
    for (size_t i = 0; i < count; i++) {
        strncpy(v->expected[i], expected[i], SEQ_MAX_LABEL - 1);
    }
}

bool seq_validator_feed(seq_validator_t *v, const char *code)
{
    if (v->count == 0 || v->pos >= v->count) return false;
    // duplicate of the last matched code: ignore (no progress, no reset)
    if (v->pos > 0 && strncmp(code, v->expected[v->pos - 1], SEQ_MAX_LABEL) == 0)
        return false;
    if (strncmp(code, v->expected[v->pos], SEQ_MAX_LABEL) == 0) {
        v->pos++;
        return v->pos == v->count;
    }
    v->pos = 0;                    // wrong scan -> reset
    // allow the wrong scan to itself start a new run at index 0
    if (strncmp(code, v->expected[0], SEQ_MAX_LABEL) == 0) v->pos = 1;
    return false;
}
