import { expect } from "@playwright/test";
import { apiRequest, getAuthToken } from "./api.js";
import { expectId, readJsonSafe } from "./generalFixtures.js";
const BASE_URL = process.env.BASE_URL?.trim() || null;
function resolveUrl(path) {
  return BASE_URL ? `${BASE_URL}${path}` : path;
}
async function apiRequestWithSelectedOrg(request, method, path, options) {
  const headers = {
    Authorization: `Bearer ${options.token}`,
    "Content-Type": "application/json",
    Cookie: `om_selected_org=${options.selectedOrgId}`
  };
  return request.fetch(resolveUrl(path), { method, headers, data: options.data });
}
async function createRoleFixture(request, token, input) {
  const payload = {
    name: input.name
  };
  if (typeof input.tenantId === "string" && input.tenantId.length > 0) {
    payload.tenantId = input.tenantId;
  }
  const response = await apiRequest(request, "POST", "/api/auth/roles", {
    token,
    data: payload
  });
  const body = await readJsonSafe(response);
  expect(response.status(), "POST /api/auth/roles should return 201").toBe(201);
  return expectId(body?.id, "Role creation response should include id");
}
async function deleteRoleIfExists(request, token, roleId) {
  if (!token || !roleId) return;
  await apiRequest(request, "DELETE", `/api/auth/roles?id=${encodeURIComponent(roleId)}`, { token }).catch(() => void 0);
}
async function createUserFixture(request, token, input) {
  const response = await apiRequest(request, "POST", "/api/auth/users", {
    token,
    data: input
  });
  const body = await readJsonSafe(response);
  expect(response.status(), "POST /api/auth/users should return 201").toBe(201);
  return expectId(body?.id, "User creation response should include id");
}
async function deleteUserIfExists(request, token, userId) {
  if (!token || !userId) return;
  await apiRequest(request, "DELETE", `/api/auth/users?id=${encodeURIComponent(userId)}`, { token }).catch(() => void 0);
}
async function createOrganizationFixture(request, token, input) {
  const payload = { name: input.name };
  if (typeof input.tenantId === "string" && input.tenantId.length > 0) {
    payload.tenantId = input.tenantId;
  }
  const response = await apiRequest(request, "POST", "/api/directory/organizations", {
    token,
    data: payload
  });
  const body = await readJsonSafe(response);
  expect(response.status(), "POST /api/directory/organizations should return 201").toBe(201);
  return expectId(body?.id, "Organization creation response should include id");
}
async function deleteOrganizationIfExists(request, token, organizationId) {
  if (!token || !organizationId) return;
  await apiRequest(request, "DELETE", "/api/directory/organizations", {
    token,
    data: { id: organizationId }
  }).catch(() => void 0);
}
async function setRoleAclFeatures(request, token, input) {
  const payload = {
    roleId: input.roleId,
    features: input.features
  };
  if (input.organizations !== void 0) {
    payload.organizations = input.organizations;
  }
  const response = await apiRequest(request, "PUT", "/api/auth/roles/acl", {
    token,
    data: payload
  });
  const body = await readJsonSafe(response);
  expect(response.status(), "PUT /api/auth/roles/acl should return 200").toBe(200);
  expect(body?.ok, "Role ACL update should report ok=true").toBe(true);
}
async function setUserAclVisibility(request, token, input) {
  const payload = {
    userId: input.userId,
    organizations: input.organizations
  };
  if (input.features !== void 0) {
    payload.features = input.features;
  }
  const response = await apiRequest(request, "PUT", "/api/auth/users/acl", {
    token,
    data: payload
  });
  const body = await readJsonSafe(response);
  expect(response.status(), "PUT /api/auth/users/acl should return 200").toBe(200);
  expect(body?.ok, "User ACL update should report ok=true").toBe(true);
}
export {
  apiRequestWithSelectedOrg,
  createOrganizationFixture,
  createRoleFixture,
  createUserFixture,
  deleteOrganizationIfExists,
  deleteRoleIfExists,
  deleteUserIfExists,
  getAuthToken,
  setRoleAclFeatures,
  setUserAclVisibility
};
//# sourceMappingURL=authFixtures.js.map
