"use strict";
const utils = require("../../../../_virtual/utils.cjs");
const interopRequireDefault = require("../../../@babel/runtime/helpers/interopRequireDefault.cjs");
const index = require("../../../redent/index.cjs");
const isEqual = require("../../../lodash/isEqual.cjs");
const index$1 = require("../../../@adobe/css-tools/dist/index.cjs");
var _interopRequireDefault = interopRequireDefault.interopRequireDefaultExports;
Object.defineProperty(utils.__exports, "__esModule", {
  value: true
});
utils.__exports.NodeTypeError = utils.__exports.HtmlElementTypeError = void 0;
utils.__exports.checkHtmlElement = checkHtmlElement;
utils.__exports.checkNode = checkNode;
utils.__exports.compareArraysAsSet = compareArraysAsSet;
utils.__exports.deprecate = deprecate;
utils.__exports.getMessage = getMessage;
utils.__exports.getSingleElementValue = getSingleElementValue;
utils.__exports.getTag = getTag;
utils.__exports.matches = matches;
utils.__exports.normalize = normalize;
utils.__exports.parseCSS = parseCSS;
utils.__exports.toSentence = toSentence;
var _redent = _interopRequireDefault(index.__require());
var _isEqual = _interopRequireDefault(isEqual.__require());
var _cssTools = index$1.__require();
class GenericTypeError extends Error {
  constructor(expectedString, received, matcherFn, context) {
    super();
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, matcherFn);
    }
    let withType = "";
    try {
      withType = context.utils.printWithType("Received", received, context.utils.printReceived);
    } catch (e) {
    }
    this.message = [
      context.utils.matcherHint(`${context.isNot ? ".not" : ""}.${matcherFn.name}`, "received", ""),
      "",
      // eslint-disable-next-line @babel/new-cap
      `${context.utils.RECEIVED_COLOR("received")} value must ${expectedString}.`,
      withType
    ].join("\n");
  }
}
class HtmlElementTypeError extends GenericTypeError {
  constructor(...args) {
    super("be an HTMLElement or an SVGElement", ...args);
  }
}
utils.__exports.HtmlElementTypeError = HtmlElementTypeError;
class NodeTypeError extends GenericTypeError {
  constructor(...args) {
    super("be a Node", ...args);
  }
}
utils.__exports.NodeTypeError = NodeTypeError;
function checkHasWindow(htmlElement, ErrorClass, ...args) {
  if (!htmlElement || !htmlElement.ownerDocument || !htmlElement.ownerDocument.defaultView) {
    throw new ErrorClass(htmlElement, ...args);
  }
}
function checkNode(node, ...args) {
  checkHasWindow(node, NodeTypeError, ...args);
  const window = node.ownerDocument.defaultView;
  if (!(node instanceof window.Node)) {
    throw new NodeTypeError(node, ...args);
  }
}
function checkHtmlElement(htmlElement, ...args) {
  checkHasWindow(htmlElement, HtmlElementTypeError, ...args);
  const window = htmlElement.ownerDocument.defaultView;
  if (!(htmlElement instanceof window.HTMLElement) && !(htmlElement instanceof window.SVGElement)) {
    throw new HtmlElementTypeError(htmlElement, ...args);
  }
}
class InvalidCSSError extends Error {
  constructor(received, matcherFn, context) {
    super();
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, matcherFn);
    }
    this.message = [
      received.message,
      "",
      // eslint-disable-next-line @babel/new-cap
      context.utils.RECEIVED_COLOR(`Failing css:`),
      // eslint-disable-next-line @babel/new-cap
      context.utils.RECEIVED_COLOR(`${received.css}`)
    ].join("\n");
  }
}
function parseCSS(css, ...args) {
  const ast = (0, _cssTools.parse)(`selector { ${css} }`, {
    silent: true
  }).stylesheet;
  if (ast.parsingErrors && ast.parsingErrors.length > 0) {
    const {
      reason,
      line
    } = ast.parsingErrors[0];
    throw new InvalidCSSError({
      css,
      message: `Syntax error parsing expected css: ${reason} on line: ${line}`
    }, ...args);
  }
  const parsedRules = ast.rules[0].declarations.filter((d) => d.type === "declaration").reduce((obj, {
    property,
    value
  }) => Object.assign(obj, {
    [property]: value
  }), {});
  return parsedRules;
}
function display(context, value) {
  return typeof value === "string" ? value : context.utils.stringify(value);
}
function getMessage(context, matcher, expectedLabel, expectedValue, receivedLabel, receivedValue) {
  return [
    `${matcher}
`,
    // eslint-disable-next-line @babel/new-cap
    `${expectedLabel}:
${context.utils.EXPECTED_COLOR((0, _redent.default)(display(context, expectedValue), 2))}`,
    // eslint-disable-next-line @babel/new-cap
    `${receivedLabel}:
${context.utils.RECEIVED_COLOR((0, _redent.default)(display(context, receivedValue), 2))}`
  ].join("\n");
}
function matches(textToMatch, matcher) {
  if (matcher instanceof RegExp) {
    return matcher.test(textToMatch);
  } else {
    return textToMatch.includes(String(matcher));
  }
}
function deprecate(name, replacementText) {
  console.warn(`Warning: ${name} has been deprecated and will be removed in future updates.`, replacementText);
}
function normalize(text) {
  return text.replace(/\s+/g, " ").trim();
}
function getTag(element) {
  return element.tagName && element.tagName.toLowerCase();
}
function getSelectValue({
  multiple,
  options
}) {
  const selectedOptions = [...options].filter((option) => option.selected);
  if (multiple) {
    return [...selectedOptions].map((opt) => opt.value);
  }
  if (selectedOptions.length === 0) {
    return void 0;
  }
  return selectedOptions[0].value;
}
function getInputValue(inputElement) {
  switch (inputElement.type) {
    case "number":
      return inputElement.value === "" ? null : Number(inputElement.value);
    case "checkbox":
      return inputElement.checked;
    default:
      return inputElement.value;
  }
}
function getSingleElementValue(element) {
  if (!element) {
    return void 0;
  }
  switch (element.tagName.toLowerCase()) {
    case "input":
      return getInputValue(element);
    case "select":
      return getSelectValue(element);
    default:
      return element.value;
  }
}
function compareArraysAsSet(a, b) {
  if (Array.isArray(a) && Array.isArray(b)) {
    return (0, _isEqual.default)(new Set(a), new Set(b));
  }
  return void 0;
}
function toSentence(array, {
  wordConnector = ", ",
  lastWordConnector = " and "
} = {}) {
  return [array.slice(0, -1).join(wordConnector), array[array.length - 1]].join(array.length > 1 ? lastWordConnector : "");
}
module.exports = utils.__exports;
//# sourceMappingURL=utils.cjs.map
