import { NumberValue, Schema } from "@coveo/bueno";
import { buildSmartSnippetQuestionsList } from "@coveo/headless/insight";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { getAttributesFromLinkSlotContent } from "../../common/item-link/attributes-slot.js";
import { renderAnswerAndSourceWrapper } from "../../common/smart-snippets/atomic-smart-snippet-suggestions/answer-and-source-wrapper.js";
import { renderFooter } from "../../common/smart-snippets/atomic-smart-snippet-suggestions/footer.js";
import { getQuestionPart } from "../../common/smart-snippets/atomic-smart-snippet-suggestions/get-question-part.js";
import { renderQuestion } from "../../common/smart-snippets/atomic-smart-snippet-suggestions/question.js";
import { renderQuestionWrapper } from "../../common/smart-snippets/atomic-smart-snippet-suggestions/question-wrapper.js";
import { renderSuggestionsWrapper } from "../../common/smart-snippets/atomic-smart-snippet-suggestions/suggestions-wrapper.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-controller.js";
import { bindStateToController } from "../../../decorators/bind-state.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { randomID } from "../../../utils/utils.js";
import arrow_down from "../../../images/arrow-down.js";
import arrow_right from "../../../images/arrow-right.js";
import "../../common/atomic-icon/atomic-icon.js";
import "../../common/atomic-smart-snippet-source/atomic-smart-snippet-source.js";
import "../../common/atomic-smart-snippet-answer/atomic-smart-snippet-answer.js";
import atomic_insight_smart_snippet_suggestions_tw_css from "./atomic-insight-smart-snippet-suggestions.tw.css.js";
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 AtomicInsightSmartSnippetSuggestions extends LitElement {
    initialize() {
        this.accordionId = randomID('atomic-smart-snippet-suggestions-');
        this.smartSnippetQuestionsList = buildSmartSnippetQuestionsList(this.bindings.engine);
    }
    render() {
        const shouldDisplay = this.smartSnippetQuestionsListState.questions.length > 0;
        return html`${when(shouldDisplay, ()=>renderSuggestionsWrapper({
                props: {
                    headingLevel: this.headingLevel,
                    i18n: this.bindings.i18n
                }
            })(html`
          ${this.smartSnippetQuestionsListState.questions.map((relatedQuestion, i)=>this.renderRelatedQuestion(relatedQuestion, i))}
        `), ()=>nothing)}`;
    }
    renderRelatedQuestion(relatedQuestion, index) {
        return renderQuestionWrapper({
            props: {
                expanded: relatedQuestion.expanded,
                key: relatedQuestion.questionAnswerId
            }
        })(html`
      ${this.renderQuestionButton(relatedQuestion, index)}
      ${when(relatedQuestion.expanded, ()=>this.renderAnswer(relatedQuestion, index))}
    `);
    }
    renderQuestionButton(relatedQuestion, index) {
        return renderQuestion({
            props: {
                ariaControls: `${this.accordionId}-${index}`,
                expanded: relatedQuestion.expanded,
                headingLevel: this.headingLevel,
                onClick: ()=>this.toggleQuestion(relatedQuestion),
                question: relatedQuestion.question
            }
        })(html`
      <atomic-icon
        .icon=${relatedQuestion.expanded ? arrow_down : arrow_right}
        part=${getQuestionPart('icon', relatedQuestion.expanded)}
        class="mr-3 w-2.5 stroke-[1.25]"
      ></atomic-icon>
    `);
    }
    renderAnswer(relatedQuestion, index) {
        return renderAnswerAndSourceWrapper({
            props: {
                id: `${this.accordionId}-${index}`
            }
        })(html`
      <atomic-smart-snippet-answer
        exportparts="answer"
        .htmlContent=${relatedQuestion.answer}
        .innerStyle=${this.computedSnippetStyle}
        @selectInlineLink=${(e)=>this.smartSnippetQuestionsList.selectInlineLink(relatedQuestion.questionAnswerId, e.detail)}
        @beginDelayedSelectInlineLink=${(e)=>this.smartSnippetQuestionsList.beginDelayedSelectInlineLink(relatedQuestion.questionAnswerId, e.detail)}
        @cancelPendingSelectInlineLink=${(e)=>this.smartSnippetQuestionsList.cancelPendingSelectInlineLink(relatedQuestion.questionAnswerId, e.detail)}
      ></atomic-smart-snippet-answer>
      ${when(relatedQuestion.source, ()=>this.renderSource(relatedQuestion.questionAnswerId, relatedQuestion.source))}
    `);
    }
    renderSource(questionAnswerId, source) {
        return renderFooter({
            props: {
                i18n: this.bindings.i18n
            }
        })(html`
      <atomic-smart-snippet-source
        .anchorAttributes=${getAttributesFromLinkSlotContent(this, 'source-anchor-attributes')}
        @beginDelayedSelectSource=${()=>this.smartSnippetQuestionsList.beginDelayedSelectSource(questionAnswerId)}
        @cancelPendingSelectSource=${()=>this.smartSnippetQuestionsList.cancelPendingSelectSource(questionAnswerId)}
        @selectSource=${()=>this.smartSnippetQuestionsList.selectSource(questionAnswerId)}
        .source=${source}
      ></atomic-smart-snippet-source>
    `);
    }
    get computedSnippetStyle() {
        const styleTag = this.querySelector('template')?.content.querySelector('style');
        if (!styleTag) return this.snippetStyle;
        return styleTag.innerHTML;
    }
    toggleQuestion(relatedQuestion) {
        if (relatedQuestion.expanded) this.smartSnippetQuestionsList.collapse(relatedQuestion.questionAnswerId);
        else this.smartSnippetQuestionsList.expand(relatedQuestion.questionAnswerId);
    }
    constructor(){
        super(), this.headingLevel = 0;
        new ValidatePropsController(this, ()=>({
                headingLevel: this.headingLevel
            }), AtomicInsightSmartSnippetSuggestions.propsSchema, false);
    }
}
AtomicInsightSmartSnippetSuggestions.propsSchema = new Schema({
    headingLevel: new NumberValue({
        min: 0,
        max: 5,
        required: false
    })
});
AtomicInsightSmartSnippetSuggestions.styles = atomic_insight_smart_snippet_suggestions_tw_css;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightSmartSnippetSuggestions.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightSmartSnippetSuggestions.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('smartSnippetQuestionsList'),
    state(),
    _ts_metadata("design:type", "u" < typeof SmartSnippetQuestionsListState ? Object : SmartSnippetQuestionsListState)
], AtomicInsightSmartSnippetSuggestions.prototype, "smartSnippetQuestionsListState", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: Number,
        attribute: 'heading-level'
    })
], AtomicInsightSmartSnippetSuggestions.prototype, "headingLevel", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'snippet-style'
    }),
    _ts_metadata("design:type", String)
], AtomicInsightSmartSnippetSuggestions.prototype, "snippetStyle", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightSmartSnippetSuggestions.prototype, "render", null);
AtomicInsightSmartSnippetSuggestions = _ts_decorate([
    customElement('atomic-insight-smart-snippet-suggestions'),
    bindings(),
    withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicInsightSmartSnippetSuggestions);
export { AtomicInsightSmartSnippetSuggestions };
