import { expect } from "@playwright/test";
import { randomInt } from "node:crypto";
import { apiRequest } from "./api.js";
import { getTokenContext } from "./generalFixtures.js";
async function createCurrencyFixture(request, token, input) {
  const { organizationId, tenantId } = getTokenContext(token);
  const response = await apiRequest(request, "POST", "/api/currencies/currencies", {
    token,
    data: { organizationId, tenantId, code: input.code, name: input.name, symbol: input.symbol ?? null }
  });
  expect(response.ok(), `Failed to create currency fixture: ${response.status()}`).toBeTruthy();
  const body = await response.json();
  expect(typeof body.id === "string" && body.id.length > 0).toBeTruthy();
  return body.id;
}
const SEEDED_CURRENCY_CODES = /* @__PURE__ */ new Set([
  "USD",
  "EUR",
  "JPY",
  "GBP",
  "CHF",
  "CAD",
  "AUD",
  "CNY",
  "CNH",
  "PLN"
]);
const reservedCurrencyCodes = /* @__PURE__ */ new Set();
function generateUniqueCurrencyCode() {
  const letter = () => String.fromCharCode(65 + randomInt(26));
  for (let attempt = 0; attempt < 200; attempt += 1) {
    const code = `${letter()}${letter()}${letter()}`;
    if (!SEEDED_CURRENCY_CODES.has(code) && !reservedCurrencyCodes.has(code)) {
      reservedCurrencyCodes.add(code);
      return code;
    }
  }
  throw new Error("[internal] exhausted unique currency code space");
}
async function createRandomCurrencyFixture(request, token, input) {
  const { organizationId, tenantId } = getTokenContext(token);
  let lastStatus = 0;
  for (let attempt = 0; attempt < 8; attempt += 1) {
    const code = generateUniqueCurrencyCode();
    const data = {
      organizationId,
      tenantId,
      code,
      name: input.name,
      symbol: input.symbol ?? null
    };
    if (typeof input.isActive === "boolean") data.isActive = input.isActive;
    const response = await apiRequest(request, "POST", "/api/currencies/currencies", { token, data });
    if (response.status() === 201) {
      const body = await response.json();
      if (typeof body.id === "string" && body.id.length > 0) {
        return { id: body.id, code };
      }
    }
    lastStatus = response.status();
  }
  throw new Error(`[internal] failed to create currency fixture after retries (last status ${lastStatus})`);
}
async function createFetchConfigFixture(request, token, input) {
  const response = await apiRequest(request, "POST", "/api/currencies/fetch-configs", {
    token,
    data: input
  });
  expect(response.ok(), `Failed to create fetch config fixture: ${response.status()}`).toBeTruthy();
  const body = await response.json();
  const id = body.config?.id;
  expect(typeof id === "string" && id.length > 0).toBeTruthy();
  return id;
}
async function deleteCurrenciesEntityIfExists(request, token, path, id) {
  if (!token || !id) return;
  try {
    await apiRequest(request, "DELETE", `${path}?id=${encodeURIComponent(id)}`, { token });
  } catch {
    return;
  }
}
export {
  createCurrencyFixture,
  createFetchConfigFixture,
  createRandomCurrencyFixture,
  deleteCurrenciesEntityIfExists,
  generateUniqueCurrencyCode
};
//# sourceMappingURL=currenciesFixtures.js.map
