import { IFunnelPaymentVariant } from "@/api/resources/Session";
import { createSlice, createSelector } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";

interface IPaymentButton {
  paymentRequest: PaymentRequest | null;
  availableMethods: unknown;
}

interface IPayment {
  selectedPrice: number | null;
  activeProduct: IFunnelPaymentVariant | null;
}

const initialState: IPayment = {
  selectedPrice: null,
  activeProduct: null,
};

const paymentSlice = createSlice({
  name: "payment",
  initialState,
  reducers: {
    update(state, action: PayloadAction<Partial<IPayment>>) {
      return { ...state, ...action.payload };
    },
    updatePaymentButton(state, action: PayloadAction<IPaymentButton>) {
      return { ...state, paymentButton: action.payload };
    },
  },
  extraReducers: (builder) => builder.addCase("reset", () => initialState),
});

export const { actions } = paymentSlice;
export const selectSelectedPrice = createSelector(
  (state: { payment: IPayment }) => state.payment.selectedPrice,
  (payment) => payment
);
export const selectActiveProduct = createSelector(
  (state: { payment: IPayment }) => state.payment.activeProduct,
  (payment) => payment
);
export default paymentSlice.reducer;
