// state.js (вся логика состояния)
export const state = {
    maxPoints: 1000,
    signalHistory: [],
    smoothValue: 0,
    lastBufferSize: 0,
};

export function smooth(target) {
    state.smoothValue += 0.2 * (target - state.smoothValue);
    return state.smoothValue;
}

export function addSignal(value) {
    state.signalHistory.push(value);

    if (state.signalHistory.length > state.maxPoints) {
        state.signalHistory.splice(0, state.signalHistory.length - state.maxPoints);
    }
}

export function addSignals(values, bufferSize) {
    if (!Array.isArray(values) || values.length === 0) return;

    const newBytes = bufferSize - state.lastBufferSize;

    if (newBytes > 0 && newBytes <= values.length) {
        const fresh = values.slice(-newBytes);
        const decoded = fresh.map(b => b & 0x3F);
        state.signalHistory.push(...decoded);
    }

    state.lastBufferSize = bufferSize;

    if (state.signalHistory.length > state.maxPoints) {
        state.signalHistory.splice(0, state.signalHistory.length - state.maxPoints);
    }
}

export function resetBufferTracking() {
    state.lastBufferSize = 0;
}