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] = "4527662d-139b-4458-9c6a-4e94e29b7a01", e._sentryDebugIdIdentifier = "sentry-dbid-4527662d-139b-4458-9c6a-4e94e29b7a01");
} catch (e) {}
import { r as useLegalEntity, t as ROOT_LE } from "./useLegalEntity-Dm6g0wmt.js";
import { t as LegalEntityTypes } from "./legal-entity-type-VIfNYnJP.js";
import { s as getOwnEntityAssociations } from "./entityAssociationUtil-C0gX4Ewv.js";
import { t as useAssociatedLegalEntity } from "./useAssociatedLegalEntity-CYu0lsaE.js";
import { n as getLegalEntityNameBasedOnType } from "./getName-ODLD6UPV.js";
import { t as UnincorporatedPartnershipMemberTypes } from "./unincorporated-partnership-CtwHVLzp.js";
import { useMemo } from "preact/hooks";
//#region src/components/UnincorporatedPartnership/utils/unincorporatedPartnershipMembers.ts
var getMemberOwnerId = (memberId, entityAssociations) => entityAssociations.find((association) => association.associatorId === memberId)?.legalEntityId;
var 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)}`);
};
/**
* In the API response, there are multiple {@link LegalEntityAssociation}s for the same associated entity;
* one for each role e.g. 'secondaryPartner', 'uboThroughControl', 'uboThroughOwnersghip'...
*
* Therefore we need to combine these into one {@link UnincorporatedPartnershipMember} with multiple roles.
*/
var 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: [...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,
		roles: ["primaryPartner"]
	};
	return Object.values(mappedUnincorporatedPartnershipMembers)?.sort((member) => member?.legalEntityId !== rootLegalEntity?.id ? 1 : -1);
};
var deleteUnincorporatedPartnershipMember = async ({ associatedMember, unincorporatedPartnership, updateLegalEntity }) => {
	const ownEntityAssociations = getOwnEntityAssociations(unincorporatedPartnership);
	await updateLegalEntity({
		id: unincorporatedPartnership.id,
		entityAssociations: ownEntityAssociations.filter((entityAssociation) => entityAssociation.legalEntityId !== associatedMember.legalEntityId)
	});
};
//#endregion
//#region src/hooks/useUnincorporatedPartnershipMembers.ts
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]);
}
//#endregion
export { deleteUnincorporatedPartnershipMember as n, useUnincorporatedPartnershipMembers as t };
