import { IAdapter, RepositoryBuilder } from "@/contexts/common/domain";
import { UniqueID } from "@shared/contexts";
import { IInvoiceParticipantRepository } from "../../domain";
import { InvoiceCustomer } from "../../domain/entities/invoice-customer/invoice-customer";

export const participantFinder = async (
  participantId: UniqueID,
  adapter: IAdapter,
  repository: RepositoryBuilder<IInvoiceParticipantRepository>
): Promise<InvoiceCustomer | undefined> => {
  if (!participantId || (participantId && participantId.isNull())) {
    return Promise.resolve(undefined);
  }

  const participant = await adapter
    .startTransaction()
    .complete((t) => repository({ transaction: t }).getById(participantId));

  return Promise.resolve(participant ? participant : undefined);
};
