"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__, {
    AtomicSmartSnippetAnswer: ()=>AtomicSmartSnippetAnswer
});
const external_dompurify_namespaceObject = require("dompurify");
var external_dompurify_default = /*#__PURE__*/ __webpack_require__.n(external_dompurify_namespaceObject);
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const unsafe_html_js_namespaceObject = require("lit/directives/unsafe-html.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const bind_analytics_to_link_cjs_namespaceObject = require("../item-link/bind-analytics-to-link.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const external_atomic_smart_snippet_answer_tw_css_cjs_namespaceObject = require("./atomic-smart-snippet-answer.tw.css.cjs");
var external_atomic_smart_snippet_answer_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(external_atomic_smart_snippet_answer_tw_css_cjs_namespaceObject);
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 AtomicSmartSnippetAnswer extends external_lit_namespaceObject.LitElement {
    disconnectedCallback() {
        super.disconnectedCallback();
        this.resizeObserver?.disconnect();
        this.cleanupAnalyticsFunctions.forEach((cleanup)=>cleanup());
        this.cleanupAnalyticsFunctions = [];
    }
    willUpdate() {
        this.isRendering = true;
    }
    updated() {
        this.isRendering = false;
        this.emitCurrentHeight();
        this.bindAnalyticsToLinks();
    }
    firstUpdated() {
        requestAnimationFrame(()=>{
            this.classList.add('loaded');
        });
        this.setupResizeObserver();
    }
    render() {
        return (0, external_lit_namespaceObject.html)`${this.renderStyle()} ${this.renderContent()}`;
    }
    setupResizeObserver() {
        if (this.wrapperRef.value) {
            if (this.resizeObserver) this.resizeObserver.disconnect();
            this.resizeObserver = new ResizeObserver(()=>this.emitCurrentHeight());
            this.resizeObserver.observe(this.wrapperRef.value);
        }
    }
    get sanitizedStyle() {
        if (!this.innerStyle) return;
        return (0, utils_cjs_namespaceObject.sanitizeStyle)(this.innerStyle);
    }
    emitCurrentHeight() {
        if (this.isRendering || !this.wrapperRef.value) return;
        this.dispatchEvent(new CustomEvent('answerSizeUpdated', {
            detail: {
                height: this.wrapperRef.value.scrollHeight
            },
            bubbles: false
        }));
    }
    bindAnalyticsToLink(element) {
        const link = {
            linkText: element.innerText,
            linkURL: element.href
        };
        const cleanup = (0, bind_analytics_to_link_cjs_namespaceObject.bindAnalyticsToLink)(element, {
            stopPropagation: false,
            onSelect: ()=>this.dispatchEvent(new CustomEvent('selectInlineLink', {
                    detail: link,
                    bubbles: false
                })),
            onBeginDelayedSelect: ()=>this.dispatchEvent(new CustomEvent('beginDelayedSelectInlineLink', {
                    detail: link,
                    bubbles: false
                })),
            onCancelPendingSelect: ()=>this.dispatchEvent(new CustomEvent('cancelPendingSelectInlineLink', {
                    detail: link,
                    bubbles: false
                }))
        });
        this.cleanupAnalyticsFunctions.push(cleanup);
    }
    bindAnalyticsToLinks() {
        if (!this.contentRef.value) return;
        this.cleanupAnalyticsFunctions.forEach((cleanup)=>cleanup());
        this.cleanupAnalyticsFunctions = [];
        Array.from(this.contentRef.value.querySelectorAll('a')).forEach((link)=>this.bindAnalyticsToLink(link));
    }
    renderStyle() {
        const style = this.sanitizedStyle;
        return (0, when_js_namespaceObject.when)(style, ()=>(0, external_lit_namespaceObject.html)`<style>
          ${(0, unsafe_html_js_namespaceObject.unsafeHTML)(style)}
        </style>`);
    }
    renderContent() {
        return (0, external_lit_namespaceObject.html)`
      <div class="wrapper" ${(0, ref_js_namespaceObject.ref)(this.wrapperRef)}>
        <div part="answer" class="margin" ${(0, ref_js_namespaceObject.ref)(this.contentRef)}>
          ${(0, unsafe_html_js_namespaceObject.unsafeHTML)(external_dompurify_default().sanitize(this.htmlContent, {
            USE_PROFILES: {
                html: true
            }
        }))}
        </div>
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.wrapperRef = (0, ref_js_namespaceObject.createRef)(), this.contentRef = (0, ref_js_namespaceObject.createRef)(), this.isRendering = true, this.cleanupAnalyticsFunctions = [];
    }
}
AtomicSmartSnippetAnswer.styles = external_atomic_smart_snippet_answer_tw_css_cjs_default();
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'html-content'
    }),
    _ts_metadata("design:type", String)
], AtomicSmartSnippetAnswer.prototype, "htmlContent", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'inner-style'
    }),
    _ts_metadata("design:type", String)
], AtomicSmartSnippetAnswer.prototype, "innerStyle", void 0);
AtomicSmartSnippetAnswer = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-smart-snippet-answer'),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicSmartSnippetAnswer);
exports.AtomicSmartSnippetAnswer = __webpack_exports__.AtomicSmartSnippetAnswer;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicSmartSnippetAnswer"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
