"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AtomicQuickview: ()=>AtomicQuickview
});
const bueno_namespaceObject = require("@coveo/bueno");
const headless_namespaceObject = require("@coveo/headless");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const button_cjs_namespaceObject = require("../../common/button.cjs");
const validate_props_controller_cjs_namespaceObject = require("../../common/validate-props-controller/validate-props-controller.cjs");
const result_context_controller_cjs_namespaceObject = require("../result-template-component-utils/context/result-context-controller.cjs");
const bind_state_cjs_namespaceObject = require("../../../decorators/bind-state.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const accessibility_utils_cjs_namespaceObject = require("../../../utils/accessibility-utils.cjs");
const quickview_cjs_namespaceObject = require("../../../images/quickview.cjs");
var quickview_cjs_default = /*#__PURE__*/ __webpack_require__.n(quickview_cjs_namespaceObject);
require("../../common/atomic-icon/atomic-icon.cjs");
require("../atomic-quickview-modal/atomic-quickview-modal.cjs");
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 AtomicQuickview extends external_lit_namespaceObject.LitElement {
    initialize() {
        if (this.resultContext.item) {
            const item = this.resultContext.item;
            if ('result' in item) this.result = item.result;
            else this.result = item;
        }
        this.buttonFocusTarget = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        this.quickview = (0, headless_namespaceObject.buildQuickview)(this.bindings.engine, {
            options: {
                result: this.result
            }
        });
    }
    connectedCallback() {
        super.connectedCallback();
        document.body.addEventListener('atomic/quickview/next', this.nextQuickviewHandler);
        document.body.addEventListener('atomic/quickview/previous', this.previousQuickviewHandler);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        document.body.removeEventListener('atomic/quickview/next', this.nextQuickviewHandler);
        document.body.removeEventListener('atomic/quickview/previous', this.previousQuickviewHandler);
    }
    addQuickviewModalIfNeeded() {
        if (this.quickviewModalRef || !this.bindings?.interfaceElement) return;
        const quickviewModal = this.bindings.interfaceElement.querySelector('atomic-quickview-modal');
        if (quickviewModal) {
            this.quickviewModalRef = quickviewModal;
            return;
        }
        this.quickviewModalRef = document.createElement('atomic-quickview-modal');
        this.quickviewModalRef.setAttribute('sandbox', this.sandbox);
        this.bindings.interfaceElement.appendChild(this.quickviewModalRef);
    }
    updateModalContent() {
        if (this.quickviewModalRef && this.quickview.state.content) {
            this.quickviewModalRef.content = this.quickview.state.content;
            this.quickviewModalRef.result = this.result;
            this.quickviewModalRef.total = this.quickviewState.totalResults;
            this.quickviewModalRef.current = this.quickviewState.currentResult;
            this.quickviewModalRef.modalCloseCallback = ()=>this.buttonFocusTarget.focus();
            this.ariaLiveRegion.message = this.quickviewState.isLoading ? this.bindings.i18n.t('quickview-loading') : this.bindings.i18n.t('quickview-loaded', {
                first: this.quickviewState.currentResult,
                last: this.quickviewState.totalResults,
                title: this.result.title
            });
        }
    }
    onClick(event) {
        event?.stopPropagation();
        this.quickview.fetchResultContent();
    }
    updated() {
        this.addQuickviewModalIfNeeded();
        if (this.quickviewState) this.updateModalContent();
        if (this.buttonRef.value && this.buttonFocusTarget) this.buttonFocusTarget.setTarget(this.buttonRef.value);
    }
    render() {
        return (0, when_js_namespaceObject.when)(this.quickviewState?.resultHasPreview, ()=>(0, button_cjs_namespaceObject.renderButton)({
                props: {
                    part: 'button',
                    title: this.bindings.i18n.t('quickview'),
                    style: 'outline-primary',
                    class: 'p-2',
                    onClick: (event)=>this.onClick(event),
                    ref: this.buttonRef
                }
            })((0, external_lit_namespaceObject.html)`<atomic-icon
            part="icon"
            class="flex w-5 justify-center"
            icon=${quickview_cjs_default()}
          ></atomic-icon>`), ()=>external_lit_namespaceObject.nothing);
    }
    constructor(){
        super(), this.sandbox = 'allow-popups allow-top-navigation allow-same-origin', this.resultContext = (0, result_context_controller_cjs_namespaceObject.createResultContextController)(this), this.ariaLiveRegion = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'quickview'), this.buttonRef = (0, ref_js_namespaceObject.createRef)(), this.nextQuickviewHandler = (evt)=>{
            evt.stopImmediatePropagation();
            this.quickview.next();
        }, this.previousQuickviewHandler = (evt)=>{
            evt.stopImmediatePropagation();
            this.quickview.previous();
        };
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                sandbox: this.sandbox
            }), AtomicQuickview.propsSchema);
    }
}
AtomicQuickview.propsSchema = new bueno_namespaceObject.Schema({
    sandbox: new bueno_namespaceObject.StringValue({
        required: true,
        regex: /allow-same-origin/
    })
});
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String
    })
], AtomicQuickview.prototype, "sandbox", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicQuickview.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicQuickview.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicQuickview.prototype, "result", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('quickview'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof QuickviewState ? Object : QuickviewState)
], AtomicQuickview.prototype, "quickviewState", void 0);
AtomicQuickview = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-quickview'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicQuickview);
exports.AtomicQuickview = __webpack_exports__.AtomicQuickview;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicQuickview"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
