import { Host, h } from "@stencil/core";
export class PdsPopover {
    constructor() {
        this.active = false;
        this.popoverTargetAction = 'show';
        this.popoverType = 'auto';
        this.componentId = undefined;
        this.text = undefined;
        this.maxWidth = 352;
        this.placement = 'right';
    }
    componentWillRender() {
        this.handlePopoverPositioning();
    }
    handleClick() {
        this.active = !this.active;
    }
    handleScroll() {
        if (this.active) {
            this.handlePopoverPositioning();
        }
    }
    handlePopoverPositioning() {
        const triggerEl = this.el.shadowRoot.querySelector('.pds-popover__trigger');
        const popoverEl = this.el.shadowRoot.querySelector('div[popover]');
        if (!triggerEl || !popoverEl)
            return;
        const triggerRect = triggerEl.getBoundingClientRect();
        const popoverRect = popoverEl.getBoundingClientRect();
        let top = 0;
        let left = 0;
        const offset = 8;
        switch (this.placement) {
            case 'top':
                top = triggerRect.top - popoverRect.height - offset;
                left = triggerRect.left;
                break;
            case 'right':
                top = triggerRect.top;
                left = triggerRect.right + offset;
                break;
            case 'bottom':
                top = triggerRect.bottom + offset;
                left = triggerRect.left;
                break;
            case 'left':
                top = triggerRect.top;
                left = triggerRect.left - popoverRect.width - offset;
                break;
        }
        popoverEl.style.top = `${top}px`;
        popoverEl.style.left = `${left}px`;
    }
    render() {
        return (h(Host, { key: '7a21719bb6f13f435cae77f6b2c7c2eefb36c977' }, h("button", { key: '707c94e2462fe230a777d61899b902c84feaa09c', class: "pds-popover__trigger", popoverTarget: this.componentId, popoverTargetAction: this.popoverTargetAction, onClick: this.handleClick }, this.text), h("div", { key: '703d21dd5f3caadb8c0d097849f691d2d4407358', class: `pds-popover ${this.active ? 'pds-popover--active' : ''}`, id: this.componentId, popover: this.popoverType, style: { maxWidth: `${this.maxWidth}px` } }, h("slot", { key: '42ef680b5850d62f50f1e6cfb1587c3d5b6db0d4' }))));
    }
    static get is() { return "pds-popover"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["pds-popover.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["pds-popover.css"]
        };
    }
    static get properties() {
        return {
            "popoverTargetAction": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'show' | 'toggle' | 'hide'",
                    "resolved": "\"hide\" | \"show\" | \"toggle\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "defaultValue",
                            "text": "\"show\""
                        }],
                    "text": "Determines the action that triggers the popover. For manual popovers, the consumer is responsible for toggling this value."
                },
                "attribute": "popover-target-action",
                "reflect": false,
                "defaultValue": "'show'"
            },
            "popoverType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'auto' | 'manual'",
                    "resolved": "\"auto\" | \"manual\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Determines the type of popover. Auto popovers can be \"light dismissed\" by clicking outside of the popover.\nManual popovers require the consumer to handle the visibility of the popover."
                },
                "attribute": "popover-type",
                "reflect": false,
                "defaultValue": "'auto'"
            },
            "componentId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "A unique identifier used for the underlying component `id` attribute."
                },
                "attribute": "component-id",
                "reflect": false
            },
            "text": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Text that appears on the trigger element"
                },
                "attribute": "text",
                "reflect": false
            },
            "maxWidth": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "defaultValue",
                            "text": "352"
                        }],
                    "text": "Sets the maximum width of the popover content"
                },
                "attribute": "max-width",
                "reflect": false,
                "defaultValue": "352"
            },
            "placement": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "PlacementType",
                    "resolved": "\"bottom\" | \"bottom-end\" | \"bottom-start\" | \"left\" | \"left-end\" | \"left-start\" | \"right\" | \"right-end\" | \"right-start\" | \"top\" | \"top-end\" | \"top-start\"",
                    "references": {
                        "PlacementType": {
                            "location": "import",
                            "path": "@utils/types",
                            "id": "src/utils/types.ts::PlacementType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "defaultValue",
                            "text": "\"right\""
                        }],
                    "text": "Determines the preferred position of the popover"
                },
                "attribute": "placement",
                "reflect": true,
                "defaultValue": "'right'"
            }
        };
    }
    static get states() {
        return {
            "active": {}
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "click",
                "method": "handleClick",
                "target": undefined,
                "capture": true,
                "passive": false
            }, {
                "name": "scroll",
                "method": "handleScroll",
                "target": "window",
                "capture": true,
                "passive": true
            }];
    }
}
//# sourceMappingURL=pds-popover.js.map
