import { createSlice } from "@reduxjs/toolkit";
import getItem from "../../utils/getItem";

export const theSlice = createSlice({
  name: 'holiday-settings',
  initialState: {
    countOnlyWorkingDays: false,
    disableDays: [],
    holidays: [],
    loaded: false,
  },
  reducers: {
    // section parts
    loadHolidaySettings: (state, action) => {
      const data = action.payload;
      state.countOnlyWorkingDays = data.countOnlyWorkingDays;
      state.disableDays = data.disableDays;
      state.holidays = data.holidays;
      state.loaded = true;
    },
    updateCountOnlyWorkingDays: (state, action) => {
      const data = action.payload
      state.countOnlyWorkingDays = data
    },
    updateDisableDay: (state, action) => {
      const {id, checked} = action.payload
      const item = getItem(id, state.disableDays)
      if (item) {
        item.checked = checked
      }
    },
    updateHoliday: (state, action) => {
      const {id, data} = action.payload
      const item = getItem(id, state.holidays)
      if (item) {
        Object.assign(item, data);
      }
    },
  }
});

export const {
  loadHolidaySettings,
  updateCountOnlyWorkingDays,
  updateDisableDay,
  updateHoliday,
} = theSlice.actions;
export default theSlice.reducer;