import { Schema, StringValue } from "@coveo/bueno";
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { ref } from "lit/directives/ref.js";
import { unsafeHTML } from "lit/directives/unsafe-html.js";
import { resultComponentClass } from "../../common/item-list/item-list-common.js";
import { CustomRenderController } from "../../common/layout/custom-render-controller.js";
import { ItemLayoutController } from "../../common/layout/item-layout-controller.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-controller.js";
import { booleanConverter } from "../../../converters/boolean-converter.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { ChildrenUpdateCompleteMixin } from "../../../mixins/children-update-complete-mixin.js";
import { parentNodeToString } from "../../../utils/dom-utils.js";
import "../atomic-result-text/atomic-result-text.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 AtomicResult extends ChildrenUpdateCompleteMixin(LitElement) {
    async connectedCallback() {
        super.connectedCallback();
        new CustomRenderController(this, {
            renderingFunction: ()=>this.renderingFunction,
            itemData: ()=>this.result,
            rootElementRef: ()=>this.resultRootRef,
            linkContainerRef: ()=>this.linkContainerRef,
            onRenderComplete: (element, output)=>{
                this.itemLayoutController.applyLayoutClassesToElement(element, output);
            }
        });
        this.itemLayoutController = new ItemLayoutController(this, {
            elementPrefix: 'atomic-result',
            renderingFunction: ()=>this.renderingFunction,
            content: ()=>this.content,
            layoutConfig: ()=>({
                    display: this.display,
                    density: this.density,
                    imageSize: this.imageSize
                }),
            itemClasses: ()=>this.classes
        });
        this.addEventListener('atomic/resolveResult', this.resolveResult);
        this.addEventListener('atomic/resolveInteractiveResult', this.resolveInteractiveResult);
        this.addEventListener('atomic/resolveStopPropagation', this.resolveStopPropagation);
        this.addEventListener('atomic/resolveResultDisplayConfig', this.resolveResultDisplayConfig);
        this.addEventListener('click', this.handleClick);
        await this.getUpdateComplete();
        this.classList.add('hydrated');
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('atomic/resolveResult', this.resolveResult);
        this.removeEventListener('atomic/resolveInteractiveResult', this.resolveInteractiveResult);
        this.removeEventListener('atomic/resolveStopPropagation', this.resolveStopPropagation);
        this.removeEventListener('atomic/resolveResultDisplayConfig', this.resolveResultDisplayConfig);
        this.removeEventListener('click', this.handleClick);
    }
    getContentHTML() {
        if (!this.content) {
            console.warn('atomic-result: content property is undefined. Cannot get content HTML.', this);
            return '';
        }
        return parentNodeToString(this.content);
    }
    getLinkHTML() {
        return parentNodeToString(this.linkContent ?? new HTMLElement());
    }
    render() {
        if (void 0 !== this.renderingFunction) return html`
        <div class=${resultComponentClass}>
          <div
            class="result-root"
            ${ref((el)=>{
            this.resultRootRef = el;
        })}
          ></div>
          <div
            class="link-container"
            ${ref((el)=>{
            this.linkContainerRef = el;
        })}
          ></div>
        </div>
      `;
        if (!this.itemLayoutController.getLayout()) return html`<div class=${resultComponentClass}></div>`;
        return html`
      <div class=${resultComponentClass}>
        <div
          class="result-root ${this.itemLayoutController.getCombinedClasses().join(' ')}"
        >
          ${unsafeHTML(this.getContentHTML())}
        </div>
        <div class="link-container">${unsafeHTML(this.getLinkHTML())}</div>
      </div>
    `;
    }
    firstUpdated(_changedProperties) {
        if (this.loadingFlag && this.store) this.store.unsetLoadingFlag(this.loadingFlag);
    }
    constructor(){
        super(), this.linkContent = new DocumentFragment(), this.display = 'list', this.density = 'normal', this.imageSize = 'icon', this.classes = '', this.resolveResult = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            event.detail(this.result);
        }, this.resolveInteractiveResult = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            if (this.interactiveResult) event.detail(this.interactiveResult);
        }, this.resolveStopPropagation = (event)=>{
            event.detail(this.stopPropagation);
        }, this.resolveResultDisplayConfig = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            event.detail({
                density: this.density,
                imageSize: this.imageSize
            });
        }, this.handleClick = (event)=>{
            if (this.stopPropagation) event.stopPropagation();
            if ('grid' === this.display) this.clickLinkContainer();
        }, this.clickLinkContainer = ()=>{
            this.shadowRoot?.querySelector('.link-container > atomic-result-link a:not([slot])')?.click();
        };
        new ValidatePropsController(this, ()=>({
                display: this.display,
                density: this.density,
                imageSize: this.imageSize
            }), AtomicResult.propsSchema);
    }
}
AtomicResult.styles = css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */@layer properties{@supports (((-webkit-hyphens:none)) and (not (margin-trim:inline))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,::backdrop,:after,:before{--tw-font-weight:initial}}}@layer components{:host :host{font-family:var(--atomic-font-family);--tw-font-weight:var(--atomic-font-normal);font-weight:var(--atomic-font-normal)}:host .result-root.with-sections{display:grid;justify-items:stretch}@media (min-width:1024px){:host .result-root.with-sections.display-list.image-large,:host .result-root.with-sections.display-list.image-small{grid-template-areas:"badges badges.actions""visual title title title""visual title-metadata title-metadata title-metadata""visual emphasized emphasized emphasized""visual excerpt excerpt excerpt""visual bottom-metadata bottom-metadata bottom-metadata""visual children children children";grid-template-columns:minmax(0,min-content) auto 1fr auto;grid-template-rows:repeat(6,auto) minmax(0,1fr)}:host .result-root.with-sections.display-list.image-icon{grid-template-areas:"badges badges.actions""visual title title title""visual title-metadata title-metadata title-metadata""visual emphasized emphasized emphasized""visual excerpt excerpt excerpt""visual bottom-metadata bottom-metadata bottom-metadata""visual children children children";grid-template-columns:minmax(0,min-content) auto 1fr auto;grid-template-rows:repeat(7,minmax(0,min-content))}:host .result-root.with-sections.display-list.image-none{grid-template-areas:"badges.actions""title title title""title-metadata title-metadata title-metadata""emphasized emphasized emphasized""excerpt excerpt excerpt""bottom-metadata bottom-metadata bottom-metadata""children children children";grid-template-columns:auto 1fr auto;grid-template-rows:repeat(6,auto)}}:host .result-root.with-sections.display-table.image-large,:host .result-root.with-sections.display-table.image-small{grid-template-areas:"badges badges.actions""visual title title title""visual title-metadata title-metadata title-metadata""visual emphasized emphasized emphasized""visual excerpt excerpt excerpt""visual bottom-metadata bottom-metadata bottom-metadata""visual children children children";grid-template-columns:minmax(0,min-content) auto 1fr auto;grid-template-rows:repeat(6,auto) minmax(0,1fr)}:host .result-root.with-sections.display-table.image-icon{grid-template-areas:"badges badges.actions""visual title title title""visual title-metadata title-metadata title-metadata""visual emphasized emphasized emphasized""visual excerpt excerpt excerpt""visual bottom-metadata bottom-metadata bottom-metadata""visual children children children";grid-template-columns:minmax(0,min-content) auto 1fr auto;grid-template-rows:repeat(7,minmax(0,min-content))}:host .result-root.with-sections.display-table.image-none{grid-template-areas:"badges.actions""title title title""title-metadata title-metadata title-metadata""emphasized emphasized emphasized""excerpt excerpt excerpt""bottom-metadata bottom-metadata bottom-metadata""children children children";grid-template-columns:auto 1fr auto;grid-template-rows:repeat(6,auto)}@media not all and (min-width:1024px){:host .result-root.with-sections.display-list.image-large{grid-template-areas:"visual""badges""title""title-metadata""emphasized""excerpt""bottom-metadata""actions""children";grid-template-columns:100%;grid-template-rows:repeat(8,auto)}:host .result-root.with-sections.display-list.image-small{grid-template-areas:"badges badges""visual title""visual title-metadata""visual emphasized""visual.""excerpt excerpt""bottom-metadata bottom-metadata""actions actions""children children";grid-template-columns:auto minmax(0,1fr);grid-template-rows:repeat(4,auto) minmax(0,1fr) repeat(3,auto)}:host .result-root.with-sections.display-list.image-icon{grid-template-areas:"badges badges""visual title""visual title-metadata""visual emphasized""visual excerpt""visual bottom-metadata""visual actions""visual children";grid-template-columns:auto minmax(0,1fr);grid-template-rows:repeat(7,auto) 1fr}:host .result-root.with-sections.display-list.image-none{grid-template-areas:"badges""title""title-metadata""emphasized""excerpt""bottom-metadata""actions""children";grid-template-columns:100%;grid-template-rows:repeat(7,auto)}}:host .result-root.with-sections.display-grid{grid-template-areas:"badges""visual""title""title-metadata""emphasized""excerpt""children""bottom-metadata""actions";grid-template-columns:100%;grid-template-rows:repeat(9,auto)}:host .result-root.with-sections.display-grid a,:host .result-root.with-sections.display-grid button{position:relative}@media not all and (min-width:1024px){:host .result-root.with-sections.display-grid.image-large.image-large{grid-template-areas:"visual""badges""title""title-metadata""emphasized""excerpt""bottom-metadata""actions""children";grid-template-columns:100%;grid-template-rows:repeat(8,auto)}:host .result-root.with-sections.display-grid.image-large.image-small{grid-template-areas:"badges badges""visual title""visual title-metadata""visual emphasized""visual.""excerpt excerpt""bottom-metadata bottom-metadata""actions actions""children children";grid-template-columns:auto minmax(0,1fr);grid-template-rows:repeat(4,auto) minmax(0,1fr) repeat(3,auto)}:host .result-root.with-sections.display-grid.image-large.image-icon{grid-template-areas:"badges badges""visual title""visual title-metadata""visual emphasized""visual excerpt""visual bottom-metadata""visual actions""visual children";grid-template-columns:auto minmax(0,1fr);grid-template-rows:repeat(7,auto) 1fr}:host .result-root.with-sections.display-grid.image-large.image-none{grid-template-areas:"badges""title""title-metadata""emphasized""excerpt""bottom-metadata""actions""children";grid-template-columns:100%;grid-template-rows:repeat(7,auto)}}:host .link-container,:host .result-root atomic-table-element{display:none}}@property --tw-font-weight{syntax:"*";inherits:false}`;
AtomicResult.propsSchema = new Schema({
    display: new StringValue({
        constrainTo: [
            'grid',
            'list',
            'table'
        ]
    }),
    density: new StringValue({
        constrainTo: [
            'normal',
            'comfortable',
            'compact'
        ]
    }),
    imageSize: new StringValue({
        constrainTo: [
            'small',
            'large',
            'icon',
            'none'
        ]
    })
});
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResult.prototype, "error", void 0);
_ts_decorate([
    property({
        attribute: 'stop-propagation',
        type: Boolean,
        converter: booleanConverter,
        reflect: true
    }),
    _ts_metadata("design:type", Boolean)
], AtomicResult.prototype, "stopPropagation", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", Object)
], AtomicResult.prototype, "result", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'interactive-result'
    }),
    _ts_metadata("design:type", "u" < typeof InteractiveResult ? Object : InteractiveResult)
], AtomicResult.prototype, "interactiveResult", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof SearchStore ? Object : SearchStore)
], AtomicResult.prototype, "store", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof ParentNode ? Object : ParentNode)
], AtomicResult.prototype, "content", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'link-content'
    }),
    _ts_metadata("design:type", "u" < typeof ParentNode ? Object : ParentNode)
], AtomicResult.prototype, "linkContent", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayLayout ? Object : ItemDisplayLayout)
], AtomicResult.prototype, "display", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayDensity ? Object : ItemDisplayDensity)
], AtomicResult.prototype, "density", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String,
        attribute: 'image-size'
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayImageSize ? Object : ItemDisplayImageSize)
], AtomicResult.prototype, "imageSize", void 0);
_ts_decorate([
    property({
        type: String
    })
], AtomicResult.prototype, "classes", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'loading-flag'
    }),
    _ts_metadata("design:type", String)
], AtomicResult.prototype, "loadingFlag", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: 'rendering-function'
    }),
    _ts_metadata("design:type", "u" < typeof ItemRenderingFunction ? Object : ItemRenderingFunction)
], AtomicResult.prototype, "renderingFunction", void 0);
AtomicResult = _ts_decorate([
    customElement('atomic-result'),
    withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResult);
export { AtomicResult };
