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