"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PopListSelector = exports.PopOverlaySelector = exports.Pop = void 0;
const react_1 = __importStar(require("react"));
const HandleEsc_1 = require("../../utils/HandleEsc");
const OnButtonClick_1 = require("./OnButtonClick");
const Pop_styled_1 = require("./Pop.styled");
const PopOption_1 = require("./PopOption");
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 react_1.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 = (0, react_1.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_1.default.createElement(HandleEsc_1.HandleEsc, { onPress: this.handleEsc });
        return (react_1.default.createElement(Pop_styled_1.Overlay, { className: this.props.className, ref: this.rootRef, onClick: this.handleOverlayClick },
            esc,
            react_1.default.createElement(OnButtonClick_1.OnButtonClick, { onClick: this.handleEsc },
                react_1.default.createElement(Pop_styled_1.List, { css: style }, this.props.children))));
    }
}
exports.Pop = Pop;
Pop.Option = PopOption_1.PopOption;
Pop.toString = () => Pop_styled_1.Overlay.toString();
const PopOverlaySelector = Pop_styled_1.Overlay.toString();
exports.PopOverlaySelector = PopOverlaySelector;
const PopListSelector = Pop_styled_1.List.toString();
exports.PopListSelector = PopListSelector;
//# sourceMappingURL=Pop.js.map