import { globalObject } from '@skbkontur/global-object';
import { isMac } from "../../../client";
import { isInstanceOf } from "../../../isInstanceOf";
import { extractCode } from "../extractCode";
import { KeyboardEventCodes as Codes } from "../KeyboardEventCodes";

// IE 9+ supports char attribute
// https://developer.mozilla.org/ru/docs/Web/API/KeyboardEvent
var getChar = function getChar(e) {
  var _e$nativeEvent;
  return isInstanceOf(e, globalObject.KeyboardEvent) ? e["char"] : (_e$nativeEvent = e.nativeEvent) == null ? void 0 : _e$nativeEvent["char"];
};
export var isShortcutCopy = function isShortcutCopy(e) {
  return (isMac ? e.metaKey : e.ctrlKey) && extractCode(e) === Codes.KeyC || !isMac && e.ctrlKey && isKeyInsert(e) || e.key === 'Copy';
};
export var isShortcutPaste = function isShortcutPaste(e) {
  return (isMac ? e.metaKey : e.ctrlKey) && extractCode(e) === Codes.KeyV || !isMac && e.shiftKey && isKeyInsert(e) || e.key === 'Paste';
};
export var isShortcutCut = function isShortcutCut(e) {
  return (isMac ? e.metaKey : e.ctrlKey) && extractCode(e) === Codes.KeyX || !isMac && e.shiftKey && isKeyDelete(e) || e.key === 'Cut';
};
export var isShortcutSelectAll = function isShortcutSelectAll(e) {
  return (isMac ? e.metaKey : e.ctrlKey) && extractCode(e) === Codes.KeyA;
};
export var isShortcutJumpCaret = function isShortcutJumpCaret(e) {
  return (isMac ? e.altKey : e.ctrlKey) && isKeyArrowHorizontal(e);
};
export var isKeyEscape = function isKeyEscape(e) {
  return e.key === 'Escape' || e.key === 'Esc';
};
export var isKeyHome = function isKeyHome(e) {
  return e.key === 'Home';
};
export var isKeyEnd = function isKeyEnd(e) {
  return e.key === 'End';
};
export var isKeyMeta = function isKeyMeta(e) {
  return e.key === 'Meta' || e.key === 'Win';
};
export var isKeyControl = function isKeyControl(e) {
  return e.key === 'Control';
};
export var isKeyShift = function isKeyShift(e) {
  return e.key === 'Shift';
};
export var isKeyAlt = function isKeyAlt(e) {
  return e.key === 'Alt';
};
export var isKeyDelete = function isKeyDelete(e) {
  return e.key === 'Delete' || e.key === 'Del';
};
export var isKeyBackspace = function isKeyBackspace(e) {
  return e.key === 'Backspace';
};
export var isKeyCapsLock = function isKeyCapsLock(e) {
  return e.key === 'CapsLock';
};
export var isKeyInsert = function isKeyInsert(e) {
  return e.key === 'Insert';
};
export var isKeyTab = function isKeyTab(e) {
  return e.key === 'Tab';
};
export var isKeyEnter = function isKeyEnter(e) {
  return e.key === 'Enter';
};
export var isKeyArrowUp = function isKeyArrowUp(e) {
  return e.key === 'ArrowUp' || e.key === 'Up';
};
export var isKeyArrowRight = function isKeyArrowRight(e) {
  return e.key === 'ArrowRight' || e.key === 'Right';
};
export var isKeyArrowDown = function isKeyArrowDown(e) {
  return e.key === 'ArrowDown' || e.key === 'Down';
};
export var isKeyArrowLeft = function isKeyArrowLeft(e) {
  return e.key === 'ArrowLeft' || e.key === 'Left';
};
export var isKeySpace = function isKeySpace(e) {
  return e.key === ' ' || e.key === 'Spacebar';
};
export var isKeyComma = function isKeyComma(e) {
  return e.key === ',' || getChar(e) === ',';
};
export var isCodeMinus = function isCodeMinus(e) {
  return extractCode(e) === Codes.Minus;
};
export var isCodeNumpadDecimal = function isCodeNumpadDecimal(e) {
  return extractCode(e) === Codes.NumpadDecimal;
};
export var isCodeNumpadDivide = function isCodeNumpadDivide(e) {
  return extractCode(e) === Codes.NumpadDivide;
};
export var isCodeNumpadSubtract = function isCodeNumpadSubtract(e) {
  return extractCode(e) === Codes.NumpadSubtract;
};
export var isCodeSlash = function isCodeSlash(e) {
  return extractCode(e) === Codes.Slash;
};
export var isCodeBackslash = function isCodeBackslash(e) {
  return extractCode(e) === Codes.Backslash;
};
export var isCodeIntlBackslash = function isCodeIntlBackslash(e) {
  return extractCode(e) === Codes.IntlBackslash;
};
export var isCodeComma = function isCodeComma(e) {
  return extractCode(e) === Codes.Comma;
};
export var isCodePeriod = function isCodePeriod(e) {
  return extractCode(e) === Codes.Period;
};
export var isKeyArrowHorizontal = function isKeyArrowHorizontal(e) {
  return isKeyArrowRight(e) || isKeyArrowLeft(e);
};
export var isKeyArrowVertical = function isKeyArrowVertical(e) {
  return isKeyArrowUp(e) || isKeyArrowDown(e);
};
export var isKeyArrow = function isKeyArrow(e) {
  return isKeyArrowHorizontal(e) || isKeyArrowVertical(e);
};
export var isKeyNumber = function isKeyNumber(e) {
  return /^\d+$/.test(e.key);
};
export var isKeyChar = function isKeyChar(e) {
  return e.key.trim().length === 1;
};
export var isKeyFs = function isKeyFs(e) {
  return /^(?:F[1-9]|F1[0-2]|Soft[1-4])$/.test(e.key);
};
export var isModified = function isModified(is) {
  return function (e) {
    return (e.shiftKey || e.metaKey || e.ctrlKey || e.altKey) && (is ? is(e) : true);
  };
};
export var isUnmodified = function isUnmodified(is) {
  return function (e) {
    return !isModified()(e) && (is ? is(e) : true);
  };
};
export var isModShift = function isModShift(is) {
  return function (e) {
    return e.shiftKey && (is ? is(e) : true);
  };
};
export var isModCtrl = function isModCtrl(is) {
  return function (e) {
    return e.ctrlKey && (is ? is(e) : true);
  };
};
export var isModAlt = function isModAlt(is) {
  return function (e) {
    return e.altKey && (is ? is(e) : true);
  };
};
export var someKeys = function someKeys() {
  for (var _len = arguments.length, iss = new Array(_len), _key = 0; _key < _len; _key++) {
    iss[_key] = arguments[_key];
  }
  return function (e) {
    return iss.some(function (is) {
      return is(e);
    });
  };
};