"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EuiWrappingPopover = EuiWrappingPopover;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _popover = require("./popover");
var _portal = require("../portal");
var _react2 = require("@emotion/react");
var _excluded = ["button"];
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */
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 && {}.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; }
/**
 * Injects the EuiPopover next to the button via EuiPortal
 * then the button element is moved into the popover dom.
 * On unmount, the button is moved back to its original location.
 */

function EuiWrappingPopover(props) {
  var button = props.button,
    rest = (0, _objectWithoutProperties2.default)(props, _excluded);
  var portalRef = (0, _react.useRef)(null);
  var setPortalRef = (0, _react.useCallback)(function (node) {
    portalRef.current = node;
  }, []);
  var setAnchorRef = (0, _react.useCallback)(function (node) {
    node === null || node === void 0 || node.insertAdjacentElement('beforebegin', button);
  }, [button]);
  (0, _react.useLayoutEffect)(function () {
    return function () {
      if (button.parentNode && portalRef.current) {
        portalRef.current.insertAdjacentElement('beforebegin', button);
      }
    };
  }, [button]);
  return (0, _react2.jsx)(_portal.EuiPortal, {
    portalRef: setPortalRef,
    insert: {
      sibling: button,
      position: 'after'
    }
  }, (0, _react2.jsx)(_popover.EuiPopover, (0, _extends2.default)({}, rest, {
    button: (0, _react2.jsx)("div", {
      ref: setAnchorRef,
      className: "euiWrappingPopover__anchor"
    })
  })));
}