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

import { SORT_DESC } from '@/modules/@common/constants/sort';
import { TEMPLATE_CATEGORIES_OPTIONS, TEMPLATE_CATEGORY_ALL } from '@/modules/forms/constants';
import { ROWS_PER_PAGE } from '@/modules/forms/forms/constants';

const initialState = {
  forms: [],
  page: 1,
  totalCount: 0,
  rowsPerPage: ROWS_PER_PAGE,
  search: '',
  sortBy: 'updatedAt',
  sortOrder: SORT_DESC,
  status: 1,
  site: null,
  siteOptions: [],
  isFormDownloadInProgress: false,
  formDownloadProcessingNotificationKey: null,

  shouldShowDownloadTryRequestOverConfirm: false,
  formDownloadTryRequestCounter: 1,
  // templates
  templates: [],
  lastUsedTemplates: [],
  templateCategory: TEMPLATE_CATEGORY_ALL,
  templateCategoryOptions: TEMPLATE_CATEGORIES_OPTIONS,
  templateSearchText: '',
};

export const slice = createSlice({
  name: '@@FORMS/LIST',
  initialState,

  reducers: {
    getSiteOptionsAction: (state) => state,
    getFormsAction: (state) => state,
    toArchiveAction: (state) => state,
    downloadFormDataAction: (state) => state,
    sendFormReportToUserEmailAction: (state) => state,
    getTemplateGalleryAction: (state) => state,
    getTemplateGalleryCategoryAction: (state) => state,
    setFieldAction: (state, action) => ({
      ...state,
      ...action.payload,
    }),
  },
});

export const {
  downloadFormDataAction,
  getFormsAction,
  getSiteOptionsAction,
  getTemplateGalleryAction,
  getTemplateGalleryCategoryAction,
  sendFormReportToUserEmailAction,
  setFieldAction,
  toArchiveAction,
} = slice.actions;

export default slice.reducer;
