import { SafeStorage, StorageItems } from "../../../utils/local-storage-utils.js";
import "../atomic-generated-answer-feedback-modal/atomic-generated-answer-feedback-modal.js";
class GeneratedAnswerController {
    hostConnected() {}
    get data() {
        return this._data;
    }
    set data(newData) {
        this._data = newData;
    }
    insertFeedbackModal() {
        this.modalRef = document.createElement('atomic-generated-answer-feedback-modal');
        const generatedAnswer = this.options.getGeneratedAnswer();
        if (generatedAnswer) this.modalRef.generatedAnswer = generatedAnswer;
        this.host.insertAdjacentElement('beforebegin', this.modalRef);
    }
    readStoredData() {
        const { withToggle } = this.options;
        const storedData = this.storage.getParsedJSON(StorageItems.GENERATED_ANSWER_DATA, {
            isVisible: true
        });
        return {
            isVisible: withToggle && storedData.isVisible || !withToggle
        };
    }
    writeStoredData(data) {
        this.storage.setJSON(StorageItems.GENERATED_ANSWER_DATA, data);
    }
    getGeneratedAnswerStatus() {
        const state = this.options.getGeneratedAnswerState();
        const bindings = this.options.getBindings();
        if (!state) return '';
        const isHidden = !state.isVisible;
        const isGenerating = !!state.isStreaming;
        const hasNonEmptyAnswer = !!state.answer?.trim();
        const hasError = !!state.error;
        if (isHidden) return bindings.i18n.t('generated-answer-hidden');
        if (isGenerating) return bindings.i18n.t('generating-answer');
        if (hasError) return bindings.i18n.t('answer-could-not-be-generated');
        if (hasNonEmptyAnswer) return bindings.i18n.t('answer-generated', {
            answer: state.answer
        });
        return '';
    }
    get hasRetryableError() {
        const searchState = this.options.getSearchStatusState();
        const answerState = this.options.getGeneratedAnswerState();
        return !searchState?.hasError && !!answerState?.error?.isRetryable;
    }
    get hasNoAnswerGenerated() {
        const { answer, citations } = this.options.getGeneratedAnswerState() ?? {};
        const isAnswerEmpty = !answer?.trim();
        const hasNoCitations = !citations?.length;
        const hasNoRetryableError = !this.hasRetryableError;
        return isAnswerEmpty && hasNoCitations && hasNoRetryableError;
    }
    get isAnswerVisible() {
        return !!this.options.getGeneratedAnswerState()?.isVisible;
    }
    getToggleTooltip() {
        const key = this.isAnswerVisible ? 'generated-answer-toggle-on' : 'generated-answer-toggle-off';
        return this.options.getBindings().i18n.t(key);
    }
    getCopyToClipboardTooltip(copied, copyError) {
        const bindings = this.options.getBindings();
        if (copyError) return bindings.i18n.t('failed-to-copy-generated-answer');
        return copied ? bindings.i18n.t('generated-answer-copied') : bindings.i18n.t('copy-generated-answer');
    }
    async copyToClipboard(answer, onCopySuccess, onCopyError) {
        try {
            await navigator.clipboard.writeText(answer);
            onCopySuccess();
            this.options.getGeneratedAnswer()?.logCopyToClipboard();
        } catch (error) {
            onCopyError();
            this.options.getBindings().engine.logger.error(`Failed to copy to clipboard: ${error}`);
        }
    }
    clickOnShowButton() {
        const state = this.options.getGeneratedAnswerState();
        const generatedAnswer = this.options.getGeneratedAnswer();
        if (state?.expanded) generatedAnswer?.collapse();
        else generatedAnswer?.expand();
    }
    clickDislike() {
        this.setIsAnswerHelpful(false);
        this.options.getGeneratedAnswer()?.dislike();
        this.openFeedbackModal();
    }
    clickLike() {
        this.setIsAnswerHelpful(true);
        this.options.getGeneratedAnswer()?.like();
        this.openFeedbackModal();
    }
    setIsAnswerHelpful(isHelpful) {
        if (this.modalRef) this.modalRef.helpful = isHelpful;
    }
    openFeedbackModal() {
        const state = this.options.getGeneratedAnswerState();
        if (this.modalRef && !state?.feedbackSubmitted) this.modalRef.isOpen = true;
    }
    constructor(host, options){
        this.storage = new SafeStorage();
        this.host = host;
        this.options = options;
        this._data = this.readStoredData();
        host.addController(this);
    }
}
export { GeneratedAnswerController };
