import { identity, pickBy } from 'lodash-es';
import qs from 'qs';

import api from 'src/config/api';
import { User } from 'src/types/orgChart';
import { Pagination } from 'src/types/services';
import { type UserListResponse } from 'src/types/user';

export const getUserBosses = (userId: number) =>
  api.get<User[]>(`/organization-charts/${userId}/all-bosses`, {
    params: {
      page: 1,
      limit: 100,
    },
  });

export const getUserSubordinates = (userId: number) =>
  api.get<User[]>(`/organization-charts/${userId}/subordinates`, {
    params: {
      page: 1,
      limit: 100,
    },
  });

export const getUserBoss = (userId: number) =>
  api.get<Pagination<User[]>>(`/organization-charts/${userId}/bosses`, {
    params: {
      page: 1,
      limit: 10,
    },
  });

export const getOrgChartPeopleList = (
  q: string,
  page: number,
  limit = 10,
  orderBy?: string,
  order?: string,
) =>
  api.get<UserListResponse>('/organization-charts/people', {
    params: pickBy({ page, limit, q, orderBy, order }, identity),
    paramsSerializer: params => qs.stringify(params, { arrayFormat: 'comma' }),
  });
