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] = "0bf26b1b-fa1e-42bb-9000-7ac2633822b4", e._sentryDebugIdIdentifier = "sentry-dbid-0bf26b1b-fa1e-42bb-9000-7ac2633822b4");
} 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 { t as useToggleContext } from "./useToggleContext-CLfowItk.js";
import { s as getOwnEntityAssociations } from "./entityAssociationUtil-C0gX4Ewv.js";
import { r as TrustMemberTypes } from "./trustMemberGuidance-paR1FXfI.js";
import { t as useAssociatedLegalEntity } from "./useAssociatedLegalEntity-CYu0lsaE.js";
import { n as getLegalEntityNameBasedOnType } from "./getName-ODLD6UPV.js";
import { useMemo } from "preact/hooks";
import { skipToken } from "@tanstack/preact-query";
//#region src/components/SoleProprietorship/hooks/useExemptSettlor.ts
var useExemptSettlor = ({ trust }) => {
	const { data } = useLegalEntity(useMemo(() => {
		if (!trust) return void 0;
		return getOwnEntityAssociations(trust).find((association) => association.type === TrustMemberTypes.SETTLOR && Boolean(association.settlorExemptionReason));
	}, [trust])?.legalEntityId ?? skipToken);
	return data;
};
//#endregion
//#region src/utils/splitAtFirstOccurrence.ts
var splitAtFirstOccurrence = (str, separator) => {
	const firstOccurrence = str.indexOf(separator);
	if (firstOccurrence === -1) return [str];
	return [str.slice(0, firstOccurrence), str.slice(firstOccurrence + 1)];
};
//#endregion
//#region src/components/Trust/mapping/trustMembers/mapEntityAssociationsToTrustMembers.ts
var getTrustMemberOwnerId = (trustMemberId, entityAssociations) => entityAssociations.find((association) => association.associatorId === trustMemberId)?.legalEntityId;
var getFallbackName = (exemptSettlorAssociation) => {
	const [firstName, lastName] = splitAtFirstOccurrence(exemptSettlorAssociation.name ?? "", " ");
	return {
		firstName,
		lastName
	};
};
var convertEntityAssociationIntoTrustMember = (association, associations, exemptSettlorLE) => {
	if (association.settlorExemptionReason) return {
		trustMemberType: "exemptSettlor",
		roles: [TrustMemberTypes.SETTLOR],
		legalEntityType: association.entityType,
		legalEntityId: association.legalEntityId,
		settlorExemptionReason: association.settlorExemptionReason,
		exemptSettlorName: association.entityType === LegalEntityTypes.INDIVIDUAL ? exemptSettlorLE?.individual?.name ?? getFallbackName(association) : void 0,
		name: association.name
	};
	if (association.entityType === LegalEntityTypes.ORGANIZATION) return {
		trustMemberType: "company",
		roles: [],
		legalEntityType: LegalEntityTypes.ORGANIZATION,
		legalEntityId: association.legalEntityId,
		name: association.name,
		ownerId: getTrustMemberOwnerId(association.legalEntityId, associations)
	};
	if (association.entityType === LegalEntityTypes.INDIVIDUAL) return {
		trustMemberType: "regular",
		roles: [],
		legalEntityType: LegalEntityTypes.INDIVIDUAL,
		legalEntityId: association.legalEntityId,
		name: association.name
	};
	throw new Error(`Not a valid trust 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. 'trustee', 'protector', 'settlor'...
*
* Therefore we need to combine these into one {@link TrustMember} with multiple roles.
*/
var mapEntityAssociationsToTrustMembers = (trustEntityId, entityAssociations, exemptSettlorLE) => {
	const mappedTrustMembers = entityAssociations.reduce((acc, entityAssociation) => {
		if (!Object.values(TrustMemberTypes).includes(entityAssociation.type) || entityAssociation.associatorId !== trustEntityId || !entityAssociation.legalEntityId) return acc;
		const trustMember = acc[entityAssociation.legalEntityId] ?? convertEntityAssociationIntoTrustMember(entityAssociation, entityAssociations, exemptSettlorLE);
		const trustMemberType = entityAssociation.type;
		acc[entityAssociation.legalEntityId] = {
			...trustMember,
			roles: [...new Set([...trustMember.roles, trustMemberType])]
		};
		return acc;
	}, {});
	return Object.values(mappedTrustMembers);
};
//#endregion
//#region src/components/Trust/mapping/trustMembers/mapRootLegalEntityToTrustMember.ts
var mapRootLegalEntityToTrustMember = (rootLegalEntity) => ({
	trustMemberType: "rootTrustee",
	roles: [TrustMemberTypes.TRUSTEE],
	legalEntityType: rootLegalEntity.type,
	legalEntityId: rootLegalEntity.id,
	name: getLegalEntityNameBasedOnType(rootLegalEntity)
});
//#endregion
//#region src/components/Trust/mapping/trustMembers/mapUndefinedBeneficiaryInfoToTrustMembers.ts
var mapUndefinedBeneficiaryInfoToTrustMembers = (undefinedBeneficiaryInfo) => undefinedBeneficiaryInfo.map(({ reference, description }) => ({
	trustMemberType: "undefinedBeneficiary",
	roles: [TrustMemberTypes.UNDEFINED_BENEFICIARY],
	reference,
	description
}));
//#endregion
//#region src/components/Trust/mapping/trustMembers/getTrustMembers.ts
var getTrustMembers = (trustLE, rootLegalEntity, exemptSettlorLE) => [
	mapRootLegalEntityToTrustMember(rootLegalEntity),
	...mapEntityAssociationsToTrustMembers(trustLE.id, trustLE.entityAssociations ?? [], exemptSettlorLE),
	...mapUndefinedBeneficiaryInfoToTrustMembers(trustLE.trust?.undefinedBeneficiaryInfo ?? [])
];
var combineRootLegalEntityWithEntityAssociations = (rootLegalEntity, entityAssociations) => {
	const allowedRolesForRootLegalEntity = [
		TrustMemberTypes.SETTLOR,
		TrustMemberTypes.PROTECTOR,
		TrustMemberTypes.DEFINED_BENEFICIARY
	];
	return {
		trustMemberType: "rootTrustee",
		roles: [...entityAssociations.map((entityAssociation) => entityAssociation.type).filter((role) => allowedRolesForRootLegalEntity.includes(role)), TrustMemberTypes.TRUSTEE],
		legalEntityType: rootLegalEntity.type,
		legalEntityId: rootLegalEntity.id,
		name: getLegalEntityNameBasedOnType(rootLegalEntity)
	};
};
var getRootTrusteeTrustMembers = (trustLE, rootLegalEntity, exemptSettlorLE) => {
	const entityAssociations = trustLE.entityAssociations ?? [];
	const rootLegalEntityAssociations = entityAssociations.filter((a) => a.legalEntityId === rootLegalEntity.id);
	const nonRootLegalEntityAssociations = entityAssociations.filter((a) => a.legalEntityId !== rootLegalEntity.id);
	return [
		combineRootLegalEntityWithEntityAssociations(rootLegalEntity, rootLegalEntityAssociations),
		...mapEntityAssociationsToTrustMembers(trustLE.id, nonRootLegalEntityAssociations ?? [], exemptSettlorLE),
		...mapUndefinedBeneficiaryInfoToTrustMembers(trustLE.trust?.undefinedBeneficiaryInfo ?? [])
	];
};
//#endregion
//#region src/hooks/useTrustMembers.ts
function useTrustMembers() {
	const { data: rootLegalEntity } = useLegalEntity(ROOT_LE);
	const { data: trust } = useAssociatedLegalEntity(rootLegalEntity, LegalEntityTypes.TRUST);
	const { isFeatureEnabled } = useToggleContext();
	const exemptSettlor = useExemptSettlor({ trust });
	const allowMoreRolesForMainRootTrustee = isFeatureEnabled("AllowMoreRolesForMainRootTrustee");
	return useMemo(() => {
		if (!rootLegalEntity || !trust?.trust) return [];
		return allowMoreRolesForMainRootTrustee ? getRootTrusteeTrustMembers(trust, rootLegalEntity, exemptSettlor) : getTrustMembers(trust, rootLegalEntity, exemptSettlor);
	}, [
		rootLegalEntity,
		trust,
		exemptSettlor,
		allowMoreRolesForMainRootTrustee
	]);
}
//#endregion
export { useTrustMembers as t };
