import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _typeof from "@babel/runtime/helpers/typeof";
/* eslint-disable @typescript-eslint/no-namespace */
import { expect } from '@jest/globals';
import { diff } from 'jest-diff';
import matches from 'lodash/matches';
var isObject = function isObject(x) {
  return _typeof(x) === 'object' && x !== null;
};
var objectHas = function objectHas(x, prop) {
  return isObject(x) && prop in x;
};
expect.extend({
  toBeFiredWithAnalyticEventOnce: function toBeFiredWithAnalyticEventOnce(spy, event, channel) {
    var _this = this;
    /**
     * Find all events that match the comparison event
     */
    var matchingEvents = spy.mock.calls.filter(function (arg) {
      return matches(event)(arg[0]);
    });

    /**
     * We only pass if we can find a single matching event
     */
    if (matchingEvents.length === 1) {
      /**
       * The single matching event must be fired on the channel specified, else is not a pass
       */
      if (channel && matchingEvents[0][1] !== channel) {
        return {
          message: function message() {
            return _this.utils.matcherHint('toBeFiredWithAnalyticEventOnce') + '\n\n' + "Expected to fire on: ".concat(_this.utils.printExpected(channel), ",\n") + "Actually fired on: ".concat(_this.utils.printReceived(matchingEvents[0][1]));
          },
          pass: false
        };
      }
      return {
        message: function message() {
          return "analytic event was fired once";
        },
        pass: true
      };
    }
    return {
      message: function message() {
        if (spy.mock.calls.length === 0) {
          return "no events were fired!";
        }
        var candidates = spy.mock.calls.filter(function (parameters) {
          var e = parameters === null || parameters === void 0 ? void 0 : parameters[0];
          if (objectHas(e, 'payload') && objectHas(e.payload, 'action') && objectHas(e.payload, 'actionSubject') && objectHas(event, 'payload') && objectHas(event.payload, 'action') && objectHas(event.payload, 'actionSubject')) {
            return e.payload.action === event.payload.action && e.payload.actionSubject === event.payload.actionSubject;
          }
          return false;
        });
        if (candidates.length === 0) {
          return _this.utils.matcherHint('toBeFiredWithAnalyticEventOnce', undefined, undefined) + '\n\n' + "Event Expected: \n\n".concat(JSON.stringify(event), "\n\n") + "Events Received: \n\n".concat(spy.mock.calls.map(function (_ref, i) {
            var _ref2 = _slicedToArray(_ref, 1),
              x = _ref2[0];
            if (x.payload.eventType === 'screen') {
              return "".concat(i, ": ").concat(x.payload.name, " ").concat(x.payload.action);
            }
            return "".concat(i, ": ").concat(x.payload.actionSubject, " ").concat(x.payload.action);
          }).join('\n'));
        }
        if (candidates.length === 1) {
          var difference = diff(event, candidates[0][0]);
          return _this.utils.matcherHint('toBeFiredWithAnalyticEventOnce', undefined, undefined) + '\n\n' + "Expected: ".concat(JSON.stringify(event), "\n\n") + "Received: ".concat(spy.mock.calls.map(function (_ref3) {
            var _ref4 = _slicedToArray(_ref3, 1),
              x = _ref4[0];
            return "".concat(JSON.stringify(x));
          }).join('\n\n')) + "".concat(difference);
        }
        return _this.utils.matcherHint('toBeFiredWithAnalyticEventOnce', undefined, undefined) + '\n\n' + "Expected: ".concat(JSON.stringify(event), "\n") + "Received: ".concat(candidates.map(function (_ref5) {
          var _ref6 = _slicedToArray(_ref5, 1),
            x = _ref6[0];
          return "".concat(JSON.stringify(x));
        }).join('\n\n'));
      },
      pass: false
    };
  }
});