"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__, {
    AtomicGeneratedAnswerContent: ()=>AtomicGeneratedAnswerContent
});
const external_lit_namespaceObject = require("lit");
const context_namespaceObject = require("@lit/context");
const decorators_js_namespaceObject = require("lit/decorators.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const atomic_generated_answer_tw_css_cjs_namespaceObject = require("../../search/atomic-generated-answer/atomic-generated-answer.tw.css.cjs");
var atomic_generated_answer_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(atomic_generated_answer_tw_css_cjs_namespaceObject);
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
require("../atomic-agent-stream-of-thought/atomic-agent-stream-of-thought.cjs");
const generated_content_container_cjs_namespaceObject = require("../generated-answer/generated-content-container.cjs");
const render_feedback_and_copy_buttons_cjs_namespaceObject = require("../generated-answer/render-feedback-and-copy-buttons.cjs");
const source_citations_cjs_namespaceObject = require("../generated-answer/source-citations.cjs");
const external_answer_context_cjs_namespaceObject = require("./answer-context.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);
}
const COPY_RESET_DURATION_MS = 2000;
class AtomicGeneratedAnswerContent extends external_lit_namespaceObject.LitElement {
    disconnectedCallback() {
        super.disconnectedCallback();
        clearTimeout(this.resetCopyTimeout);
    }
    render() {
        const { answer, answerContentFormat, isStreaming, generationSteps, citations = [], answerId, error, cannotAnswer } = this.generatedAnswer || {};
        this.answerId = answerId || '';
        if (error) return this.renderError();
        if (cannotAnswer) return this.renderCannotAnswer();
        if (!answerId) return (0, external_lit_namespaceObject.html)``;
        const shouldRenderFeedbackAndCopyButtons = Boolean(answer) && !isStreaming;
        return (0, external_lit_namespaceObject.html)`
      <div>
        <atomic-agent-stream-of-thought
          .i18n=${this.i18n}
          .agentSteps=${generationSteps ?? []}
          .isStreaming=${Boolean(isStreaming)}
        ></atomic-agent-stream-of-thought>
        <div>
          ${(0, generated_content_container_cjs_namespaceObject.renderGeneratedContentContainer)({
            props: {
                answer,
                answerContentFormat,
                isStreaming: Boolean(isStreaming)
            }
        })((0, external_lit_namespaceObject.html)`
            ${(0, source_citations_cjs_namespaceObject.renderSourceCitations)({
            props: {
                label: this.i18n.t('citations'),
                isVisible: citations.length > 0
            }
        })((0, external_lit_namespaceObject.html)`${this.renderCitations(citations, answerId)}`)}
          `)}
        </div>
        ${(0, when_js_namespaceObject.when)(shouldRenderFeedbackAndCopyButtons, ()=>this.renderFeedbackAndCopyButtons(answerId))}
      </div>
    `;
    }
    renderFeedbackAndCopyButtons(answerId) {
        return (0, external_lit_namespaceObject.html)`
      <div class="mt-4" part="feedback-and-copy-buttons">
        ${(0, render_feedback_and_copy_buttons_cjs_namespaceObject.renderFeedbackAndCopyButtons)({
            props: {
                i18n: this.i18n,
                generatedAnswerActionsState: {
                    liked: this.generatedAnswer.liked,
                    disliked: this.generatedAnswer.disliked,
                    isStreaming: this.generatedAnswer.isStreaming,
                    isLoading: this.generatedAnswer.isLoading,
                    answer: this.generatedAnswer.answer
                },
                copied: 'success' === this.copyState,
                copyError: 'error' === this.copyState,
                getCopyToClipboardTooltip: ()=>this.getCopyToClipboardTooltip(),
                onClickLike: ()=>this.onClickLike(answerId),
                onClickDislike: ()=>this.onClickDislike(answerId),
                onCopyToClipboard: ()=>this.copyToClipboard()
            }
        })}
      </div>
    `;
    }
    async copyToClipboard() {
        const { answer, answerId } = this.generatedAnswer;
        if (!answer || !answerId) return;
        try {
            await navigator.clipboard.writeText(answer);
            this.copyState = 'success';
            this.onCopyToClipboard(answerId);
        } catch (error) {
            this.copyState = 'error';
            console.error(`Failed to copy to clipboard: ${error}`);
        }
        this.scheduleCopyReset();
    }
    scheduleCopyReset() {
        clearTimeout(this.resetCopyTimeout);
        this.resetCopyTimeout = window.setTimeout(()=>{
            this.copyState = 'idle';
        }, COPY_RESET_DURATION_MS);
    }
    getCopyToClipboardTooltip() {
        switch(this.copyState){
            case 'error':
                return this.i18n.t('failed-to-copy-generated-answer');
            case 'success':
                return this.i18n.t('generated-answer-copied');
            default:
                return this.i18n.t('copy-generated-answer');
        }
    }
    renderError() {
        const errorMessageKey = this.generatedAnswer.error?.isSseTurnLimitReachedError?.() ? 'generated-answer-error-turn-limit-reached' : 'generated-answer-error-generic';
        return (0, external_lit_namespaceObject.html)`
      <div part="generated-answer-error">
        <p>${this.i18n.t(errorMessageKey)}</p>
      </div>
    `;
    }
    renderCannotAnswer() {
        return (0, external_lit_namespaceObject.html)`
      <div part="generated-answer-cannot-answer">
        <p>${this.i18n.t('generated-answer-cannot-generate-answer')}</p>
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.answerId = '', this.renderCitations = ()=>(0, external_lit_namespaceObject.html)``, this.onClickLike = ()=>{}, this.onClickDislike = ()=>{}, this.onCopyToClipboard = ()=>{}, this.copyState = 'idle';
    }
}
AtomicGeneratedAnswerContent.styles = [
    atomic_generated_answer_tw_css_cjs_default()
];
_ts_decorate([
    (0, context_namespaceObject.provide)({
        context: external_answer_context_cjs_namespaceObject.answerContext
    })
], AtomicGeneratedAnswerContent.prototype, "answerId", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof GeneratedAnswer ? Object : GeneratedAnswer)
], AtomicGeneratedAnswerContent.prototype, "generatedAnswer", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof i18n ? Object : i18n)
], AtomicGeneratedAnswerContent.prototype, "i18n", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: false
    }),
    _ts_metadata("design:type", Function)
], AtomicGeneratedAnswerContent.prototype, "renderCitations", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: false
    }),
    _ts_metadata("design:type", Function)
], AtomicGeneratedAnswerContent.prototype, "onClickLike", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: false
    }),
    _ts_metadata("design:type", Function)
], AtomicGeneratedAnswerContent.prototype, "onClickDislike", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: false
    }),
    _ts_metadata("design:type", Function)
], AtomicGeneratedAnswerContent.prototype, "onCopyToClipboard", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CopyState ? Object : CopyState)
], AtomicGeneratedAnswerContent.prototype, "copyState", void 0);
AtomicGeneratedAnswerContent = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-generated-answer-content'),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicGeneratedAnswerContent);
exports.AtomicGeneratedAnswerContent = __webpack_exports__.AtomicGeneratedAnswerContent;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicGeneratedAnswerContent"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
