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

import { getDefaultConfig } from '../../../filterConfig';

const initialState = {
  config: getDefaultConfig(),
  configCopy: null,

  isMoreFiltersOpened: false,
  isFilterDirty: false,
  isInsightsMode: false,

  restoreConfig: null,
  defaultEmptyFilterOrder: false,

  tabCounters: { all: null, own: null, shared: null },
};

export const slice = createSlice({
  name: '@@editorialSearchFilter/EDITORIAL_SEARCH_FILTERS',
  initialState,
  reducers: {
    configAction: (state, action) => {
      state.config = action.payload || initialState.config;
    },
    configCopyAction: (state, action) => {
      state.configCopy = action.payload || initialState.configCopy;
    },
    isMoreFiltersOpenedAction: (state, action) => {
      state.isMoreFiltersOpened = action.payload || initialState.isMoreFiltersOpened;
    },
    isFilterDirtyAction: (state, action) => {
      state.isFilterDirty = action.payload || initialState.isFilterDirty;
    },
    isInsightsModeAction: (state, action) => {
      state.isInsightsMode = action.payload || initialState.isInsightsMode;
    },
    restoreConfigAction: (state, action) => {
      state.restoreConfig = action.payload || initialState.restoreConfig;
    },
    defaultEmptyFilterOrderAction: (state, action) => {
      state.defaultEmptyFilterOrder = action.payload || initialState.defaultEmptyFilterOrder;
    },
    tabCountersAction: (state, action) => {
      state.tabCounters = action.payload || initialState.tabCounters;
    },
    configChangedEventAction: (state) => state,
  },
});

export const {
  configAction,
  configCopyAction,
  isMoreFiltersOpenedAction,
  isFilterDirtyAction,
  isInsightsModeAction,
  restoreConfigAction,
  defaultEmptyFilterOrderAction,
  tabCountersAction,
  configChangedEventAction,
} = slice.actions;
