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

import { FORM_REDUX_FIELD_NAME, STATUS_FILTERS, VALUE_FORM_REDUX_FIELD_NAME } from '../../constants';

import { validationScheme } from '../../helpers/validationScheme';
import { validationValueScheme } from '../../helpers/validationSchemeValue';
import createFormSlice from '@/modules/@common/Form/redux/slices';

const formSlice = createFormSlice(FORM_REDUX_FIELD_NAME, validationScheme);

export const { validateFields, validateSingleField } = formSlice;

const formValueSlice = createFormSlice(VALUE_FORM_REDUX_FIELD_NAME, validationValueScheme);

export const { validateFields: validateValueFormFields } = formValueSlice;

const initialListsState = {
  search: '',
  status: STATUS_FILTERS[1].value,
  totalCount: 0,
  page: 0,
  rowsPerPage: 10,
  sortBy: 'updated',
  sortOrder: 'DESC',
  keyLists: [],
};

const initialFormState = {
  name: '',
  demandAccount: null,
  demandAccountOptions: [],
  searchKeys: '',
  keys: [],
  keysPage: 0,
  keysRowsPerPage: 5,
  bulkKeys: [],
  // for key value form
  keyName: '',
  keyValue: '',

  ...formSlice.state,
  ...formValueSlice.state,
};

const initialState = {
  tab: 0,
  userHubs: [],
  userDemandAccounts: [],
  // list
  ...initialListsState,
  // form
  ...initialFormState,
};

export const slice = createSlice({
  name: '@@marketplaceKeyLists/MARKETPLACE_KEYLISTS',
  initialState,

  reducers: {
    setFieldAction: (state, action) => {
      Object.entries(action.payload).forEach(([key, value]) => {
        state[key] = value;
      });
    },
    setInitialListsStateAction: (state) => {
      Object.keys(initialListsState).forEach((key) => {
        state[key] = initialListsState[key];
      });
    },
    setInitialFormAction: (state) => ({
      ...state,
      ...initialFormState,
    }),

    searchKeyListsAction: (state) => state,
    bulkUpdateKeyListsAction: (state) => state,
    getKeyListByIdAction: (state) => state,
    createKeyListAction: (state) => state,
    updateKeyListAction: (state) => state,
    searchAvailableKeysAction: (state) => state,
    bulkUpdateAvailableKeysAction: (state) => state,
    getAvailableKeyByIdAction: (state) => state,
    createAvailableKeyAction: (state) => state,
    updateAvailableKeyAction: (state) => state,
    getDemandAccountsAction: (state) => state,
    getHubsAndDemandAccountsAction: (state) => state,

    setScrollToFieldNameAction: formSlice.actions.setScrollToFieldAction,
    setErrorByPropAction: formSlice.actions.updateValidationSchemeAction,
    removeErrorByPropAction: formSlice.actions.removeErrorByFieldNameAction,

    setScrollToFieldNameValueFormAction: formValueSlice.actions.setScrollToFieldAction,
    setErrorByPropValueFormAction: formValueSlice.actions.updateValidationSchemeAction,
    removeErrorByPropValueFormAction: formValueSlice.actions.removeErrorByFieldNameAction,
  },
});

export const {
  setFieldAction,
  setInitialListsStateAction,
  searchKeyListsAction,
  bulkUpdateKeyListsAction,
  getKeyListByIdAction,
  createKeyListAction,
  updateKeyListAction,
  searchAvailableKeysAction,
  bulkUpdateAvailableKeysAction,
  getAvailableKeyByIdAction,
  createAvailableKeyAction,
  updateAvailableKeyAction,
  getDemandAccountsAction,
  getHubsAndDemandAccountsAction,
  setInitialFormAction,

  setScrollToFieldNameAction,
  setErrorByPropAction,
  removeErrorByPropAction,

  setScrollToFieldNameValueFormAction,
  setErrorByPropValueFormAction,
  removeErrorByPropValueFormAction,
} = slice.actions;

export default slice.reducer;
