import { __decorate, __metadata } from "tslib";
import { Host, h, } from "@stencil/core";
import { BEM } from "../../../utils/bem";
import { isDescendant } from "../../../utils/helpers";
import { Logger } from "../../../utils/log";
import { ListenToResize } from "../../../utils/resize";
import { BalScrollHandler } from "../../../utils/scroll";
export class NavMenuFlyout {
    constructor() {
        this.bodyScrollHandler = new BalScrollHandler();
        this.isHidden = false;
        /**
         * PUBLIC PROPERTY API
         * ------------------------------------------------------
         */
        /**
         * This is used to connect the flyout to the aria controls
         * */
        this.navId = `bal-nav-x${NavMenuFlyOutIds++}`;
    }
    createLogger(log) {
        this.log = log;
    }
    /**
     * LIFECYCLE
     * ------------------------------------------------------
     */
    connectedCallback() {
        this.bodyScrollHandler.connect();
    }
    disconnectedCallback() {
        this.bodyScrollHandler.disconnect();
    }
    /**
     * LISTENERS
     * ------------------------------------------------------
     */
    onFocusLeave(event) {
        if (this.el && event.relatedTarget && !isDescendant(this.el, event.relatedTarget)) {
            this.balFocusOut.emit(event);
        }
    }
    resizeListener() {
        if (this.isFlyoutScrollable()) {
            this.bodyScrollHandler.disable();
        }
        else {
            this.bodyScrollHandler.enable();
        }
    }
    /**
     * PRIVATE METHODS
     * ------------------------------------------------------
     */
    isFlyoutScrollable() {
        var _a, _b;
        return ((_a = this.el) === null || _a === void 0 ? void 0 : _a.scrollHeight) > ((_b = this.el) === null || _b === void 0 ? void 0 : _b.clientHeight);
    }
    /**
     * RENDER
     * ------------------------------------------------------
     */
    render() {
        const block = BEM.block('nav-menu-flyout');
        const line = block.element('line');
        return (h(Host, { key: '0ade6170ce034d4820781f4c990d3635bb0dfe79', id: `${this.navId}-menu-flyout`, class: Object.assign({}, block.class()) }, h("div", { key: 'a76e5c0918a3b972fee334a7d9262fae315b85c9', class: Object.assign({}, line.class()) }), h("div", { key: 'd71930bf624ee077c46cf7139f012c7116dc1dc7', class: Object.assign({}, block.element('container').class()) }, h("slot", { key: 'ff98f9f52e602ec787230b9a91a64920531e1152' }))));
    }
    static get is() { return "bal-nav-menu-flyout"; }
    static get originalStyleUrls() {
        return {
            "$": ["bal-nav-menu-flyout.sass"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bal-nav-menu-flyout.css"]
        };
    }
    static get properties() {
        return {
            "navId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "This is used to connect the flyout to the aria controls"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "nav-id",
                "defaultValue": "`bal-nav-x${NavMenuFlyOutIds++}`"
            }
        };
    }
    static get states() {
        return {
            "isHidden": {}
        };
    }
    static get events() {
        return [{
                "method": "balFocusOut",
                "name": "balFocusOut",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the flyout loses focus"
                },
                "complexType": {
                    "original": "BalEvents.BalNavFlyoutFocusOutDetail",
                    "resolved": "FocusEvent",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "focusout",
                "method": "onFocusLeave",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
__decorate([
    Logger('bal-nav-menu-flyout'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Function]),
    __metadata("design:returntype", void 0)
], NavMenuFlyout.prototype, "createLogger", null);
__decorate([
    ListenToResize(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], NavMenuFlyout.prototype, "resizeListener", null);
let NavMenuFlyOutIds = 0;
