import { d as defineEventHandler, g as getCookie, c as createError } from '../../nitro/nitro.mjs';
import { b as verifyToken, r as readUsers } from '../../_/auth.mjs';
import { r as readMembers } from '../../_/members.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';
import 'bcryptjs';
import 'jsonwebtoken';
import 'fs';
import 'path';
import '../../_/encryption.mjs';
import 'crypto';

const members_get = defineEventHandler(async (event) => {
  var _a, _b, _c;
  try {
    const token = getCookie(event, "auth_token");
    if (!token) {
      throw createError({
        statusCode: 401,
        message: "Nicht authentifiziert."
      });
    }
    const decoded = verifyToken(token);
    if (!decoded) {
      throw createError({
        statusCode: 401,
        message: "Ung\xFCltiges Token."
      });
    }
    const manualMembers = await readMembers();
    const registeredUsers = await readUsers();
    const mergedMembers = [];
    const processedEmails = /* @__PURE__ */ new Set();
    const processedNames = /* @__PURE__ */ new Set();
    for (const member of manualMembers) {
      const normalizedEmail = ((_a = member.email) == null ? void 0 : _a.toLowerCase().trim()) || "";
      const fullName = `${member.firstName || ""} ${member.lastName || ""}`.trim();
      const normalizedName = fullName.toLowerCase();
      mergedMembers.push({
        ...member,
        name: fullName,
        // Computed for display
        source: "manual",
        editable: true,
        hasLogin: false
      });
      if (normalizedEmail) processedEmails.add(normalizedEmail);
      if (normalizedName) processedNames.add(normalizedName);
    }
    for (const user of registeredUsers) {
      if (!user.active) continue;
      const normalizedEmail = ((_b = user.email) == null ? void 0 : _b.toLowerCase().trim()) || "";
      const normalizedName = ((_c = user.name) == null ? void 0 : _c.toLowerCase().trim()) || "";
      let matchedManualIndex = -1;
      if (normalizedEmail) {
        matchedManualIndex = mergedMembers.findIndex(
          (m) => {
            var _a2;
            return m.source === "manual" && ((_a2 = m.email) == null ? void 0 : _a2.toLowerCase().trim()) === normalizedEmail;
          }
        );
      }
      if (matchedManualIndex === -1 && normalizedName) {
        matchedManualIndex = mergedMembers.findIndex(
          (m) => {
            var _a2;
            return m.source === "manual" && ((_a2 = m.name) == null ? void 0 : _a2.toLowerCase().trim()) === normalizedName;
          }
        );
      }
      if (matchedManualIndex !== -1) {
        mergedMembers[matchedManualIndex] = {
          ...mergedMembers[matchedManualIndex],
          hasLogin: true,
          loginEmail: user.email,
          loginRole: user.role,
          lastLogin: user.lastLogin
        };
      } else {
        mergedMembers.push({
          id: user.id,
          name: user.name,
          email: user.email,
          phone: user.phone || "",
          address: "",
          notes: `Rolle: ${user.role}`,
          source: "login",
          editable: false,
          hasLogin: true,
          loginEmail: user.email,
          loginRole: user.role,
          lastLogin: user.lastLogin
        });
      }
    }
    mergedMembers.sort((a, b) => a.name.localeCompare(b.name));
    return {
      success: true,
      members: mergedMembers
    };
  } catch (error) {
    console.error("Fehler beim Abrufen der Mitgliederliste:", error);
    throw error;
  }
});

export { members_get as default };
//# sourceMappingURL=members.get.mjs.map
