import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _typeof from "@babel/runtime/helpers/esm/typeof";
var _excluded = ["children"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import * as React from 'react';
import { useMergeRefs } from '@floating-ui/react';
import usePopoverContext from './usePopoverContext';

function isReactForwardRefType(object) {
  var _object$type;

  var REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref');
  if (_typeof(object) !== 'object' || object === null) return false;
  return ((_object$type = object.type) === null || _object$type === void 0 ? void 0 : _object$type.$$typeof) === REACT_FORWARD_REF_TYPE;
}

var PopoverTrigger = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var children = props.children,
      rest = _objectWithoutProperties(props, _excluded);

  var context = usePopoverContext();
  var childrenRef = children.ref;
  var triggerRef = useMergeRefs([context.refs.setReference, ref, childrenRef]);
  var className = 'sg-popover-trigger';
  var popoverId = context.getFloatingProps().id; // If children is valid element, i.e. <p>, <Checkbox> etc.
  // and if the element is forward ref type (otherwise we cannot pass ref).

  if ( /*#__PURE__*/React.isValidElement(children) && isReactForwardRefType(children)) {
    return /*#__PURE__*/React.cloneElement(children, _objectSpread({}, context.getReferenceProps(_objectSpread(_objectSpread(_objectSpread({
      className: className,
      tabIndex: 0
    }, rest), children.props), {}, {
      ref: triggerRef,
      // override forwarded ref with merged refs
      'data-state': context.isOpen ? 'open' : 'closed'
    }))));
  }

  return /*#__PURE__*/React.createElement("span", _extends({
    ref: triggerRef,
    tabIndex: 0 // ensure the element tabindex is set, but allow overriding with children props
    ,
    className: className // The user can style the trigger based on the state
    ,
    "data-state": context.isOpen ? 'open' : 'closed'
  }, context.getReferenceProps(props), {
    "aria-describedby": popoverId
  }), children);
});
export default PopoverTrigger;