'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-859ae5a6.js');
const utils = require('./utils-2a7c8f6e.js');

const Popover = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.scrollListener = () => this.onScroll(window);
        /**
         * Render the arrow
         */
        this.arrow = true;
        /**
         * The strategy for dismissing the popover
         * Options are: `"explicit"`and `"auto"`. Defaults to "explicit".
         */
        this.dismiss = 'explicit';
        /**
         * The preferred placement
         * Options are: `"botton"`, `"left"`, `"right"`, and "top" which is the default
         */
        this.placement = 'top';
    }
    componentWillLoad() {
        this.onScroll = utils.debounce(this.onScroll.bind(this), 50);
    }
    async disconnectedCallback() {
        if (this.dismiss === 'auto') {
            this.target = undefined;
            window.removeEventListener('scroll', this.scrollListener);
        }
        if (this.overlay) {
            const popper = await this.overlay.popper();
            if (popper) {
                this.overlay.remove();
            }
        }
    }
    /**
     * Display the popover
     */
    async display(target) {
        const popper = await this.overlay.popper();
        if (popper) {
            popper.reference = target;
            popper.scheduleUpdate();
        }
        else {
            if (this.dismiss === 'auto') {
                this.target = target;
                window.addEventListener('scroll', this.scrollListener);
            }
            return this.overlay.open(target);
        }
    }
    /**
     * Hide the popover
     */
    async hide() {
        this.disconnectedCallback();
        await this.overlay.close();
    }
    /**
     * Alternate hiding and showing
     */
    async toggle(target) {
        const popper = await this.overlay.popper();
        if (popper) {
            return this.hide();
        }
        else {
            return this.display(target);
        }
    }
    async showing() {
        const popper = await this.overlay.popper();
        return !!popper;
    }
    async onScroll(win) {
        if (win.scrollY > (this.target.offsetTop + this.target.offsetHeight) || win.scrollY < (this.target.offsetTop - this.target.offsetHeight)) {
            return this.hide();
        }
    }
    render() {
        return (core.h("anj-overlay", { class: "anj-popover", dismiss: this.dismiss, scrollable: true, placement: this.placement, offset: this.arrow ? '0, 15' : '0, 5', ref: (ref) => this.overlay = ref }, this.arrow ? core.h("anj-arrow", null) : '', core.h("div", { class: "anj-popover-content" }, core.h("slot", null))));
    }
    get element() { return core.getElement(this); }
    static get style() { return ".sc-anj-popover-h {\n  display: none;\n}"; }
};

exports.anj_popover = Popover;
