import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { booleanConverter } from "../../../converters/boolean-converter.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 { buildCustomEvent, listenOnce } from "../../../utils/event-utils.js";
import arrow_down from "../../../images/arrow-down.js";
import atomic_smart_snippet_expandable_answer_tw_css from "./atomic-smart-snippet-expandable-answer.tw.css.js";
import "../atomic-icon/atomic-icon.js";
import "../atomic-smart-snippet-answer/atomic-smart-snippet-answer.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 AtomicSmartSnippetExpandableAnswer extends LitElement {
    validateProps() {
        if (this.maximumHeight < this.collapsedHeight) throw new Error('maximumHeight must be greater than or equal to collapsedHeight');
    }
    initialize() {
        this.validateProps();
    }
    get showButton() {
        return void 0 !== this.fullHeight && this.fullHeight > this.maximumHeight;
    }
    get isExpanded() {
        return this.expanded || !this.showButton;
    }
    updated(changedProperties) {
        super.updated(changedProperties);
        if (changedProperties.has('fullHeight')) {
            this.style.setProperty('--full-height', `${this.fullHeight}px`);
            this.style.setProperty('--collapsed-size', `${this.showButton ? this.collapsedHeight : this.fullHeight}px`);
        }
    }
    async firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        this.fullHeight = await this.establishInitialHeight();
    }
    async establishInitialHeight() {
        const answerElement = document.createElement('atomic-smart-snippet-answer');
        answerElement.htmlContent = this.htmlContent;
        answerElement.innerStyle = this.snippetStyle;
        answerElement.style.visibility = 'hidden';
        answerElement.style.position = 'absolute';
        return new Promise((resolve)=>{
            listenOnce(answerElement, 'answerSizeUpdated', (event)=>{
                answerElement.remove();
                resolve(event.detail.height);
            });
            this.parentElement.appendChild(answerElement);
        });
    }
    handleExpand() {
        this.dispatchEvent(buildCustomEvent('expand'));
    }
    handleCollapse() {
        this.dispatchEvent(buildCustomEvent('collapse'));
    }
    handleSelectInlineLink(e) {
        this.dispatchEvent(buildCustomEvent('selectInlineLink', e.detail));
    }
    handleBeginDelayedSelectInlineLink(e) {
        this.dispatchEvent(buildCustomEvent('beginDelayedSelectInlineLink', e.detail));
    }
    handleCancelPendingSelectInlineLink(e) {
        this.dispatchEvent(buildCustomEvent('cancelPendingSelectInlineLink', e.detail));
    }
    renderAnswer() {
        return html`
      <div part="truncated-answer">
        <atomic-smart-snippet-answer
          exportparts="answer"
          .htmlContent=${this.htmlContent}
          .innerStyle=${this.snippetStyle}
          @answerSizeUpdated=${(e)=>{
            this.fullHeight = e.detail.height;
        }}
          @selectInlineLink=${this.handleSelectInlineLink}
          @beginDelayedSelectInlineLink=${this.handleBeginDelayedSelectInlineLink}
          @cancelPendingSelectInlineLink=${this.handleCancelPendingSelectInlineLink}
        ></atomic-smart-snippet-answer>
      </div>
    `;
    }
    renderButton() {
        return when(this.showButton, ()=>html`
        <button
          @click=${()=>this.isExpanded ? this.handleCollapse() : this.handleExpand()}
          class="text-primary mb-4 hover:underline"
          part=${this.isExpanded ? 'show-less-button' : 'show-more-button'}
        >
          ${this.bindings.i18n.t(this.isExpanded ? 'show-less' : 'show-more')}
          <atomic-icon
            .icon=${arrow_down}
            class="ml-2 w-3 align-baseline"
          ></atomic-icon>
        </button>
      `);
    }
    render() {
        return html`
      <div class=${this.isExpanded ? 'expanded' : ''}>
        ${this.renderAnswer()} ${this.renderButton()}
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.maximumHeight = 250, this.collapsedHeight = 180;
    }
}
AtomicSmartSnippetExpandableAnswer.styles = atomic_smart_snippet_expandable_answer_tw_css;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof AnyBindings ? Object : AnyBindings)
], AtomicSmartSnippetExpandableAnswer.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicSmartSnippetExpandableAnswer.prototype, "error", void 0);
_ts_decorate([
    property({
        type: Boolean,
        reflect: true,
        converter: booleanConverter
    }),
    _ts_metadata("design:type", Boolean)
], AtomicSmartSnippetExpandableAnswer.prototype, "expanded", void 0);
_ts_decorate([
    property({
        type: String
    }),
    _ts_metadata("design:type", String)
], AtomicSmartSnippetExpandableAnswer.prototype, "htmlContent", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true
    })
], AtomicSmartSnippetExpandableAnswer.prototype, "maximumHeight", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true
    })
], AtomicSmartSnippetExpandableAnswer.prototype, "collapsedHeight", void 0);
_ts_decorate([
    property({
        type: String
    }),
    _ts_metadata("design:type", String)
], AtomicSmartSnippetExpandableAnswer.prototype, "snippetStyle", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Number)
], AtomicSmartSnippetExpandableAnswer.prototype, "fullHeight", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicSmartSnippetExpandableAnswer.prototype, "render", null);
AtomicSmartSnippetExpandableAnswer = _ts_decorate([
    customElement('atomic-smart-snippet-expandable-answer'),
    bindings(),
    withTailwindStyles
], AtomicSmartSnippetExpandableAnswer);
export { AtomicSmartSnippetExpandableAnswer };
