import { configure } from "@testing-library/dom";
import chai from "chai";
import chaiDOM from "chai-dom";
chai.use(chaiDOM);
configure({
  getElementError: (message) => new Error(message ?? "Unknown error")
});
const { expect } = chai;
function isError(maybeError) {
  return maybeError && typeof maybeError === "object" && "message" in maybeError && typeof maybeError.message === "string";
}
async function testStep(title, get) {
  let caughtError;
  try {
    const result = await get();
    const titleString2 = typeof title === "function" ? title({ type: "pass" }) : title;
    if (window.parent !== window) {
      window.parent.postMessage(
        {
          type: "kcdshop:test-step-update",
          status: "pass",
          title: titleString2,
          timestamp: Date.now()
        },
        "*"
      );
    } else {
      console.log(`\u2705 ${titleString2}`);
    }
    return result;
  } catch (e) {
    caughtError = e;
  }
  const error = isError(caughtError) ? caughtError : new Error(typeof caughtError === "string" ? caughtError : "Unknown error");
  const titleString = typeof title === "function" ? title({ type: "fail", error }) : title;
  error.message = `${titleString}${error.message ? `

${error.message}` : ""}`;
  throw error;
}
export {
  expect,
  testStep
};
