import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { ProductNode } from "@/components/catalog/type";

interface DialogState {
  isOpen: boolean;
  product: ProductNode | null;
}

const initialState: DialogState = {
  isOpen: false,
  product: null,
};

const addToCartDialogSlice = createSlice({
  name: 'addToCartDialog',
  initialState,
  reducers: {
    openAddToCartDialog(state, action: PayloadAction<ProductNode>) {
      state.isOpen = true;
      state.product = action.payload;
    },
    closeAddToCartDialog(state) {
      state.isOpen = false;
      // 可以保留 product 用于退出动画（建议动画完成后再清空，这里简单处理）
      // 如果需要等动画结束，可以在组件中延迟 dispatch(clearProduct())
    },
    clearAddToCartProduct(state) {
      state.product = null;
    },
  },
});

export const { openAddToCartDialog, closeAddToCartDialog, clearAddToCartProduct } = addToCartDialogSlice.actions;
export default addToCartDialogSlice.reducer;