import { Q as useSession, S as clearResponseHeaders, V as getHeader, U as User } from '../nitro/nitro.mjs';
import { v as verifyJwtToken } from './jwt.mjs';

async function getUser(event) {
  let session;
  if (typeof getUserSession === "undefined") {
    session = await useSession(event, { password: process.env.NUXT_SESSION_PASSWORD, name: "nuxt-session" });
    session = session.data;
  } else {
    session = await getUserSession(event);
  }
  let user = session?.user;
  if (!user) {
    clearResponseHeaders(event, ["set-cookie"]);
    const authHeader = getHeader(event, "authorization");
    const token = authHeader?.split(" ")[1];
    if (token) {
      try {
        const payload = await verifyJwtToken(token);
        user = {
          id: payload.id,
          email: payload.email,
          role: payload.role
        };
      } catch (err) {
        console.warn("Invalid JWT:", err);
      }
    }
  }
  if (!user) return;
  const u = await User.findByPk(user.id, { where: { is_active: true }, attributes: ["id", "email", "role"], raw: true }).then((u2) => u2 || void 0).catch((err) => {
    console.warn("Error finding user by id:", err);
    return void 0;
  });
  return u;
}

export { getUser as g };
