"use strict";
const toHaveErrormessage = require("../../../../_virtual/to-have-errormessage.cjs");
require("./utils.cjs");
const utils = require("../../../../_virtual/utils.cjs");
Object.defineProperty(toHaveErrormessage.__exports, "__esModule", {
  value: true
});
toHaveErrormessage.__exports.toHaveErrorMessage = toHaveErrorMessage;
var _utils = utils.__exports;
function toHaveErrorMessage(htmlElement, checkWith) {
  (0, _utils.checkHtmlElement)(htmlElement, toHaveErrorMessage, this);
  if (!htmlElement.hasAttribute("aria-invalid") || htmlElement.getAttribute("aria-invalid") === "false") {
    const not = this.isNot ? ".not" : "";
    return {
      pass: false,
      message: () => {
        return (0, _utils.getMessage)(this, this.utils.matcherHint(`${not}.toHaveErrorMessage`, "element", ""), `Expected the element to have invalid state indicated by`, 'aria-invalid="true"', "Received", htmlElement.hasAttribute("aria-invalid") ? `aria-invalid="${htmlElement.getAttribute("aria-invalid")}"` : this.utils.printReceived(""));
      }
    };
  }
  const expectsErrorMessage = checkWith !== void 0;
  const errormessageIDRaw = htmlElement.getAttribute("aria-errormessage") || "";
  const errormessageIDs = errormessageIDRaw.split(/\s+/).filter(Boolean);
  let errormessage = "";
  if (errormessageIDs.length > 0) {
    const document = htmlElement.ownerDocument;
    const errormessageEls = errormessageIDs.map((errormessageID) => document.getElementById(errormessageID)).filter(Boolean);
    errormessage = (0, _utils.normalize)(errormessageEls.map((el) => el.textContent).join(" "));
  }
  return {
    pass: expectsErrorMessage ? checkWith instanceof RegExp ? checkWith.test(errormessage) : this.equals(errormessage, checkWith) : Boolean(errormessage),
    message: () => {
      const to = this.isNot ? "not to" : "to";
      return (0, _utils.getMessage)(this, this.utils.matcherHint(`${this.isNot ? ".not" : ""}.toHaveErrorMessage`, "element", ""), `Expected the element ${to} have error message`, this.utils.printExpected(checkWith), "Received", this.utils.printReceived(errormessage));
    }
  };
}
module.exports = toHaveErrormessage.__exports;
//# sourceMappingURL=to-have-errormessage.cjs.map
