import { hasFeature } from "@open-mercato/shared/security/features";
import { getStaffMemberByUserId } from "../lib/staffMemberResolver.js";
async function hasManageAllFeature(context) {
  if (hasFeature(context.userFeatures, "staff.timesheets.manage_all")) return true;
  try {
    const rbac = context.container.resolve("rbacService");
    if (rbac?.userHasAllFeatures) {
      return await rbac.userHasAllFeatures(
        context.userId,
        ["staff.timesheets.manage_all"],
        { tenantId: context.tenantId ?? null, organizationId: context.organizationId ?? null }
      );
    }
  } catch {
  }
  return false;
}
const interceptors = [
  {
    id: "staff.timesheets.self-scope-widget-data",
    targetRoute: "dashboards/widgets/data",
    methods: ["POST"],
    priority: 70,
    async before(request, context) {
      const entityType = request.body?.entityType;
      if (entityType !== "staff:staff_time_entries") {
        return { ok: true };
      }
      if (await hasManageAllFeature(context)) {
        return { ok: true };
      }
      const staffMember = await getStaffMemberByUserId(
        context.em,
        context.userId,
        context.tenantId ?? null,
        context.organizationId ?? null
      );
      if (!staffMember) {
        return {
          ok: false,
          statusCode: 403,
          message: "User is not a staff member."
        };
      }
      const existingFilters = Array.isArray(request.body?.filters) ? request.body.filters : [];
      const otherFilters = existingFilters.filter(
        (f) => f.field !== "staffMemberId"
      );
      return {
        ok: true,
        body: {
          ...request.body,
          filters: [
            ...otherFilters,
            { field: "staffMemberId", operator: "eq", value: staffMember.id }
          ]
        }
      };
    }
  },
  {
    id: "staff.timesheets.self-scope-time-entries",
    targetRoute: "staff/timesheets/time-entries",
    methods: ["GET"],
    priority: 70,
    async before(request, context) {
      if (await hasManageAllFeature(context)) {
        return { ok: true };
      }
      const staffMember = await getStaffMemberByUserId(
        context.em,
        context.userId,
        context.tenantId ?? null,
        context.organizationId ?? null
      );
      if (!staffMember) {
        return {
          ok: false,
          statusCode: 403,
          message: "User is not a staff member."
        };
      }
      return {
        ok: true,
        query: {
          ...request.query ?? {},
          staffMemberId: staffMember.id
        }
      };
    }
  }
];
export {
  interceptors
};
//# sourceMappingURL=interceptors.js.map
