import { __assign, __spreadArray } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { cloneElement, useContext } from 'react';
import { isElement, isFragment } from 'react-is';
import Context from '../Context';
import Anchor from '../Anchor';
export function PopoverFocusTrigger(_a) {
    var children = _a.children, getElement = _a.getElement;
    var ctx = useContext(Context);
    if (!ctx) {
        throw new Error('PopoverFocusTrigger must be child of Popover');
    }
    var childProps = {
        onFocus: function () {
            var _a, _b;
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            if (isElement(children)) {
                (_b = (_a = children.props).onFocus) === null || _b === void 0 ? void 0 : _b.call.apply(_b, __spreadArray([_a], args));
            }
            ctx.popover.setVisible(true);
        },
        onBlur: function () {
            var _a, _b;
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            if (isElement(children)) {
                (_b = (_a = children.props).onBlur) === null || _b === void 0 ? void 0 : _b.call.apply(_b, __spreadArray([_a], args));
            }
            ctx.popover.setVisible(false);
        },
    };
    var child;
    if (typeof children === 'function') {
        child = children(childProps);
    }
    else if (isElement(children) && !isFragment(children)) {
        child = cloneElement(children, childProps);
    }
    else {
        child = _jsx("span", __assign({}, childProps, { "data-zv": 'v10.0.11' }, { children: children }), void 0);
    }
    return _jsx(Anchor, __assign({ getElement: getElement }, { children: child }), void 0);
}
export default PopoverFocusTrigger;
