"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AtomicCitation: ()=>AtomicCitation
});
const core_namespaceObject = require("@popperjs/core");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const class_map_js_namespaceObject = require("lit/directives/class-map.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const watch_cjs_namespaceObject = require("../../../decorators/watch.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const external_heading_cjs_namespaceObject = require("../heading.cjs");
const item_link_cjs_namespaceObject = require("../item-link/item-link.cjs");
const external_atomic_citation_tw_css_cjs_namespaceObject = require("./atomic-citation.tw.css.cjs");
var external_atomic_citation_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(external_atomic_citation_tw_css_cjs_namespaceObject);
const external_citation_anchoring_utils_cjs_namespaceObject = require("./citation-anchoring-utils.cjs");
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicCitation extends external_lit_namespaceObject.LitElement {
    disconnectedCallback() {
        super.disconnectedCallback();
        this.cleanupPopper();
        this.clearTimers();
        this.removeEventListener('keydown', this.handleKeyDown);
    }
    connectedCallback() {
        super.connectedCallback();
        this.addEventListener('keydown', this.handleKeyDown);
    }
    sendHoverAnalytics() {
        if (this.isOpen) {
            this.hoverStart = Date.now();
            return;
        }
        if (!this.hoverStart) return;
        const difference = Date.now() - this.hoverStart;
        if (difference > this.hoverAnalyticsTimeout) this.sendHoverEndEvent(difference);
        this.hoverStart = void 0;
    }
    updated() {
        this.updatePopper();
    }
    render() {
        return (0, external_lit_namespaceObject.html)`
      <div class="relative">
        ${(0, item_link_cjs_namespaceObject.renderLinkWithItemAnalytics)({
            props: {
                href: this.anchorUrl(this.citation.clickUri ?? this.citation.uri, this.citation.text, this.citation.filetype ?? this.citation.fields?.filetype),
                ref: this.citationRef,
                part: 'citation',
                target: '_blank',
                rel: 'noopener',
                attributes: [
                    this.ariaHaspopupAttr
                ],
                className: 'bg-background btn-outline-primary border-neutral text-on-background flex items-center rounded-full border p-1',
                onSelect: ()=>this.interactiveCitation.select(),
                onBeginDelayedSelect: ()=>this.interactiveCitation.beginDelayedSelect(),
                onCancelPendingSelect: ()=>this.interactiveCitation.cancelPendingSelect(),
                stopPropagation: this.stopPropagation,
                onMouseLeave: this.delayedClosePopover,
                onMouseOver: this.delayedPopoverOpen,
                onFocus: this.openPopover,
                onBlur: this.closePopover
            }
        })((0, external_lit_namespaceObject.html)`
          <span class="citation-title mx-1 truncate">
            ${this.citation.title}
          </span>
        `)}
        ${this.renderPopover()}
      </div>
    `;
    }
    updatePopper() {
        if (this.popperInstance) return void this.popperInstance.forceUpdate();
        const citationEl = this.citationRef.value;
        const popupEl = this.popupRef.value;
        if (!citationEl || !popupEl) return;
        this.popperInstance = (0, core_namespaceObject.createPopper)(citationEl, popupEl, {
            placement: 'top-start',
            modifiers: [
                {
                    name: 'offset',
                    options: {
                        offset: [
                            0,
                            6
                        ]
                    }
                },
                core_namespaceObject.preventOverflow
            ]
        });
    }
    cleanupPopper() {
        if (this.popperInstance) {
            this.popperInstance.destroy();
            this.popperInstance = void 0;
        }
    }
    clearTimers() {
        clearTimeout(this.hoverTimeout);
        clearTimeout(this.closePopoverTimeout);
    }
    getTruncatedText() {
        return this.citation.text && `${this.citation.text?.trim().slice(0, 200)}${this.citation.text.length > 200 ? '...' : ''}`;
    }
    anchorUrl(uri, text, filetype, pageNumber) {
        if (this.disableCitationAnchoring) return uri;
        switch(filetype){
            case 'html':
                return (0, external_citation_anchoring_utils_cjs_namespaceObject.generateTextFragmentUrl)(uri, text, filetype);
            case 'pdf':
                return (0, external_citation_anchoring_utils_cjs_namespaceObject.generatePdfPageUrl)(uri, pageNumber);
            default:
                return uri;
        }
    }
    renderPopover() {
        return (0, external_lit_namespaceObject.html)`
      <div
        ${(0, ref_js_namespaceObject.ref)(this.popupRef)}
        part="citation-popover"
        class="border-neutral bg-background mobile-only:hidden z-10 rounded-md border p-4 shadow flex-col gap-3 ${(0, class_map_js_namespaceObject.classMap)({
            'desktop-only:flex': this.isOpen,
            hidden: !this.isOpen
        })}"
        role="dialog"
        @mouseenter=${this.cancelClosePopover}
        @mouseleave=${this.delayedClosePopover}
      >
        <div class="text-neutral-dark truncate text-sm">
          ${this.citation.clickUri ?? this.citation.uri}
        </div>
        ${(0, external_heading_cjs_namespaceObject.renderHeading)({
            props: {
                level: 0,
                class: 'text-md font-bold'
            }
        })((0, external_lit_namespaceObject.html)`${this.citation.title}`)}
        <p class="text-on-background text-sm">${this.getTruncatedText()}</p>
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.disableCitationAnchoring = false, this.isOpen = false, this.citationRef = (0, ref_js_namespaceObject.createRef)(), this.popupRef = (0, ref_js_namespaceObject.createRef)(), this.hoverAnalyticsTimeout = 1000, this.hoverDebounceTimeoutMs = 200, this.closePopoverDebounceMs = 100, this.ariaHaspopupAttr = (()=>{
            const attr = document.createAttribute('aria-haspopup');
            attr.value = 'dialog';
            return attr;
        })(), this.openPopover = ()=>{
            this.isOpen = true;
        }, this.closePopover = ()=>{
            this.clearTimers();
            this.isOpen = false;
        }, this.handleKeyDown = (e)=>{
            if ('Escape' === e.key && this.isOpen) this.closePopover();
        }, this.delayedClosePopover = ()=>{
            this.clearTimers();
            this.closePopoverTimeout = setTimeout(()=>{
                this.isOpen = false;
            }, this.closePopoverDebounceMs);
        }, this.cancelClosePopover = ()=>{
            clearTimeout(this.closePopoverTimeout);
        }, this.delayedPopoverOpen = ()=>{
            this.clearTimers();
            this.hoverTimeout = setTimeout(this.openPopover, this.hoverDebounceTimeoutMs);
        };
    }
}
AtomicCitation.styles = external_atomic_citation_tw_css_cjs_default();
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof GeneratedAnswerCitation ? Object : GeneratedAnswerCitation)
], AtomicCitation.prototype, "citation", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number
    }),
    _ts_metadata("design:type", Number)
], AtomicCitation.prototype, "index", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", Function)
], AtomicCitation.prototype, "sendHoverEndEvent", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof InteractiveCitation ? Object : InteractiveCitation)
], AtomicCitation.prototype, "interactiveCitation", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'disable-citation-anchoring'
    })
], AtomicCitation.prototype, "disableCitationAnchoring", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicCitation.prototype, "isOpen", void 0);
_ts_decorate([
    (0, watch_cjs_namespaceObject.watch)('isOpen'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCitation.prototype, "sendHoverAnalytics", null);
AtomicCitation = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-citation'),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicCitation);
exports.AtomicCitation = __webpack_exports__.AtomicCitation;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCitation"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
