"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = A11y;

var React = _interopRequireWildcard(require("react"));

var _axeCore = _interopRequireDefault(require("axe-core"));

var _layer = require("../layer");

var _typography = require("../typography");

var _styles = require("../styles");

var _themeProvider = require("../styles/theme-provider");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function validateNode(node) {
  return new Promise(function (resolve, reject) {
    _axeCore.default.run(node, {
      reporter: 'v2'
    }, function (error, results) {
      if (error) reject(error);
      resolve(results.violations);
    });
  });
}

function segmentViolationsByNode(violations) {
  var nodes = violations.reduce(function (map, violation) {
    violation.nodes.forEach(function (node) {
      // @ts-expect-error todo(flow-ts) node.target is an Array
      if (!map[node.target]) {
        // @ts-expect-error todo(flow-ts) node.target is an Array
        map[node.target] = [violation];
      } else {
        // todo(flow->ts) node.target is an Array
        // @ts-expect-error todo(flow-ts) result of Array.push is not Array
        map[node.target] = map[node.target].push(violation);
      }
    });
    return map;
  }, {});
  return Object.entries(nodes);
}

var ViolationContainer = (0, _styles.styled)('div', function (_ref) {
  var $theme = _ref.$theme,
      $top = _ref.$top,
      $left = _ref.$left;
  return {
    backgroundColor: $theme.colors.backgroundPrimary,
    boxShadow: $theme.lighting.shadow600,
    position: 'absolute',
    padding: $theme.sizing.scale400,
    top: $top,
    left: $left
  };
});
ViolationContainer.displayName = "ViolationContainer";

function Violation(props) {
  var _React$useState = React.useState({
    top: 0,
    left: 0
  }),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      offset = _React$useState2[0],
      setOffset = _React$useState2[1];

  var _React$useState3 = React.useState(null),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      anchor = _React$useState4[0],
      setAnchor = _React$useState4[1];

  var _React$useState5 = React.useState(null),
      _React$useState6 = _slicedToArray(_React$useState5, 2),
      popper = _React$useState6[0],
      setPopper = _React$useState6[1];

  var _React$useState7 = React.useState(false),
      _React$useState8 = _slicedToArray(_React$useState7, 2),
      isHovered = _React$useState8[0],
      setIsHovered = _React$useState8[1];

  var theme = React.useContext(_themeProvider.ThemeContext);

  var handleMouseEnter = function handleMouseEnter() {
    return setIsHovered(true);
  };

  var handleMouseLeave = function handleMouseLeave() {
    return setIsHovered(false);
  };

  React.useEffect(function () {
    var node = document.querySelector(props.target);

    if (node) {
      // @ts-ignore
      setAnchor(node);
      node.setAttribute('style', "border: solid 1px ".concat(theme.colors.borderNegative, ";"));
      node.addEventListener('mouseenter', handleMouseEnter);
      node.addEventListener('mouseleave', handleMouseLeave);
    }

    return function () {
      if (node) {
        node.removeEventListener('mouseenter', handleMouseEnter);
        node.removeEventListener('mouseleave', handleMouseLeave);
      }
    };
  }, [props.target]);
  if (!isHovered) return null;
  return /*#__PURE__*/React.createElement(_layer.Layer, null, /*#__PURE__*/React.createElement(_layer.TetherBehavior, {
    anchorRef: anchor,
    popperRef: popper,
    onPopperUpdate: function onPopperUpdate(update) {
      return setOffset(update.popper);
    },
    placement: _layer.TETHER_PLACEMENT.bottom
  }, /*#__PURE__*/React.createElement(ViolationContainer // @ts-ignore
  , {
    ref: setPopper,
    $top: "".concat(offset.top, "px") || '0px',
    $left: "".concat(offset.left, "px") || '0px'
  }, /*#__PURE__*/React.createElement(_typography.ParagraphXSmall, null, props.target), props.violations.map(function (violation, index) {
    return /*#__PURE__*/React.createElement(_typography.ParagraphSmall, {
      key: index
    }, violation.description);
  }))));
}

function A11y(props) {
  var _React$useState9 = React.useState([]),
      _React$useState10 = _slicedToArray(_React$useState9, 2),
      violations = _React$useState10[0],
      setViolations = _React$useState10[1];

  var _React$useState11 = React.useState(null),
      _React$useState12 = _slicedToArray(_React$useState11, 2),
      idleID = _React$useState12[0],
      setIdleID = _React$useState12[1];

  var child = React.useRef(null);
  React.useEffect(function () {
    if (child.current) {
      if (idleID) {
        cancelIdleCallback(idleID);
        setIdleID(null);
      }

      var id = requestIdleCallback(function () {
        // @ts-ignore
        validateNode(child.current).then(setViolations);
      }); // @ts-ignore

      setIdleID(id);
    }
  }, [props.children]);
  var violationsByNode = segmentViolationsByNode(violations);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
    ref: child
  }, props.children), /*#__PURE__*/React.createElement("div", null, violationsByNode.map(function (_ref2, index) {
    var _ref3 = _slicedToArray(_ref2, 2),
        node = _ref3[0],
        violations = _ref3[1];

    return /*#__PURE__*/React.createElement(Violation, {
      target: node,
      violations: violations,
      key: index
    });
  })));
}