import { expect } from "@playwright/test";
import { apiRequest } from "./api.js";
function readTokenPayload(token) {
  const parts = token.split(".");
  return JSON.parse(Buffer.from(parts[1], "base64url").toString());
}
function getTokenContext(token) {
  const payload = readTokenPayload(token);
  return { organizationId: payload.orgId ?? "", tenantId: payload.tenantId ?? "" };
}
function getTokenScope(token) {
  const payload = readTokenPayload(token);
  return {
    organizationId: payload.orgId ?? "",
    tenantId: payload.tenantId ?? "",
    userId: payload.sub ?? ""
  };
}
async function readJsonSafe(response) {
  const raw = await response.text();
  if (!raw) return null;
  try {
    return JSON.parse(raw);
  } catch {
    return null;
  }
}
function expectId(value, message) {
  expect(typeof value === "string" && value.length > 0, message).toBe(true);
  return value;
}
async function deleteEntityByPathIfExists(request, token, fullPath) {
  if (!token || !fullPath) return;
  try {
    await apiRequest(request, "DELETE", fullPath, { token });
  } catch {
    return;
  }
}
async function deleteGeneralEntityIfExists(request, token, path, id) {
  if (!token || !id) return;
  try {
    await apiRequest(request, "DELETE", `${path}?id=${encodeURIComponent(id)}`, { token });
  } catch {
    return;
  }
}
export {
  deleteEntityByPathIfExists,
  deleteGeneralEntityIfExists,
  expectId,
  getTokenContext,
  getTokenScope,
  readJsonSafe
};
//# sourceMappingURL=generalFixtures.js.map
