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

import { FILTERS, TIME_INTERVAL_OPTIONS } from '../../../common/constants';
import { PERFORMANCE_COMPARISON_FILTERS } from '../../constants';

const initialState = {
  filterDate: FILTERS[0].value,
  formatFilterId: null,
  formatFilter: null,
  device: null,
  geo: null,
  total: {},
  comparisonFilter: PERFORMANCE_COMPARISON_FILTERS[3].value,
  comparisonHistogram: [],
  performanceFilter: TIME_INTERVAL_OPTIONS[2].value,
  performanceHistogram: [],
  financialsFilter: TIME_INTERVAL_OPTIONS[2].value,
  financialsHistogram: [],
  searchResult: [],
  isSearchLoading: false,
  userHubs: [],
  userDemandAccounts: [],
  pageConfig: null,
};

export const slice = createSlice({
  name: '@@marketplaceDashboard/MARKETPLACEDASHBOARD',
  initialState,

  reducers: {
    setFilterDateAction: (state, action) => {
      state.filterDate = action.payload;
    },
    setFilterFormatAction: (state, action) => {
      Object.keys(action.payload ?? {}).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    setGeoAction: (state, action) => {
      state.geo = action.payload;
    },
    setDeviceAction: (state, action) => {
      state.device = action.payload;
    },
    setTotalAction: (state, action) => {
      state.total = action.payload;
    },
    setComparisonFilterAction: (state, action) => {
      state.comparisonFilter = action.payload;
    },
    setComparisonHistogramAction: (state, action) => {
      state.comparisonHistogram = action.payload;
    },
    setPerformanceFilterAction: (state, action) => {
      state.performanceFilter = action.payload;
    },
    setPerformanceHistogramAction: (state, action) => {
      state.performanceHistogram = action.payload;
    },
    setFinancialsFilterAction: (state, action) => {
      state.financialsFilter = action.payload;
    },
    setFinancialsHistogramAction: (state, action) => {
      state.financialsHistogram = action.payload;
    },
    setIsSearchLoadingAction: (state, action) => {
      state.isSearchLoading = action.payload;
    },
    setSearchResultAction: (state, action) => {
      state.searchResult = action.payload;
    },
    setPageConfigAction: (state, action) => {
      state.pageConfig = action.payload;
    },
    setHubsAndDemandAccountsAction: (state, action) => {
      state.userHubs = action.payload?.userHubs ?? [];
      state.userDemandAccounts = action.payload?.userDemandAccounts ?? [];
    },

    getTotalAction: (state) => state,
    getComparisonHistogramAction: (state) => state,
    getPerformanceHistogramAction: (state) => state,
    getFinancialsHistogramAction: (state) => state,
    setSearchAction: (state) => state,
    getHubsAndDemandAccountsAction: (state) => state,
  },
});

export const {
  setDeviceAction,
  setFilterDateAction,
  setFilterFormatAction,
  setGeoAction,
  setTotalAction,
  setComparisonFilterAction,
  setComparisonHistogramAction,
  setPerformanceFilterAction,
  setPerformanceHistogramAction,
  setFinancialsFilterAction,
  setFinancialsHistogramAction,
  setIsSearchLoadingAction,
  setSearchResultAction,
  setPageConfigAction,
  setHubsAndDemandAccountsAction,
  getTotalAction,
  getComparisonHistogramAction,
  getPerformanceHistogramAction,
  getFinancialsHistogramAction,
  setSearchAction,
  getHubsAndDemandAccountsAction,
} = slice.actions;

export default slice.reducer;
