/* eslint-disable prettier/prettier */
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';

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

import onLeaveApi from '../../network/api/onLeaveApi';
import Utils from '../../utils';

const initialHome = {
  quotationList: [],
  news: [],
  birthdayListInMonth: [],
};

export const getUserLeavesDay = createAsyncThunk(
  'onLeave/getUserLeavesDay',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetLeavesDay,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getUserRestDay = createAsyncThunk(
  'onLeave/getUserRestDay',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetRestDay,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);

const onLeaveSlice = createSlice({
  name: 'onLeave',
  initialState: initialHome,
  reducers: {},
  extraReducers: builder => {
    builder.addCase(getUserLeavesDay.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      console.log('getUserLeavesDay', action);
      if (success) {
        state.usersLeavesDayList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
    builder.addCase(getUserRestDay.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      console.log('getUserRestDay', action);
      if (success) {
        state.userRestDayList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
  },
});

const {reducer} = onLeaveSlice;
export default reducer;
