import { readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
function isAssignableEndpointMissing(error) {
  return typeof error === "object" && error !== null && error.status === 404;
}
async function fetchAssignableStaffMembersPage(query, options) {
  const page = options?.page ?? 1;
  const pageSize = options?.pageSize ?? 24;
  const params = new URLSearchParams();
  params.set("page", String(page));
  params.set("pageSize", String(pageSize));
  const normalizedQuery = query.trim();
  if (normalizedQuery.length > 0) {
    params.set("search", normalizedQuery);
  }
  let data;
  try {
    data = await readApiResultOrThrow(
      `/api/staff/team-members/assignable?${params.toString()}`,
      options?.signal ? { signal: options.signal } : void 0
    );
  } catch (error) {
    if (isAssignableEndpointMissing(error)) {
      return { items: [], total: 0, page, pageSize };
    }
    throw error;
  }
  const rawItems = Array.isArray(data?.items) ? data.items : [];
  const deduped = /* @__PURE__ */ new Map();
  for (const item of rawItems) {
    const userId = typeof item?.userId === "string" ? item.userId : typeof item?.user_id === "string" ? item.user_id : null;
    if (!userId || deduped.has(userId)) continue;
    const user = item?.user && typeof item.user === "object" ? item.user : null;
    const team = item?.team && typeof item.team === "object" ? item.team : null;
    const displayName = typeof item?.displayName === "string" && item.displayName.trim().length > 0 ? item.displayName.trim() : typeof item?.display_name === "string" && item.display_name.trim().length > 0 ? item.display_name.trim() : null;
    const email = user && typeof user.email === "string" && user.email.trim().length > 0 ? user.email.trim() : typeof item?.email === "string" && item.email.trim().length > 0 ? item.email.trim() : null;
    const teamName = typeof item?.teamName === "string" && item.teamName.trim().length > 0 ? item.teamName.trim() : typeof item?.team_name === "string" && item.team_name.trim().length > 0 ? item.team_name.trim() : team && typeof team.name === "string" && team.name.trim().length > 0 ? team.name.trim() : null;
    const teamMemberId = typeof item?.teamMemberId === "string" ? item.teamMemberId : typeof item?.team_member_id === "string" ? item.team_member_id : typeof item?.id === "string" ? item.id : userId;
    deduped.set(userId, {
      teamMemberId,
      userId,
      displayName: displayName ?? email ?? userId,
      email,
      teamName
    });
  }
  return {
    items: Array.from(deduped.values()),
    total: typeof data?.total === "number" && Number.isFinite(data.total) ? data.total : deduped.size,
    page: typeof data?.page === "number" && Number.isFinite(data.page) ? data.page : page,
    pageSize: typeof data?.pageSize === "number" && Number.isFinite(data.pageSize) ? data.pageSize : pageSize
  };
}
async function fetchAssignableStaffMembers(query, options) {
  const result = await fetchAssignableStaffMembersPage(query, options);
  return result.items;
}
function mapAssignableStaffToFilterOptions(items) {
  return items.map((item) => ({
    value: item.userId,
    label: item.email && item.email !== item.displayName ? `${item.displayName} (${item.email})` : item.displayName,
    tone: "neutral"
  }));
}
function ensureCurrentUserFilterOption(options, currentUserId, fallbackLabel) {
  const trimmed = currentUserId.trim();
  if (!trimmed || options.some((option) => option.value === trimmed)) return options;
  return [{ value: trimmed, label: fallbackLabel, tone: "neutral" }, ...options];
}
export {
  ensureCurrentUserFilterOption,
  fetchAssignableStaffMembers,
  fetchAssignableStaffMembersPage,
  mapAssignableStaffToFilterOptions
};
//# sourceMappingURL=assignableStaff.js.map
