import { matchRequestUrl } from "msw";
import { server, LOGGING_V2_API } from "./server.js";
import { Environment } from "../types.js";
import { logger, http } from "../index.js";
import { browserDetails } from "../browserDetails.js";
const HTTP_TRANSPORT = http(LOGGING_V2_API);
const CORRELATION_ID = "123456789";
jest.mock("uuid", () => ({ v4: () => "123456789" }));
const LOGGER_OPTIONS = {
  index: "pui",
  environment: Environment.QA,
  team: "ui-platform",
  appName: "pui-diagnostics",
  appVersion: "1.0.0"
};
const BASE_LOG_OBJECT = {
  host: "NA",
  environment: Environment.DEV,
  appVersion: "",
  location: "NA",
  instanceId: "",
  customerId: "",
  userId: "",
  correlationId: CORRELATION_ID,
  context: browserDetails
};
const waitForRequest = (method, url) => {
  let requestId = "";
  return new Promise((resolve, reject) => {
    server.events.on("request:start", (req) => {
      const matchesMethod = req.method.toLowerCase() === method.toLowerCase();
      const matchesUrl = matchRequestUrl(req.url, url).matches;
      if (matchesMethod && matchesUrl) {
        requestId = req.id;
      }
    });
    server.events.on("request:match", (req) => {
      if (req.id === requestId) {
        resolve(req);
        requestId = "";
      }
    });
    server.events.on("request:unhandled", (req) => {
      if (req.id === requestId) {
        reject(
          new Error(`The ${req.method} ${req.url.href} request was unhandled.`)
        );
        requestId = "";
      }
    });
  });
};
const waitForLoggingRequest = () => waitForRequest("POST", LOGGING_V2_API);
const getLogger = (options = {}) => logger({ ...LOGGER_OPTIONS, transport: HTTP_TRANSPORT, ...options });
export {
  BASE_LOG_OBJECT,
  CORRELATION_ID,
  LOGGER_OPTIONS,
  getLogger,
  waitForLoggingRequest,
  waitForRequest
};
