import { useQuery } from '@tanstack/react-query';

import api from '../services/api';

export const useSearchPersonTypes = (token: string) => {
  return useQuery(
    ['search-person-types', [token]],
    () => api.organizationUnit.offerTypesAllGet(token),
    {
      enabled: !!token,
    },
  );
};

export const useSearchPersonList = (
  token: string,
  search: string,
  typeId: string,
  searchInUnits: boolean,
  searchInCatalog: boolean,
) => {
  const { data: unitList, isLoading: isUnitLoading } = useSearchPerson(
    token,
    search,
    undefined,
    undefined,
    !searchInUnits,
  );

  const { data: catalogList, isLoading: isCatalogLoading } = useSearchPersonCatalog(
    token,
    search,
    undefined,
    undefined,
    typeId,
    !searchInCatalog,
  );

  const isLoading = (searchInUnits && isUnitLoading) || (searchInCatalog && isCatalogLoading);
  const list = [
    ...(searchInCatalog
      ? catalogList?.map(item => {
          return {
            ...item,
            isFromCatalog: true,
          };
        }) || []
      : []),
  ];
  if (searchInUnits) {
    unitList?.forEach(item => {
      const index = list.findIndex(i => i.id === item.id);
      if (index === -1) {
        list.push({ ...item, isFromCatalog: false });
      }
    });
  }
  return { list, isLoading };
};
export type Person = {
  id: string;
  name: string;
};

export const useSearchPersonSelect = (
  token: string,
  name?: string,
  regNumber?: string,
  regNumberStart?: string,
  disabled = false,
) => {
  const {
    data: unitList,
    isFetching: isUnitLoading,
    refetch: refetchUnits,
  } = useSearchPerson(token, name, regNumber, regNumberStart, disabled);

  const {
    data: catalogList,
    isFetching: isCatalogLoading,
    refetch: refetchCatalog,
  } = useSearchPersonCatalog(token, name, regNumber, regNumberStart, undefined, disabled);

  const list: Person[] = [
    ...(catalogList || []).map(item => ({
      name: item.displayName,
      id: item.id,
    })),
    ...(unitList || []).map(item => ({
      name: item.displayName,
      id: item.id,
    })),
  ];

  const refetchList = () => {
    refetchUnits();
    refetchCatalog();
  };

  const isLoading = isUnitLoading || isCatalogLoading;

  return { list, isLoading, refetchList };
};

export const useSearchPerson = (
  token: string,
  name?: string,
  regNumber?: string,
  regNumberStart?: string,
  disabled = false,
) => {
  return useQuery(
    ['search-person', [token, name, regNumber, regNumberStart]],
    () => api.organizationUnit.personAllGet(token, name, regNumber, regNumberStart),
    {
      enabled: !!token && !disabled,
    },
  );
};

export const useSearchPersonCatalog = (
  token: string,
  name?: string,
  regNumber?: string,
  regNumberStart?: string,
  typeId?: string,
  disabled = false,
) => {
  return useQuery(
    ['search-person-catalog', [token, name, typeId]],
    () => api.organizationUnit.PersonAllCatalogGet(token, name, regNumber, regNumberStart, typeId),
    {
      enabled: !!token && !disabled,
    },
  );
};
