(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "react-dom", "../helpers/getPosition/helpers", "./components/PopoverContent", "./styles"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var react_dom_1 = require("react-dom");
    var helpers_1 = require("../helpers/getPosition/helpers");
    var PopoverContent_1 = (0, tslib_1.__importDefault)(require("./components/PopoverContent"));
    var styles_1 = require("./styles");
    var Popover = function (_a) {
        var children = _a.children, _b = _a.direction, direction = _b === void 0 ? "top" : _b, title = _a.title, description = _a.description, primaryButtonContent = _a.primaryButtonContent, secondaryButtonContent = _a.secondaryButtonContent, _c = _a.isPrimaryButton, isPrimaryButton = _c === void 0 ? primaryButtonContent !== undefined : _c, _d = _a.isSecondaryButton, isSecondaryButton = _d === void 0 ? secondaryButtonContent !== undefined : _d, onPrimaryButton = _a.onPrimaryButton, onSecondaryButton = _a.onSecondaryButton, onVisibleChange = _a.onVisibleChange, _e = _a.defaultIsVisible, defaultIsVisible = _e === void 0 ? false : _e, className = _a.className, _f = _a.isShow, isShow = _f === void 0 ? true : _f;
        var _g = (0, tslib_1.__read)((0, react_1.useState)(false), 2), isVisible = _g[0], setIsVisible = _g[1];
        var _h = (0, tslib_1.__read)((0, react_1.useState)(null), 2), containerPopover = _h[0], setContainerPopover = _h[1];
        var anchorComponentRef = (0, react_1.useRef)(null);
        var anchorSizeAndPosition = (0, react_1.useMemo)(function () { return (0, helpers_1.calculateSizeAnPositionElement)(anchorComponentRef.current); }, [isVisible]);
        (0, react_1.useEffect)(function () {
            var container = document.getElementsByTagName("body").item(0);
            setContainerPopover(container);
            if (defaultIsVisible) {
                setIsVisible(defaultIsVisible);
            }
        }, []);
        (0, react_1.useEffect)(function () {
            onVisibleChange && onVisibleChange(isVisible || false);
        }, [isVisible]);
        var handleMouseClick = function () {
            setIsVisible(isShow);
        };
        var handleClose = function () {
            setIsVisible(false);
        };
        return (react_1.default.createElement(styles_1.PopoverStyled, { ref: anchorComponentRef, onMouseDown: handleMouseClick, onBlur: handleClose, className: className },
            children,
            containerPopover &&
                isVisible &&
                (0, react_dom_1.createPortal)(react_1.default.createElement(PopoverContent_1.default, { direction: direction, anchorSizeAndPosition: anchorSizeAndPosition, title: title, description: description, isPrimaryButton: isPrimaryButton, isSecondaryButton: isSecondaryButton, primaryButtonContent: primaryButtonContent, secondaryButtonContent: secondaryButtonContent, onPrimaryButton: onPrimaryButton, onSecondaryButton: onSecondaryButton, handleClose: handleClose }), containerPopover)));
    };
    Popover.displayName = "Popover";
    exports.default = Popover;
});
