import { Role } from "@open-mercato/core/modules/auth/data/entities";
import { DashboardRoleWidgets } from "../data/entities.js";
import { loadAllWidgets } from "./widgets.js";
async function findRoleByName(em, roleName, tenantId) {
  const tenantRole = await em.findOne(Role, { name: roleName, tenantId });
  if (tenantRole) return tenantRole;
  return em.findOne(Role, { name: roleName, tenantId: null });
}
async function resolveAnalyticsWidgetIds() {
  const widgets = await loadAllWidgets();
  return widgets.filter((widget) => widget.metadata.category === "analytics" || widget.metadata.id.startsWith("dashboards.analytics.")).map((widget) => widget.metadata.id);
}
async function appendWidgetsToRoles(em, { tenantId, organizationId = null, roleNames, widgetIds }) {
  const trimmedTenantId = tenantId.trim();
  const widgets = await loadAllWidgets();
  const validWidgetIds = new Set(widgets.map((widget) => widget.metadata.id));
  const resolvedWidgetIds = widgetIds.filter((id) => validWidgetIds.has(id));
  if (!resolvedWidgetIds.length) return false;
  let updated = false;
  await em.transactional(async (tem) => {
    for (const roleName of roleNames) {
      const role = await findRoleByName(tem, roleName, trimmedTenantId);
      if (!role) continue;
      const record = await tem.findOne(DashboardRoleWidgets, {
        roleId: String(role.id),
        tenantId: trimmedTenantId,
        organizationId,
        deletedAt: null
      });
      const roleRecord = record ?? (organizationId ? await tem.findOne(DashboardRoleWidgets, {
        roleId: String(role.id),
        tenantId: trimmedTenantId,
        organizationId: null,
        deletedAt: null
      }) : null);
      if (!roleRecord) continue;
      const current = Array.isArray(roleRecord.widgetIdsJson) ? roleRecord.widgetIdsJson : [];
      const next = [...current];
      const existing = new Set(current);
      for (const widgetId of resolvedWidgetIds) {
        if (existing.has(widgetId)) continue;
        existing.add(widgetId);
        next.push(widgetId);
      }
      if (next.length === current.length) continue;
      roleRecord.widgetIdsJson = next;
      roleRecord.updatedAt = /* @__PURE__ */ new Date();
      tem.persist(roleRecord);
      updated = true;
    }
  });
  return updated;
}
export {
  appendWidgetsToRoles,
  resolveAnalyticsWidgetIds
};
//# sourceMappingURL=role-widgets.js.map
