"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "DropdownMenuExtensionItems", {
  enumerable: true,
  get: function get() {
    return _DropdownMenuExtensionItems.DropdownMenuExtensionItems;
  }
});
Object.defineProperty(exports, "DropdownMenuItem", {
  enumerable: true,
  get: function get() {
    return _DropdownMenuItem.DropdownMenuItem;
  }
});
Object.defineProperty(exports, "DropdownSeparator", {
  enumerable: true,
  get: function get() {
    return _DropdownSeparator.DropdownSeparator;
  }
});
exports.isSameItem = exports.compareArrays = exports.areSameItems = void 0;
Object.defineProperty(exports, "messages", {
  enumerable: true,
  get: function get() {
    return _messages.default;
  }
});
exports.shallowEqual = void 0;
var _DropdownMenuExtensionItems = require("./DropdownMenuExtensionItems");
var _DropdownMenuItem = require("./DropdownMenuItem");
var _messages = _interopRequireDefault(require("./messages"));
var _DropdownSeparator = require("./DropdownSeparator");
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function makeSameType(_a, _b) {
  return true;
}
var shallowEqual = exports.shallowEqual = function shallowEqual(objA, objB) {
  if (objA === objB) {
    return true;
  }
  if (objA == null || objB == null) {
    return false;
  }
  var keysA = Object.keys(objA);
  var keysB = Object.keys(objB);
  if (keysA.length !== keysB.length) {
    return false;
  }
  var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
  for (var idx = 0; idx < keysA.length; idx++) {
    var key = keysA[idx];
    if (!bHasOwnProperty(key)) {
      return false;
    }

    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    if (objA[key] !== objB[key]) {
      return false;
    }
  }
  return true;
};

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @atlaskit/volt-strict-mode/no-multiple-exports
var compareArrays = exports.compareArrays = function compareArrays(left, right) {
  var compareFn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : shallowEqual;
  if (left.length !== right.length) {
    return false;
  }
  for (var idx = 0; idx < left.length; idx++) {
    if (!compareFn(left[idx], right[idx])) {
      return false;
    }
  }
  return true;
};
var compareItemWithKeys = function compareItemWithKeys(leftItem, rightItem) {
  var excludedKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  return Object.keys(leftItem).filter(function (key) {
    return excludedKeys.indexOf(key) === -1;
  }).every(function (key) {
    return leftItem[key] instanceof Object ?
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    shallowEqual(leftItem[key], rightItem[key]) : leftItem[key] === rightItem[key];
  });
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var isSameItem = exports.isSameItem = function isSameItem(leftItem, rightItem) {
  if (leftItem.type !== rightItem.type) {
    return false;
  }
  switch (leftItem.type) {
    case 'button':
      // Need to typecast `rightItem as typeof leftItem` otherwise we will
      // have to put the `type !==` inside each case.
      return compareItemWithKeys(leftItem, rightItem, ['type', 'onClick', 'onMouseEnter', 'onMouseLeave']);
    case 'copy-button':
      return compareItemWithKeys(leftItem, rightItem, ['type', 'items']);
    case 'input':
      return compareItemWithKeys(leftItem, rightItem, ['type', 'onSubmit', 'onBlur']);
    case 'select':
      if (makeSameType(leftItem, rightItem) && Array.isArray(leftItem.options) && Array.isArray(rightItem.options) &&
      // Ignored via go/ees005
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      !compareArrays(leftItem.options, rightItem.options, function (left, right) {
        return compareItemWithKeys(left, right);
      })) {
        return false;
      }
      return compareItemWithKeys(leftItem, rightItem, ['type', 'onChange', 'options']);
    case 'overflow-dropdown':
    case 'dropdown':
      if (makeSameType(leftItem, rightItem) && Array.isArray(leftItem.options) && Array.isArray(rightItem.options) &&
      // @ts-expect-error TS2345: Argument of type 'DropdownOptionT<Function>[]' is not assignable to parameter of type 'any[][]'
      !compareArrays(leftItem.options, rightItem.options, function (left, right) {
        return (
          // @ts-expect-error  TS2322: Type '"onClick"' is not assignable to type 'keyof any[]'
          compareItemWithKeys(left, right, ['onClick'])
        );
      })) {
        return false;
      }
      return compareItemWithKeys(leftItem, rightItem, ['type', 'options']);
    case 'custom':
      return false;
    case 'separator':
      return compareItemWithKeys(leftItem, rightItem);
    case 'extensions-placeholder':
      return compareItemWithKeys(leftItem, rightItem);
  }
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var areSameItems = exports.areSameItems = function areSameItems(leftArr, rightArr) {
  if (leftArr === undefined && rightArr === undefined) {
    return true;
  }
  if (leftArr === undefined || rightArr === undefined) {
    return false;
  }
  if (leftArr.length !== rightArr.length) {
    return false;
  }
  return leftArr.every(function (item, index) {
    return isSameItem(rightArr[index], item);
  });
};

/* eslint-disable @atlaskit/editor/no-re-export */