import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';

import serviceRequest from '../../app/serviceRequest';

import Utils from '../../utils';
import authAPI from '../../api/authAPI';
import confirmDateAPI from '../../api/confirmDateAPI';
const initialState = {};

export const getUserConfirmDay = createAsyncThunk(
  'confirmDate/getUserConfirmDay',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: confirmDateAPI.requestGetConfirmDay,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getConfirmApprovedDate = createAsyncThunk(
  'confirmDate/getConfirmApprovedDate',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: confirmDateAPI.requestGetConfirmApprovedDate,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
const authSlice = createSlice({
  name: 'confirmDate',
  initialState: initialState,
  reducers: {},
  extraReducers: builder => {
    builder.addCase(getUserConfirmDay.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);

      if (success) {
        state.confirmDateList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
    builder.addCase(getConfirmApprovedDate.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        state.confirmApprovedDateList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
  },
});

const {reducer} = authSlice;
export default reducer;
