import React, { Component, createRef } from "react";
import { HandleEsc } from "../../utils/HandleEsc.js";
import { OnButtonClick } from "./OnButtonClick.js";
import { List, Overlay } from "./Pop.styled.js";
import { PopOption } from "./PopOption.js";
const html = typeof document === "object" ? document.getElementsByTagName("html")[0] : null;
const getElementDimensions = (elem) => {
    return {
        width: elem.clientWidth,
        height: elem.clientHeight,
    };
};
const getElementLocation = (element, outerElement = html) => {
    const outer = getElementDimensions(outerElement);
    const rect = element.getBoundingClientRect();
    const distanceToBottom = outer.height - rect.bottom;
    const distanceToTop = rect.top;
    const verticalLocation = distanceToTop > distanceToBottom ? "bottom" : "top";
    const distanceToLeft = rect.left;
    const distanceToRight = outer.width - rect.right;
    const horizontalLocation = distanceToLeft > distanceToRight ? "right" : "left";
    return [verticalLocation, horizontalLocation];
};
class Pop extends Component {
    constructor(props) {
        super(props);
        this.onOpen = () => {
            const anchorElement = this.getAnchorElement();
            if (!anchorElement) {
                console.warn("PopOptions can not find anchor element");
                return;
            }
            if (!(anchorElement instanceof HTMLElement)) {
                console.warn("PopOptions anchor element not a HTMLElement");
                return;
            }
            const [vertical, horizontal] = getElementLocation(anchorElement);
            const dimensions = getElementDimensions(html);
            const rect = anchorElement.getBoundingClientRect();
            const x = horizontal === "left" ? rect.left : dimensions.width - rect.right;
            const y = vertical === "top" ? rect.bottom : dimensions.height - rect.top;
            this.setState({
                vertical,
                horizontal,
                x,
                y,
            });
        };
        this.handleOverlayClick = (e) => {
            if (e.target === e.currentTarget) {
                this.props.onClose();
            }
        };
        this.handleEsc = () => {
            this.props.onClose();
        };
        this.state = {
            x: 0,
            y: 0,
            vertical: "top",
            horizontal: "left",
        };
        this.rootRef = createRef();
    }
    componentDidUpdate(prevProps) {
        if (!prevProps.open && this.props.open) {
            this.onOpen();
        }
    }
    getAnchorElement() {
        if (this.props.anchor instanceof HTMLElement) {
            return this.props.anchor;
        }
        if (!this.rootRef.current) {
            return null;
        }
        if (this.props.anchor === "prev") {
            return this.rootRef.current.previousElementSibling;
        }
        if (this.props.anchor === "next") {
            return this.rootRef.current.nextElementSibling;
        }
        return null;
    }
    render() {
        var _a;
        const style = {};
        if (this.state.vertical === "top") {
            style.top = this.state.y;
        }
        else {
            style.bottom = this.state.y;
        }
        if (this.state.horizontal === "left") {
            style.left = this.state.x;
        }
        else {
            style.right = this.state.x;
        }
        if (!this.props.open) {
            return null;
        }
        const closeOnEsc = (_a = this.props.closeOnEsc) !== null && _a !== void 0 ? _a : true;
        const esc = closeOnEsc && React.createElement(HandleEsc, { onPress: this.handleEsc });
        return (React.createElement(Overlay, { className: this.props.className, ref: this.rootRef, onClick: this.handleOverlayClick },
            esc,
            React.createElement(OnButtonClick, { onClick: this.handleEsc },
                React.createElement(List, { css: style }, this.props.children))));
    }
}
Pop.Option = PopOption;
Pop.toString = () => Overlay.toString();
const PopOverlaySelector = Overlay.toString();
const PopListSelector = List.toString();
export { Pop, PopOverlaySelector, PopListSelector };
//# sourceMappingURL=Pop.js.map