import { resolveOrganizationScopeForRequest } from "@open-mercato/core/modules/directory/utils/organizationScope";
import { CustomFieldDef } from "@open-mercato/core/modules/entities/data/entities";
function resolveDefinitionScopeFromOrganizationScope(auth, scope) {
  const authTenantId = auth.tenantId ?? null;
  const scopeTenantId = scope.tenantId ?? null;
  const tenantMismatch = Boolean(authTenantId && scopeTenantId && authTenantId !== scopeTenantId);
  return {
    tenantId: tenantMismatch ? authTenantId : scopeTenantId ?? authTenantId,
    organizationId: tenantMismatch ? auth.orgId ?? null : scope.selectedId ?? auth.orgId ?? null
  };
}
async function resolveDefinitionMutationScope({
  auth,
  container,
  request
}) {
  const scope = await resolveOrganizationScopeForRequest({ container, auth, request });
  return resolveDefinitionScopeFromOrganizationScope(auth, scope);
}
function createExactDefinitionWhere(entityId, key, scope) {
  return {
    entityId,
    key,
    organizationId: scope.organizationId,
    tenantId: scope.tenantId
  };
}
function createVisibleDefinitionWhere(entityId, key, scope, options = {}) {
  const organizationCandidates = [{ organizationId: null }];
  if (scope.organizationId) organizationCandidates.unshift({ organizationId: scope.organizationId });
  const tenantCandidates = [{ tenantId: null }];
  if (scope.tenantId) tenantCandidates.unshift({ tenantId: scope.tenantId });
  return {
    entityId,
    key,
    ...options,
    $and: [
      { $or: organizationCandidates },
      { $or: tenantCandidates }
    ]
  };
}
function definitionTimestamp(def) {
  const updatedAt = def?.updatedAt instanceof Date ? def.updatedAt.getTime() : def?.updatedAt ? new Date(def.updatedAt).getTime() : 0;
  if (updatedAt) return updatedAt;
  return def?.createdAt instanceof Date ? def.createdAt.getTime() : def?.createdAt ? new Date(def.createdAt).getTime() : 0;
}
function definitionScopeScore(def) {
  return (def?.tenantId ? 2 : 0) + (def?.organizationId ? 1 : 0);
}
function selectVisibleDefinitionWinner(definitions) {
  let winner = null;
  for (const definition of definitions) {
    if (!winner) {
      winner = definition;
      continue;
    }
    const nextScore = definitionScopeScore(definition);
    const winnerScore = definitionScopeScore(winner);
    if (nextScore > winnerScore) {
      winner = definition;
      continue;
    }
    if (nextScore < winnerScore) continue;
    if (definitionTimestamp(definition) >= definitionTimestamp(winner)) winner = definition;
  }
  return winner;
}
function markDefinitionTombstoned(definition, now = /* @__PURE__ */ new Date()) {
  definition.isActive = false;
  definition.deletedAt = definition.deletedAt ?? now;
  definition.updatedAt = now;
  return definition;
}
function cloneConfigJson(configJson) {
  if (configJson == null) return configJson;
  return JSON.parse(JSON.stringify(configJson));
}
function createScopedDefinitionTombstone(em, source, scope, now = /* @__PURE__ */ new Date()) {
  return em.create(CustomFieldDef, {
    entityId: source.entityId,
    key: source.key,
    kind: source.kind,
    configJson: cloneConfigJson(source.configJson) ?? {},
    organizationId: scope.organizationId,
    tenantId: scope.tenantId,
    isActive: false,
    createdAt: now,
    updatedAt: now,
    deletedAt: now
  });
}
export {
  createExactDefinitionWhere,
  createScopedDefinitionTombstone,
  createVisibleDefinitionWhere,
  markDefinitionTombstoned,
  resolveDefinitionMutationScope,
  resolveDefinitionScopeFromOrganizationScope,
  selectVisibleDefinitionWinner
};
//# sourceMappingURL=definition-scope.js.map
