;
{
  try {
    let e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof globalThis ? globalThis : "undefined" != typeof self ? self : {}, n = new e.Error().stack;
    n && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[n] = "1ba0f3b2-a9c6-4094-8f78-ecf8d2a0042b", e._sentryDebugIdIdentifier = "sentry-dbid-1ba0f3b2-a9c6-4094-8f78-ecf8d2a0042b");
  } catch (e) {
  }
}
;
import { useMemo } from "preact/hooks";
import { o as LegalEntityTypes, ae as getOwnEntityAssociations, c as useLegalEntity, R as ROOT_LE } from "./StoreProvider-CqQrndhS.js";
import { u as useAssociatedLegalEntity } from "./useAssociatedLegalEntity-C4OxYq45.js";
import { U as UnincorporatedPartnershipMemberTypes } from "./unincorporated-partnership-MeN1dp53.js";
import { g as getLegalEntityNameBasedOnType } from "./getName-CiCWopsp.js";
const getMemberOwnerId = (memberId, entityAssociations) => entityAssociations.find((association) => association.associatorId === memberId)?.legalEntityId;
const convertEntityAssociationIntoUnincorporatedPartnershipMember = (association) => {
  if (association.entityType === LegalEntityTypes.INDIVIDUAL || association.entityType === LegalEntityTypes.ORGANIZATION)
    return {
      memberType: association.type,
      roles: [],
      legalEntityType: association.entityType,
      legalEntityId: association.legalEntityId || "",
      name: association.name || ""
    };
  throw new Error(`Not a valid member: ${JSON.stringify(association)}`);
};
const mapEntityAssociationsToUnincorporatedPartnershipMembers = (entityId, entityAssociations, rootLegalEntity) => {
  const mappedUnincorporatedPartnershipMembers = entityAssociations.reduce(
    (acc, entityAssociation) => {
      if (!Object.values(UnincorporatedPartnershipMemberTypes).includes(
        entityAssociation.type
      ) || entityAssociation.associatorId !== entityId || !entityAssociation.legalEntityId) {
        return acc;
      }
      const unincorporatedPartnershipMember = acc[entityAssociation.legalEntityId] ?? convertEntityAssociationIntoUnincorporatedPartnershipMember(entityAssociation);
      const unincorporatedPartnershipMemberType = entityAssociation.type;
      const memberOwnerId = getMemberOwnerId(entityAssociation.legalEntityId, entityAssociations);
      if (memberOwnerId) {
        unincorporatedPartnershipMember.ownerId = memberOwnerId;
      }
      acc[entityAssociation.legalEntityId] = {
        ...unincorporatedPartnershipMember,
        roles: [
          .../* @__PURE__ */ new Set([
            ...unincorporatedPartnershipMember.roles,
            unincorporatedPartnershipMemberType
          ])
        ]
      };
      return acc;
    },
    {}
  );
  if (rootLegalEntity) {
    mappedUnincorporatedPartnershipMembers[rootLegalEntity.id] = {
      legalEntityId: rootLegalEntity.id,
      legalEntityType: rootLegalEntity.type,
      name: getLegalEntityNameBasedOnType(rootLegalEntity),
      memberType: UnincorporatedPartnershipMemberTypes.SECONDARY_PARTNER,
      // Overriding role as primaryPartner to display in the UI, but type does not exist in the backend
      roles: ["primaryPartner"]
    };
  }
  return Object.values(mappedUnincorporatedPartnershipMembers)?.sort(
    (member) => member?.legalEntityId !== rootLegalEntity?.id ? 1 : -1
  );
};
const deleteUnincorporatedPartnershipMember = async ({
  associatedMember,
  unincorporatedPartnership,
  updateLegalEntity
}) => {
  const ownEntityAssociations = getOwnEntityAssociations(unincorporatedPartnership);
  await updateLegalEntity({
    id: unincorporatedPartnership.id,
    entityAssociations: ownEntityAssociations.filter(
      (entityAssociation) => entityAssociation.legalEntityId !== associatedMember.legalEntityId
    )
  });
};
function useUnincorporatedPartnershipMembers() {
  const { data: rootLegalEntity } = useLegalEntity(ROOT_LE);
  const { data: unincorporatedPartnership } = useAssociatedLegalEntity(
    rootLegalEntity,
    LegalEntityTypes.UNINCORPORATED_PARTNERSHIP
  );
  return useMemo(() => {
    if (!rootLegalEntity || !unincorporatedPartnership?.unincorporatedPartnership) return [];
    return mapEntityAssociationsToUnincorporatedPartnershipMembers(
      unincorporatedPartnership.id,
      unincorporatedPartnership.entityAssociations ?? [],
      rootLegalEntity
    );
  }, [rootLegalEntity, unincorporatedPartnership]);
}
export {
  deleteUnincorporatedPartnershipMember as d,
  useUnincorporatedPartnershipMembers as u
};
