"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Window = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactFocusLock = _interopRequireDefault(require("react-focus-lock"));
var _reactRemoveScroll = require("react-remove-scroll");
var _classnames = _interopRequireDefault(require("classnames"));
var _Style = require("./Style");
var _helpers = require("../../helpers");
var _utils = require("../../utils");
var _useForkRef = _interopRequireDefault(require("../../utils/useForkRef"));
var _excluded = ["children", "onClose", "className", "style", "onKeyDown", "disableFocusLock", "disableScrollLock", "disableCloseOnEsc", "onClick"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var returnFocus = {
  preventScroll: true
};
var Window = exports.Window = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var children = _ref.children,
    onClose = _ref.onClose,
    className = _ref.className,
    style = _ref.style,
    onKeyDown = _ref.onKeyDown,
    _ref$disableFocusLock = _ref.disableFocusLock,
    disableFocusLock = _ref$disableFocusLock === void 0 ? false : _ref$disableFocusLock,
    _ref$disableScrollLoc = _ref.disableScrollLock,
    disableScrollLock = _ref$disableScrollLoc === void 0 ? false : _ref$disableScrollLoc,
    _ref$disableCloseOnEs = _ref.disableCloseOnEsc,
    disableCloseOnEsc = _ref$disableCloseOnEs === void 0 ? false : _ref$disableCloseOnEs,
    onClick = _ref.onClick,
    rest = _objectWithoutProperties(_ref, _excluded);
  var innerRef = (0, _react.useRef)(null);
  var handleRef = (0, _useForkRef.default)(innerRef, ref);
  var _useLayerContext = (0, _helpers.useLayerContext)(),
    _useLayerContext$zInd = _useLayerContext.zIndex,
    zIndex = _useLayerContext$zInd === void 0 ? _utils.layers.skyscraper : _useLayerContext$zInd;
  var handleKeyDown = (0, _react.useCallback)(function (event) {
    if (!event.defaultPrevented && (event.key === "Esc" || event.key === "Escape") && !disableCloseOnEsc) {
      event.preventDefault();
      if (onClose) {
        onClose();
      }
    }
    if (typeof onKeyDown === "function") {
      onKeyDown(event);
    }
  }, [disableCloseOnEsc, onClose, onKeyDown]);
  var handleClick = (0, _react.useCallback)(function (event) {
    event === null || event === void 0 || event.stopPropagation();
    typeof onClick === "function" && onClick(event);
  }, [onClick]);
  return /*#__PURE__*/_react.default.createElement(_helpers.Portal, null, /*#__PURE__*/_react.default.createElement(_reactFocusLock.default, {
    returnFocus: returnFocus,
    disabled: disableFocusLock
  }, /*#__PURE__*/_react.default.createElement(_reactRemoveScroll.RemoveScroll, {
    forwardProps: true,
    ref: handleRef,
    enabled: !disableScrollLock,
    noIsolation: true
  }, /*#__PURE__*/_react.default.createElement(_Style.StyledWindow, _extends({}, rest, {
    style: _objectSpread({
      zIndex
    }, style),
    onKeyDown: handleKeyDown,
    tabIndex: 0,
    className: (0, _classnames.default)("c-window", className),
    onClick: handleClick
  }), children))));
});
Window.displayName = "Window";
//# sourceMappingURL=Window.js.map