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

const initialState: SignupState = {
  apps: {
    pageState: "input",
    company: "",
    country: "",
    dealer: "",
    adminName: "",
    email: "",
    password: "",
  },
};

export const signupSlice = createSlice({
  name: "signup",
  initialState,
  reducers: {
    changePageState: (
      state,
      action: PayloadAction<{ pageState: "input" | "confirm" | "complete" }>
    ) => {
      const { pageState } = action.payload;
      state.apps.pageState = pageState;
    },
    changeCompany: (state, action: PayloadAction<{ company: string }>) => {
      const { company } = action.payload;
      state.apps.company = company;
    },
    changeCountry: (state, action: PayloadAction<{ country: string }>) => {
      const { country } = action.payload;
      state.apps.country = country;
    },
    changeDealer: (state, action: PayloadAction<{ dealer: string }>) => {
      const { dealer } = action.payload;
      state.apps.dealer = dealer;
    },
    changeAdminName: (state, action: PayloadAction<{ adminName: string }>) => {
      const { adminName } = action.payload;
      state.apps.adminName = adminName;
    },
    changeEmail: (state, action: PayloadAction<{ email: string }>) => {
      const { email } = action.payload;
      state.apps.email = email;
    },
    changePassword: (state, action: PayloadAction<{ password: string }>) => {
      const { password } = action.payload;
      state.apps.password = password;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(signupAsync.pending, () => {
      //
    });
    builder.addCase(signupAsync.fulfilled, (state) => {
      state.apps.pageState = "complete";
    });
    builder.addCase(signupAsync.rejected, () => {
      //
    });
  },
});
export const {
  changePageState,
  changeEmail,
  changeAdminName,
  changeCompany,
  changeCountry,
  changeDealer,
  changePassword,
} = signupSlice.actions;
export default signupSlice.reducer;
