import { GET_CONFIGURATIONS } from '@/graphql/services/onlineHelp/constants';

import { PAYLOAD_NAME } from '@/graphql/services/onlineHelp/types/payload/configurations';

import * as selectors from '../selectors';
import { getDataAction, setTableAction } from '../slices';
import createEpicGetData from '@/modules/@common/Table/redux/epics';

const gqlQuery = `
  query ${GET_CONFIGURATIONS}($payload: ${PAYLOAD_NAME}) {
    ${GET_CONFIGURATIONS}(payload: $payload) {
      records {
        id
        name
        suffix
        helpPageUrl
        createdAt
        createdBy
        updatedAt
        updatedBy
      }
      recordsTotal
    }
  }
`;

export default createEpicGetData({
  gqlQuery,
  triggerActionType: getDataAction.type,
  processBodyRequest: (state) => {
    const variables = {
      page: selectors.pageSelector(state),
      pageSize: selectors.pageSizeSelector(state),
      sortBy: selectors.sortBySelector(state),
      sortOrder: selectors.sortOrderSelector(state),
      searchText: selectors.searchSelector(state),
    };

    return {
      payload: variables,
    };
  },
  processResponse: ({ data }) => {
    const data$ = data[GET_CONFIGURATIONS];

    return {
      records: data$.records,
      recordsTotal: data$.recordsTotal,
      allRecordsCount: data$.recordsTotal,
    };
  },
  setTableAction,
});
