// slices/digitalInputsSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

interface DigitalInput {
  id: number;
  label: string;
  status: boolean;
  counter: number;
  flutter: number;
}

interface DigitalInputsState {
  inputs: DigitalInput[];
}

const initialState: DigitalInputsState = {
  inputs: [],
};

const digitalInputsSlice = createSlice({
  name: "digitalInputs",
  initialState,
  reducers: {
    setInputs: (state, action: PayloadAction<DigitalInput[]>) => {
      state.inputs = action.payload;
    },
    updateInputStatus: (
      state,
      action: PayloadAction<{ id: number; status: boolean }>
    ) => {
      const { id, status } = action.payload;
      const input = state.inputs.find((input) => input.id === id);
      if (input) {
        input.status = status;
      }
    },
  },
});

export const { setInputs, updateInputStatus } = digitalInputsSlice.actions;
export default digitalInputsSlice.reducer;
