export class InputHistory {
    limit;
    entries = [];
    index = null;
    draft = "";
    constructor(limit = 100) {
        this.limit = limit;
    }
    add(input) {
        const value = input.trim();
        if (!value || value.startsWith("/"))
            return;
        if (this.entries.at(-1) === value)
            return;
        this.entries.push(value);
        if (this.entries.length > this.limit)
            this.entries.shift();
        this.index = null;
        this.draft = "";
    }
    previous(currentDraft) {
        if (this.entries.length === 0)
            return null;
        if (this.index === null) {
            this.draft = currentDraft;
            this.index = this.entries.length - 1;
        }
        else {
            this.index = Math.max(0, this.index - 1);
        }
        return this.entries[this.index] ?? null;
    }
    next() {
        if (this.index === null)
            return null;
        this.index += 1;
        if (this.index >= this.entries.length) {
            this.index = null;
            return this.draft;
        }
        return this.entries[this.index] ?? null;
    }
    snapshot(options = {}) {
        const values = [...this.entries];
        return options.newestFirst ? values.reverse() : values;
    }
}
