import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { PartnerState } from "./state";
import { createPartnerAccountAsync } from "./operations";

const initialState: PartnerState = {
  apps: {
    addPartner: {
      companyName: "",
      country: "",
      adminName: "",
      email: "",
    },
    isLoading: false,
  },
};

export const partnerSlice = createSlice({
  name: "partner",
  initialState,
  reducers: {
    changeCompany: (state, action: PayloadAction<{ company: string }>) => {
      const { company } = action.payload;
      state.apps.addPartner.companyName = company;
    },
    changeCountry: (state, action: PayloadAction<{ country: string }>) => {
      const { country } = action.payload;
      state.apps.addPartner.country = country;
    },
    changeAdminName: (state, action: PayloadAction<{ adminName: string }>) => {
      const { adminName } = action.payload;
      state.apps.addPartner.adminName = adminName;
    },
    changeEmail: (state, action: PayloadAction<{ email: string }>) => {
      const { email } = action.payload;
      state.apps.addPartner.email = email;
    },
    cleanupAddPartner: (state) => {
      state.apps.addPartner = initialState.apps.addPartner;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(createPartnerAccountAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(createPartnerAccountAsync.fulfilled, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(createPartnerAccountAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
  },
});
export const {
  changeEmail,
  changeAdminName,
  changeCompany,
  changeCountry,
  cleanupAddPartner,
} = partnerSlice.actions;
export default partnerSlice.reducer;
