import { createSlice, PayloadAction } from "@reduxjs/toolkit";

// Define the structure of an individual item in myObj
interface DetailsTable {
  Sl: number;
  Room: number;
  Width: number;
  Height: number;
  Comment: string;
  Product: string;
  Pattern: string;
  ListPrice: number;
  selected: boolean;
}

// Define the initial state as an array of MyObjItem
const initialState: DetailsTable[] = [
  {
    Sl: 1,
    Room: 1,
    Width: 0.0,
    Height: 0.0,
    Comment: "",
    Product: '2 1/2" BassWood Blinds',
    Pattern: "",
    ListPrice: 69.62,
    selected: true,
  },
  {
    Sl: 2,
    Room: 1,
    Width: 0.0,
    Height: 0.0,
    Comment: "",
    Product: '2 1/2" BassWood Blinds',
    Pattern: "",
    ListPrice: 69.62,
    selected: false,
  },
];

const detailsTableSlice = createSlice({
  name: "myObj",
  initialState,
  reducers: {
    // Toggle the "selected" property of an item based on its Sl value
    toggleSelected: (state, action: PayloadAction<number>) => {
      const index = state.findIndex((item) => item.Sl === action.payload);
      if (index !== -1) {
        state[index].selected = !state[index].selected;
      }
    },
  },
});

export const { toggleSelected } = detailsTableSlice.actions;
export default detailsTableSlice.reducer;
