import { Schema, StringValue } from "@coveo/bueno";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { createRef } from "lit/directives/ref.js";
import { createProductContextController } from "../product-template-component-utils/context/product-context-controller.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 { renderExpandableText } from "../../common/expandable-text/expandable-text.js";
import "../atomic-product-text/atomic-product-text.js";
import { LightDomMixin } from "../../../mixins/light-dom.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 AtomicProductExcerpt extends LightDomMixin(LitElement) {
    initialize() {}
    validateProps() {
        new Schema({
            truncateAfter: new StringValue({
                constrainTo: [
                    'none',
                    '1',
                    '2',
                    '3',
                    '4'
                ]
            })
        }).validate({
            truncateAfter: this.truncateAfter
        });
    }
    updated(changedProperties) {
        super.updated(changedProperties);
        if (changedProperties.has('truncateAfter')) this.validateProps();
        if (this.excerptRef.value) this.resizeObserver.observe(this.excerptRef.value);
    }
    onToggleExpand(e) {
        if (e) e.stopPropagation();
        this.isExpanded = !this.isExpanded;
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.resizeObserver.disconnect();
    }
    get product() {
        return this.productController.item;
    }
    render() {
        const productExcerpt = this.product?.excerpt ?? null;
        if (!productExcerpt) return nothing;
        return html`${renderExpandableText({
            props: {
                isExpanded: this.isExpanded,
                isTruncated: this.isTruncated,
                truncateAfter: this.truncateAfter,
                onToggleExpand: (e)=>this.onToggleExpand(e),
                showMoreLabel: this.bindings.i18n.t('show-more'),
                showLessLabel: this.bindings.i18n.t('show-less'),
                isCollapsible: this.isCollapsible,
                textRef: this.excerptRef
            }
        })(html`<atomic-product-text field="excerpt"></atomic-product-text>`)}`;
    }
    constructor(){
        super(), this.productController = createProductContextController(this), this.isExpanded = false, this.isTruncated = false, this.excerptRef = createRef(), this.truncateAfter = '2', this.isCollapsible = false;
        this.resizeObserver = new ResizeObserver(()=>{
            if (this.excerptRef.value) this.isTruncated = this.excerptRef.value.scrollHeight > this.excerptRef.value.clientHeight;
        });
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductExcerpt.prototype, "bindings", void 0);
_ts_decorate([
    state()
], AtomicProductExcerpt.prototype, "isExpanded", void 0);
_ts_decorate([
    state()
], AtomicProductExcerpt.prototype, "isTruncated", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'truncate-after'
    }),
    _ts_metadata("design:type", "u" < typeof TruncateAfter ? Object : TruncateAfter)
], AtomicProductExcerpt.prototype, "truncateAfter", void 0);
_ts_decorate([
    property({
        type: Boolean,
        attribute: 'is-collapsible',
        converter: booleanConverter
    })
], AtomicProductExcerpt.prototype, "isCollapsible", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductExcerpt.prototype, "render", null);
AtomicProductExcerpt = _ts_decorate([
    customElement('atomic-product-excerpt'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicProductExcerpt);
export { AtomicProductExcerpt };
