import api, {PageData} from '@config/api';
import {AudienceUser} from '@interfaces/user';
import {
  JourneyAssignee,
  GetJourneyAssigneesParams,
  FilterItem,
} from '@modules/employeeLifecycle/interfaces';
import {getResponseQuery} from '@services/utils';
import {AUDIENCE_HEADER} from '@services/constants';

import {GetUsersAudienceParams} from './interfaces';

export const getSegmentationItems = async (
  params: GetJourneyAssigneesParams,
) => {
  const response = await api.get<JourneyAssignee[]>(
    '/employee-lifecycle/user/journeys/tasks/collaborators',
    params,
  );

  return getResponseQuery<JourneyAssignee[], FilterItem[]>(
    response,
    (responseData: JourneyAssignee[]) =>
      responseData.map(item => ({
        id: item.id,
        label: item.fullName,
      })),
  );
};

export const getUsersAudience = async (params: GetUsersAudienceParams) => {
  const response = await api.get<PageData<AudienceUser>>(
    '/users/audience',
    params,
    AUDIENCE_HEADER,
  );

  return getResponseQuery<PageData<AudienceUser>>(response);
};
