;
{
  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] = "470fe282-50e1-4f91-90d9-ae1a3c5563a2", e._sentryDebugIdIdentifier = "sentry-dbid-470fe282-50e1-4f91-90d9-ae1a3c5563a2");
  } catch (e) {
  }
}
;
import { useMemo } from "preact/hooks";
import { ae as getOwnEntityAssociations, bn as TrustMemberTypes, c as useLegalEntity, o as LegalEntityTypes, R as ROOT_LE, v as useExperimentsContext } from "./StoreProvider-CqQrndhS.js";
import { u as useAssociatedLegalEntity } from "./useAssociatedLegalEntity-C4OxYq45.js";
import { skipToken } from "@tanstack/preact-query";
import { g as getLegalEntityNameBasedOnType } from "./getName-CiCWopsp.js";
const useExemptSettlor = ({
  trust
}) => {
  const matchingAssociation = useMemo(() => {
    if (!trust) return void 0;
    return getOwnEntityAssociations(trust).find(
      (association) => association.type === TrustMemberTypes.SETTLOR && Boolean(association.settlorExemptionReason)
    );
  }, [trust]);
  const { data } = useLegalEntity(matchingAssociation?.legalEntityId ?? skipToken);
  return data;
};
const splitAtFirstOccurrence = (str, separator) => {
  const firstOccurrence = str.indexOf(separator);
  if (firstOccurrence === -1) return [str];
  return [str.slice(0, firstOccurrence), str.slice(firstOccurrence + 1)];
};
const getTrustMemberOwnerId = (trustMemberId, entityAssociations) => entityAssociations.find((association) => association.associatorId === trustMemberId)?.legalEntityId;
const getFallbackName = (exemptSettlorAssociation) => {
  const [firstName, lastName] = splitAtFirstOccurrence(exemptSettlorAssociation.name ?? "", " ");
  return {
    firstName,
    lastName
  };
};
const 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)}`);
};
const 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: [.../* @__PURE__ */ new Set([...trustMember.roles, trustMemberType])]
      };
      return acc;
    },
    {}
  );
  return Object.values(mappedTrustMembers);
};
const mapRootLegalEntityToTrustMember = (rootLegalEntity) => ({
  trustMemberType: "rootTrustee",
  roles: [TrustMemberTypes.TRUSTEE],
  legalEntityType: rootLegalEntity.type,
  legalEntityId: rootLegalEntity.id,
  name: getLegalEntityNameBasedOnType(rootLegalEntity)
});
const mapUndefinedBeneficiaryInfoToTrustMembers = (undefinedBeneficiaryInfo) => undefinedBeneficiaryInfo.map(({ reference, description }) => ({
  trustMemberType: "undefinedBeneficiary",
  roles: [TrustMemberTypes.UNDEFINED_BENEFICIARY],
  reference,
  description
}));
const getTrustMembers = (trustLE, rootLegalEntity, exemptSettlorLE) => [
  mapRootLegalEntityToTrustMember(rootLegalEntity),
  ...mapEntityAssociationsToTrustMembers(
    trustLE.id,
    trustLE.entityAssociations ?? [],
    exemptSettlorLE
  ),
  ...mapUndefinedBeneficiaryInfoToTrustMembers(trustLE.trust?.undefinedBeneficiaryInfo ?? [])
];
const combineRootLegalEntityWithEntityAssociations = (rootLegalEntity, entityAssociations) => {
  const allowedRolesForRootLegalEntity = [
    TrustMemberTypes.SETTLOR,
    TrustMemberTypes.PROTECTOR,
    TrustMemberTypes.DEFINED_BENEFICIARY
  ];
  const roles = entityAssociations.map((entityAssociation) => entityAssociation.type).filter((role) => allowedRolesForRootLegalEntity.includes(role));
  return {
    trustMemberType: "rootTrustee",
    roles: [...roles, TrustMemberTypes.TRUSTEE],
    legalEntityType: rootLegalEntity.type,
    legalEntityId: rootLegalEntity.id,
    name: getLegalEntityNameBasedOnType(rootLegalEntity)
  };
};
const 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 ?? [])
  ];
};
function useTrustMembers() {
  const { data: rootLegalEntity } = useLegalEntity(ROOT_LE);
  const { data: trust } = useAssociatedLegalEntity(rootLegalEntity, LegalEntityTypes.TRUST);
  const { isExperimentEnabled } = useExperimentsContext();
  const exemptSettlor = useExemptSettlor({
    trust
  });
  const allowMoreRolesForMainRootTrustee = isExperimentEnabled("AllowMoreRolesForMainRootTrustee");
  return useMemo(() => {
    if (!rootLegalEntity || !trust?.trust) return [];
    return allowMoreRolesForMainRootTrustee ? getRootTrusteeTrustMembers(trust, rootLegalEntity, exemptSettlor) : getTrustMembers(trust, rootLegalEntity, exemptSettlor);
  }, [rootLegalEntity, trust, exemptSettlor, allowMoreRolesForMainRootTrustee]);
}
export {
  useTrustMembers as u
};
