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

import { ROWS_PER_PAGE_DEFAULT, TABLE_REDUX_FIELD_NAME, TABLE_SORT_BY, TYPE_ALL } from '../../constants';
import { SORT_ASC } from '@/modules/@common/constants/sort';

import createTableSlice from '@/modules/@common/Table/redux/slices';

const tableSlice = createTableSlice(TABLE_REDUX_FIELD_NAME, {
  page: 1,
  pageSize: ROWS_PER_PAGE_DEFAULT,
  sortBy: TABLE_SORT_BY,
  sortOrder: SORT_ASC,
});

const initialState = {
  // table
  ...tableSlice.state,

  // filters
  search: '',
  type: TYPE_ALL,

  account: null,
  accountOptions: null,
};

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

  reducers: {
    getDataAction: tableSlice.actions.getTableDataAction,
    setTableAction: tableSlice.actions.setTableAction,
    setAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    getAccountOptionsAction: (state) => state,
  },
});

export const { getDataAction, setTableAction, setAction, getAccountOptionsAction, archiveAction } = slice.actions;
