var _excluded = ["button"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
/*
 * 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.
 */

import React, { useCallback, useLayoutEffect, useRef } from 'react';
import { EuiPopover } from './popover';
import { EuiPortal } from '../portal';
import { jsx as ___EmotionJSX } from "@emotion/react";
/**
 * 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.
 */

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