import { GET_CONTENT_OWNERS } from '@/graphql/services/accounts/constants';

import { PAYLOAD_NAME } from '@/graphql/services/accounts/types/payload/contentOwners';

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

const gqlQuery = `
  query ${GET_CONTENT_OWNERS}($payload: ${PAYLOAD_NAME}) {
    ${GET_CONTENT_OWNERS}(payload: $payload) {
      records {
        id
        name
        salesforceId
        status
        isPublic
        comments
        callToAction
      }
      recordsTotal
    }
  }
`;

export default createEpicGetData({
  gqlQuery,
  triggerActionType: getDataAction.type,
  processBodyRequest: (state, actionPayload) => ({
    payload: {
      accountId: actionPayload,
      page: selectors.pageSelector(state),
      pageSize: selectors.pageSizeSelector(state),
    },
  }),
  processResponse: ({ data }) => {
    const contentOwners = data[GET_CONTENT_OWNERS];

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