function hasFiniteCoordinates(address) {
  return typeof address.latitude === "number" && Number.isFinite(address.latitude) && typeof address.longitude === "number" && Number.isFinite(address.longitude);
}
function toSortableTime(value) {
  if (value instanceof Date) return value.getTime();
  if (typeof value === "string") {
    const parsed = new Date(value).getTime();
    return Number.isNaN(parsed) ? Number.MAX_SAFE_INTEGER : parsed;
  }
  return Number.MAX_SAFE_INTEGER;
}
function compareCandidates(a, b) {
  if (a.isPrimary !== b.isPrimary) return a.isPrimary ? -1 : 1;
  const timeDiff = toSortableTime(a.createdAt) - toSortableTime(b.createdAt);
  if (timeDiff !== 0) return timeDiff;
  return a.id.localeCompare(b.id);
}
function groupLinkEntityIds(links) {
  const entityIdsByDeal = /* @__PURE__ */ new Map();
  for (const link of links) {
    if (!link.dealId || !link.entityId) continue;
    const bucket = entityIdsByDeal.get(link.dealId) ?? [];
    if (!bucket.includes(link.entityId)) bucket.push(link.entityId);
    entityIdsByDeal.set(link.dealId, bucket);
  }
  return entityIdsByDeal;
}
function pickLocation(entityIds, addressesByEntity, source) {
  if (!entityIds || entityIds.length === 0) return null;
  let best = null;
  for (const entityId of entityIds) {
    const candidate = addressesByEntity.get(entityId)?.[0];
    if (!candidate) continue;
    if (!best || compareCandidates(candidate, best) < 0) best = candidate;
  }
  if (!best) return null;
  return {
    latitude: best.latitude,
    longitude: best.longitude,
    city: best.city ?? null,
    region: best.region ?? null,
    country: best.country ?? null,
    source,
    entityId: best.entityId,
    addressId: best.id
  };
}
function resolveDealLocations(dealIds, companyLinks, personLinks, addresses) {
  const addressesByEntity = /* @__PURE__ */ new Map();
  for (const address of addresses) {
    if (!address.id || !address.entityId) continue;
    if (!hasFiniteCoordinates(address)) continue;
    const bucket = addressesByEntity.get(address.entityId) ?? [];
    bucket.push(address);
    addressesByEntity.set(address.entityId, bucket);
  }
  for (const bucket of addressesByEntity.values()) {
    bucket.sort(compareCandidates);
  }
  const companyEntityIdsByDeal = groupLinkEntityIds(companyLinks);
  const personEntityIdsByDeal = groupLinkEntityIds(personLinks);
  const locations = /* @__PURE__ */ new Map();
  for (const dealId of dealIds) {
    const companyLocation = pickLocation(companyEntityIdsByDeal.get(dealId), addressesByEntity, "company");
    locations.set(
      dealId,
      companyLocation ?? pickLocation(personEntityIdsByDeal.get(dealId), addressesByEntity, "person")
    );
  }
  return locations;
}
export {
  resolveDealLocations
};
//# sourceMappingURL=dealsMapLocation.js.map
