'use strict';

var index = require('./index-BEF5cZxd.js');
var props = require('./props-CPENhBnM.js');
var colors = require('./colors-DgrrxjsX.js');
var sizes = require('./sizes-D5zsw2g5.js');
var utils = require('./utils-NnC78QR3.js');
var string_utils = require('./string.utils-BG4pUfEi.js');
var template = require('./template-Cj6PcLmR.js');
var gov_log = require('./gov.log-C5EH15b_.js');
var stencil_utils = require('./stencil.utils-8sCYbm34.js');

const TooltipColors = utils.pick(colors.Colors, ['PRIMARY', 'NEUTRAL']);
const TooltipSize = utils.pick(sizes.Sizes, ['_S', '_M', '_L']);
var TooltipPosition;
(function (TooltipPosition) {
    TooltipPosition["TOP"] = "top";
    TooltipPosition["RIGHT"] = "right";
    TooltipPosition["BOTTOM"] = "bottom";
    TooltipPosition["LEFT"] = "left";
})(TooltipPosition || (TooltipPosition = {}));
const TooltipClass = {
    root: 'gov-tooltip',
};
const TooltipContentClass = {
    root: 'gov-tooltip-content',
};

const GovTooltip = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.tooltipContentId = undefined;
        this.tooltipId = undefined;
        /**
         * Style variation of the tooltip
         */
        this.color = TooltipColors.PRIMARY;
        /**
         * Tooltip’s size.
         */
        this.size = TooltipSize._M;
        /**
         * Tooltip display position
         */
        this.position = "right";
        /**
         * Information contained in the tooltip
         */
        this.message = undefined;
        /**
         * Trigger is just an icon
         */
        this.icon = undefined;
        this.persist = false;
        this.h = template.govHost(this.host);
        this.tooltipId = string_utils.createID("GovTooltip");
        this.tooltipContentId = string_utils.createID("GovContentTooltip");
    }
    validateColor(newValue) {
        props.validateProp(TooltipColors, newValue, TooltipClass.root);
    }
    validateSize(newValue) {
        props.validateProp(TooltipSize, newValue, TooltipClass.root);
    }
    validatePosition(newValue) {
        props.validateProp(TooltipPosition, newValue, TooltipClass.root);
    }
    rerenderMessage() {
        this.createContentWrapper();
    }
    connectedCallback() {
        this.createContentWrapper();
        this.registerListeners();
    }
    componentWillLoad() {
        this.validateColor(this.color);
        this.validateSize(this.size);
        this.validatePosition(this.position);
    }
    componentDidUpdate() {
        this.createContentWrapper();
    }
    disconnectedCallback() {
        this.removeContent();
    }
    createContentWrapper() {
        var _a;
        if (this.tooltipContent) {
            if (this.message) {
                this.tooltipContent.innerHTML = this.message;
            }
            else if (this.insideContent) {
                this.tooltipContent.innerHTML = this.insideContent.innerHTML;
            }
            return;
        }
        let content;
        if (this.insideContent) {
            content = this.insideContent;
        }
        else {
            content = gov_log.documentNode().createElement(TooltipContentClass.root);
            content.innerHTML = this.message;
        }
        content.setAttribute("id", (_a = this.identifier) !== null && _a !== void 0 ? _a : this.tooltipContentId);
        content.setAttribute("parent-id", this.tooltipId);
        content.setAttribute("color", this.color);
        content.setAttribute("size", this.size);
        gov_log.documentNode().body.appendChild(content);
    }
    get tooltipContent() {
        var _a;
        return gov_log.documentNode().body.querySelector(`#${(_a = this.identifier) !== null && _a !== void 0 ? _a : this.tooltipContentId}`);
    }
    get insideContent() {
        return this.host.querySelector(TooltipContentClass.root);
    }
    registerListeners() {
        this.host.addEventListener("click", e => {
            e.preventDefault();
            this.persist = true;
            if (this.tooltipContent) {
                this.tooltipContent.setAttribute("persist", "true");
            }
            this.showTooltip();
        });
        this.host.addEventListener("mouseenter", e => {
            e.preventDefault();
            this.showTooltip();
        });
        this.host.addEventListener("mouseleave", e => {
            e.preventDefault();
            if (this.persist === false) {
                this.hideTooltip();
            }
        });
    }
    verifyPositionSpace() {
        const offset = 12;
        const windowWidth = window.innerWidth - (window.innerWidth - document.documentElement.clientWidth);
        const trigger = this.host.getBoundingClientRect();
        const trgWidth = this.host.offsetWidth;
        const trgHeight = this.host.offsetHeight;
        const contentWidth = this.tooltipContent.offsetWidth;
        const contentHeight = this.tooltipContent.offsetHeight;
        const triggerPos = {
            left: trigger.left + document.documentElement.scrollLeft,
            top: trigger.top + document.documentElement.scrollTop,
        };
        const isRightSpace = ((triggerPos.left + offset + trigger.width) + contentWidth) <= windowWidth;
        const isLeftSpace = triggerPos.left > contentWidth;
        // LEFT/RIGHT
        if (this.position === TooltipPosition.LEFT || this.position === TooltipPosition.RIGHT) {
            this.tooltipContent.style.top = (triggerPos.top - ((contentHeight - trgHeight) / 2)) + "px";
            if (isRightSpace === false && isLeftSpace === false) {
                this.position = TooltipPosition.TOP;
                this.tooltipContent.setAttribute("position", TooltipPosition.TOP);
            }
            if (isLeftSpace && isRightSpace === false && this.position === TooltipPosition.RIGHT) {
                this.position = TooltipPosition.LEFT;
            }
            if (isRightSpace && isLeftSpace === false && this.position === TooltipPosition.LEFT) {
                this.position = TooltipPosition.RIGHT;
            }
        }
        if (this.position === TooltipPosition.RIGHT) {
            this.tooltipContent.setAttribute("position", TooltipPosition.RIGHT);
            this.tooltipContent.style.left = (triggerPos.left + offset + trigger.width) + "px";
        }
        if (this.position === TooltipPosition.LEFT) {
            this.tooltipContent.setAttribute("position", TooltipPosition.LEFT);
            this.tooltipContent.style.left = (triggerPos.left - (contentWidth + offset)) + "px";
        }
        // LEFT/RIGHT FOR TOP/BOTTOM
        if (this.position === TooltipPosition.TOP || this.position === TooltipPosition.BOTTOM) {
            if (contentWidth > trgWidth) {
                let leftPosition = (triggerPos.left - ((contentWidth - trgWidth) / 2));
                const isNotLeftSpace = leftPosition < 0;
                const isNotRightSpace = (leftPosition + contentWidth) > windowWidth;
                if (isNotLeftSpace && isNotRightSpace) {
                    this.tooltipContent.style.width = "auto";
                    this.tooltipContent.style.left = offset + "px";
                    this.tooltipContent.style.right = offset + "px";
                }
                else {
                    if (isNotLeftSpace) {
                        leftPosition = offset;
                    }
                    if (isNotRightSpace) {
                        leftPosition = leftPosition - ((windowWidth - (leftPosition + contentWidth + offset)) * -1);
                    }
                    this.tooltipContent.style.left = leftPosition + "px";
                }
            }
            else {
                const leftPosition = (triggerPos.left + ((trgWidth - contentWidth) / 2));
                this.tooltipContent.style.left = leftPosition + "px";
            }
        }
        if (this.position === TooltipPosition.TOP) {
            this.tooltipContent.setAttribute("position", TooltipPosition.TOP);
            this.tooltipContent.style.top = (triggerPos.top - (contentHeight + offset)) + "px";
        }
        if (this.position === TooltipPosition.BOTTOM) {
            this.tooltipContent.setAttribute("position", TooltipPosition.BOTTOM);
            this.tooltipContent.style.top = (triggerPos.top + (trgHeight + offset)) + "px";
        }
        this.tooltipContent.style.opacity = '1';
    }
    showTooltip() {
        if (this.tooltipContent) {
            this.tooltipContent.style.opacity = '0';
            this.tooltipContent.hidden = false;
            this.tooltipContent.setAttribute("is-visible", "true");
            stencil_utils.tick(() => {
                this.verifyPositionSpace();
            });
        }
    }
    hideTooltip() {
        if (this.tooltipContent) {
            this.tooltipContent.setAttribute("is-visible", "false");
            this.tooltipContent.setAttribute("persist", "false");
            this.tooltipContent.removeAttribute('style');
        }
    }
    removeContent() {
        const contentEl = document.getElementById(this.tooltipContentId);
        if (contentEl) {
            contentEl.remove();
        }
    }
    render() {
        var _a;
        return (index.h(index.Host, { key: 'f64ba7c26358426ead39dadbb17687a39a4fb876', id: this.tooltipId, class: this.h.classes(TooltipClass.root), position: this.position }, index.h("span", { key: 'd51fc3b886df342fe8fbcaa9452be316658c24b2', role: 'button', tabindex: 0, onKeyPress: () => this.showTooltip(), onBlur: () => {
                if (this.persist === false) {
                    this.hideTooltip();
                }
            }, "aria-describedby": (_a = this.identifier) !== null && _a !== void 0 ? _a : this.tooltipContentId }, index.h("slot", { key: '6687d0562b3e99ea0e44ec97dadc5a2e085ff0a8' }))));
    }
    /**
     * Showing the modal
     */
    async show() {
        this.showTooltip();
    }
    /**
     * Hiding the modal
     */
    async hide() {
        this.persist = false;
        this.hideTooltip();
    }
    get host() { return index.getElement(this); }
    static get watchers() { return {
        "color": ["validateColor"],
        "size": ["validateSize"],
        "position": ["validatePosition"],
        "message": ["rerenderMessage"]
    }; }
};

