import { SORT_DESC } from '@/modules/@common/constants/sort';

export default function createTableSlice(tableReduxKey, params) {
  return {
    state: {
      // todo: move to ts
      [tableReduxKey]: {
        data: null,
        page: params.page ?? 1,
        pageSize: params.pageSize ?? 10,
        totalCount: 0,
        sortBy: params.sortBy,
        sortOrder: params.sortOrder ?? SORT_DESC,
        selected: [],
        isLoading: false,
      },
    },
    actions: {
      getTableDataAction: (state) => state,
      setTableAction: (state, action) => {
        Object.keys(action.payload).forEach((key) => {
          state[tableReduxKey][key] = action.payload[key];
        });
      },
    },
  };
}
