/**
 * Request logging utility for MSW handlers.
 * Captures request details for test assertions,
 * replacing vi.fn() call verification.
 * @module mocks/request-log
 */

/** @type {Array<{method: string, url: string, body: any}>} */
let log = [];

/**
 * Records a request in the log. Called from MSW handlers.
 * @param {string} method - HTTP method.
 * @param {string} url - Request URL path.
 * @param {any} [body] - Parsed request body.
 */
export function captureRequest(method, url, body = undefined) {
  log.push({ method, url, body });
}

/**
 * Returns a copy of all logged requests.
 * @returns {Array<{method: string, url: string, body: any}>}
 */
export function getRequests() {
  return [...log];
}

/**
 * Clears the request log. Call in afterEach.
 */
export function clearRequests() {
  log = [];
}

/**
 * Finds logged requests matching the given method and URL pattern.
 * @param {string} method - HTTP method to match.
 * @param {string|RegExp} urlPattern - URL string or regex to match.
 * @returns {Array<{method: string, url: string, body: any}>}
 */
export function findRequest(method, urlPattern) {
  return log.filter((r) => {
    if (r.method !== method) return false;
    if (typeof urlPattern === "string") return r.url === urlPattern;
    return urlPattern.test(r.url);
  });
}
