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

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

const initialState = {
  templates: [],
  totalCount: 0,
  page: 1,
  pageSize: ROWS_PER_PAGE,
  searchText: '',
  searchIn: ['id', 'name', 'title'],
  sortBy: 'updatedAt',
  sortOrder: SORT_DESC,
  formTemplateCategoryId: TEMPLATE_CATEGORY_ALL,
  formTemplateCategoryOptions: [],
};

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

  reducers: {
    getTemplatesAction: (state) => state,
    deleteTemplateAction: (state) => state,
    getCategoryOptionsAction: (state) => state,
    setFieldAction: (state, action) => ({
      ...state,
      ...action.payload,
    }),
  },
});

export const { deleteTemplateAction, getCategoryOptionsAction, getTemplatesAction, setFieldAction } = slice.actions;

export default slice.reducer;