const GovTooltipContent = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        /**
         * Style variation of the tooltip
         */
        this.color = TooltipColors.PRIMARY;
        /**
         * Tooltip’s size.
         */
        this.size = TooltipSize._M;
        /**
         * Tooltip is still visible.
         */
        this.persist = false;
        /**
         * Indicates display of tooltip content
         */
        this.isVisible = false;
        /**
         * Indicates display of tooltip content
         */
        this.parentId = null;
    }
    visibleContent(newValue) {
        clearTimeout(this.timeout);
        if (newValue === false) {
            this.timeout = setTimeout(() => {
                this.host.style.visibility = "hidden";
            }, 250);
        }
        else {
            this.host.style.visibility = "visible";
        }
    }
    hideContent() {
        this.isVisible = false;
        this.persist = false;
        const parentEl = document.getElementById(this.parentId);
        if (parentEl) {
            parentEl.hide().catch();
        }
    }
    render() {
        return (index.h(index.Host, { key: 'a826527fe9f7851ad25f700156d987c1b29f3adc', class: TooltipContentClass.root, role: "tooltip", hidden: !this.isVisible, "aria-hidden": !template.toBoolAttr(this.isVisible), "is-visible": this.isVisible }, index.h("slot", { key: 'd3891d8a3f3df7ee7f852db8485c7ecc2f61992c' }), this.persist ? (index.h("gov-button", { "on-gov-click": this.hideContent.bind(this), color: this.color, size: this.size, type: "base" }, index.h("gov-icon", { slot: "icon-start", name: "x-lg", type: "components" }))) : null));
    }
    get host() { return index.getElement(this); }
    static get watchers() { return {
        "isVisible": ["visibleContent"]
    }; }
};

exports.gov_tooltip = GovTooltip;
exports.gov_tooltip_content = GovTooltipContent;
//# sourceMappingURL=gov-tooltip.gov-tooltip-content.entry.cjs.js.map

//# sourceMappingURL=gov-tooltip_2.cjs.entry.js.map