export interface AccountOpenOrder {
  apiKeyId: string;
  apiKeyName: string;
  id: string;
  symbol: string;
  type: string;
  price: number;
  quantity: number;
  side: string;
  status: string;
}


import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {HYDRATE} from "next-redux-wrapper";
import {Account, AccountOpenOrder} from "../actions/cicap-diary-account";

export interface OpenOrdersSlice {
  openOrders: Record<string, Record<string, AccountOpenOrder>>;
  filledCanceledId: string[];
}

const initialState: OpenOrdersSlice = {
  openOrders: {},
  filledCanceledId: [],
};

export const openOrdersSlice = createSlice({
  name: "openOrdersSlice",
  initialState,
  reducers: {
    loadOpenOrdersFromAccount: (state, action: PayloadAction<Account>) => {
      state.openOrders = action.payload.openOrders.reduce(
        (acc, order) => {
          if (!acc[order.apiKeyId]) {
            acc[order.apiKeyId] = {};
          }

          return {
            ...acc,
            [order.apiKeyId]: {
              ...acc[order.apiKeyId],
              [order.id]: order,
            },
          };
        },
        {} as Record<string, Record<string, AccountOpenOrder>>,
      );
    },
    addOpenOrder: (state, action: PayloadAction<AccountOpenOrder>) => {
      if (!state.openOrders[action.payload.apiKeyId]) {
        state.openOrders[action.payload.apiKeyId] = {
          [action.payload.id]: action.payload,
        };
      } else {
        state.openOrders[action.payload.apiKeyId][action.payload.id] = action.payload;
      }
    },
    setOpenOrders: (state, action: PayloadAction<Record<string, Record<string, AccountOpenOrder>>>) => {
      state.openOrders = action.payload;
    },
    addFilledCanceledId: (state, action: PayloadAction<string>) => {
      const orderId = action.payload;
      state.filledCanceledId.push(orderId);
    },
    deleteOrder: (state, action: PayloadAction<AccountOpenOrder>) => {
      if (
        state.openOrders[action.payload.apiKeyId]
        && state.openOrders[action.payload.apiKeyId][action.payload.id]
      ) {
        delete state.openOrders[action.payload.apiKeyId][action.payload.id];
      }
    },
  },
  extraReducers: (builder) => {
    builder.addCase(HYDRATE, (state, action) => {
      return {
        ...state,
        ...action,
      };
    });
  },
});

export const {
  loadOpenOrdersFromAccount,
  addOpenOrder,
  setOpenOrders,
  addFilledCanceledId,
  deleteOrder,
} = openOrdersSlice.actions;

export default openOrdersSlice.reducer;


const AssetsBuySell = () => {
  useEffect(() => {
    const newOrderId = Date.now().toString(); // Generating a unique ID
    const addOrderTimeout = setTimeout(() => {
      const newOrder = {
        apiKeyId: "2",
        apiKeyName: "админ",
        id: newOrderId,
        symbol: "BTCUSDT",
        type: "NEW",
        price: 26700,
        quantity: 100,
        side: "long",
        status: "LIMIT",
      };
      setOrders(prevOrders => ({...prevOrders, [newOrderId]: newOrder}));
    }, 5000);

    const deleteOrderTimeout = setTimeout(() => {
      setOrders(prevOrders => {
        const updatedOrders = {...prevOrders};
        delete updatedOrders[newOrderId];

        return updatedOrders;
      });
    }, 10000);

    return () => {
      clearTimeout(addOrderTimeout);
      clearTimeout(deleteOrderTimeout);
    };
  }, []);


нужно добавить в редьюсер addOrderTimeout через 4 секунды, через 7 секунд добавить новый с price: 26400, через 12 секунд удалить addOrderTimeout из редьюсера, через 15 секунд удалить второй order