import { Organization } from "@open-mercato/core/modules/directory/data/entities";
import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { CustomerEntityRole, CustomerPersonCompanyRole } from "../data/entities.js";
function getUsageKey(organizationId, value) {
  return `${organizationId}::${value}`;
}
async function loadOrganizationScopeMap(em, tenantId, organizationIds) {
  const uniqueOrgIds = Array.from(new Set(organizationIds.filter((value) => value.trim().length > 0)));
  if (uniqueOrgIds.length === 0) return /* @__PURE__ */ new Map();
  const organizationFilter = {
    tenant: tenantId,
    id: { $in: uniqueOrgIds },
    deletedAt: null
  };
  const organizations = await findWithDecryption(
    em,
    Organization,
    organizationFilter,
    { fields: ["id", "descendantIds"] },
    { tenantId, organizationId: null }
  );
  const scopeMap = /* @__PURE__ */ new Map();
  organizations.forEach((organization) => {
    const scopeIds = /* @__PURE__ */ new Set([String(organization.id)]);
    if (Array.isArray(organization.descendantIds)) {
      organization.descendantIds.forEach((descendantId) => {
        if (typeof descendantId === "string" && descendantId.trim().length > 0) {
          scopeIds.add(descendantId.trim());
        }
      });
    }
    scopeMap.set(String(organization.id), Array.from(scopeIds));
  });
  uniqueOrgIds.forEach((organizationId) => {
    if (!scopeMap.has(organizationId)) {
      scopeMap.set(organizationId, [organizationId]);
    }
  });
  return scopeMap;
}
async function loadRoleTypeUsageMap(em, params) {
  const normalizedEntries = Array.from(
    new Map(
      params.entries.map((entry) => {
        const organizationId = entry.organizationId.trim();
        const value = entry.value.trim();
        if (!organizationId || !value) return null;
        return [getUsageKey(organizationId, value), { organizationId, value }];
      }).filter((entry) => entry !== null)
    ).values()
  );
  if (normalizedEntries.length === 0) return /* @__PURE__ */ new Map();
  const scopeMap = await loadOrganizationScopeMap(
    em,
    params.tenantId,
    normalizedEntries.map((entry) => entry.organizationId)
  );
  const usageEntries = await Promise.all(
    normalizedEntries.map(async (entry) => {
      const organizationScopeIds = scopeMap.get(entry.organizationId) ?? [entry.organizationId];
      const [ownerAssignments, relationshipAssignments] = await Promise.all([
        em.count(CustomerEntityRole, {
          tenantId: params.tenantId,
          organizationId: { $in: organizationScopeIds },
          roleType: entry.value
        }),
        em.count(CustomerPersonCompanyRole, {
          tenantId: params.tenantId,
          organizationId: { $in: organizationScopeIds },
          roleValue: entry.value
        })
      ]);
      return [
        getUsageKey(entry.organizationId, entry.value),
        {
          total: ownerAssignments + relationshipAssignments,
          ownerAssignments,
          relationshipAssignments
        }
      ];
    })
  );
  return new Map(usageEntries);
}
async function loadRoleTypeUsage(em, params) {
  const usageMap = await loadRoleTypeUsageMap(em, {
    tenantId: params.tenantId,
    entries: [{ organizationId: params.organizationId, value: params.value }]
  });
  return usageMap.get(getUsageKey(params.organizationId, params.value)) ?? {
    total: 0,
    ownerAssignments: 0,
    relationshipAssignments: 0
  };
}
function resolveRoleTypeUsageKey(organizationId, value) {
  return getUsageKey(organizationId, value);
}
export {
  loadRoleTypeUsage,
  loadRoleTypeUsageMap,
  resolveRoleTypeUsageKey
};
//# sourceMappingURL=roleTypeUsage.js.map
