"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__, {
    AtomicInsightPager: ()=>AtomicInsightPager
});
const bueno_namespaceObject = require("@coveo/bueno");
const insight_namespaceObject = require("@coveo/headless/insight");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const keyed_js_namespaceObject = require("lit/directives/keyed.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const pager_buttons_cjs_namespaceObject = require("../../common/pager/pager-buttons.cjs");
const pager_navigation_cjs_namespaceObject = require("../../common/pager/pager-navigation.cjs");
const pager_utils_cjs_namespaceObject = require("../../common/pager/pager-utils.cjs");
const validate_props_controller_cjs_namespaceObject = require("../../common/validate-props-controller/validate-props-controller.cjs");
const bind_state_cjs_namespaceObject = require("../../../decorators/bind-state.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const accessibility_utils_cjs_namespaceObject = require("../../../utils/accessibility-utils.cjs");
const event_utils_cjs_namespaceObject = require("../../../utils/event-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const arrow_left_rounded_cjs_namespaceObject = require("../../../images/arrow-left-rounded.cjs");
var arrow_left_rounded_cjs_default = /*#__PURE__*/ __webpack_require__.n(arrow_left_rounded_cjs_namespaceObject);
const arrow_right_rounded_cjs_namespaceObject = require("../../../images/arrow-right-rounded.cjs");
var arrow_right_rounded_cjs_default = /*#__PURE__*/ __webpack_require__.n(arrow_right_rounded_cjs_namespaceObject);
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 AtomicInsightPager extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.initFocusTargets();
        this.searchStatus = (0, insight_namespaceObject.buildSearchStatus)(this.bindings.engine);
        this.pager = (0, insight_namespaceObject.buildPager)(this.bindings.engine, {
            options: {
                numberOfPages: this.numberOfPages
            }
        });
    }
    render() {
        const pagesRange = (0, pager_utils_cjs_namespaceObject.getCurrentPagesRange)(this.pagerState.currentPage - 1, this.numberOfPages, this.pagerState.maxPage);
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(this.pagerState.maxPage > 1, ()=>(0, external_lit_namespaceObject.html)`
        ${(0, pager_navigation_cjs_namespaceObject.renderPagerNavigation)({
                props: {
                    i18n: this.bindings.i18n
                }
            })((0, external_lit_namespaceObject.html)`
          ${(0, pager_buttons_cjs_namespaceObject.renderPagerPreviousButton)({
                props: {
                    icon: arrow_left_rounded_cjs_default(),
                    disabled: !this.pagerState.hasPreviousPage,
                    i18n: this.bindings.i18n,
                    onClick: async ()=>{
                        this.pager.previousPage();
                        await this.focusOnFirstResultAndScrollToTop();
                    },
                    ref: (el)=>this.previousButton.setTarget(el)
                }
            })}
          ${(0, pager_buttons_cjs_namespaceObject.renderPageButtons)({
                props: {
                    i18n: this.bindings.i18n
                }
            })((0, external_lit_namespaceObject.html)`
            ${pagesRange.map((pageNumber, index)=>(0, keyed_js_namespaceObject.keyed)(`page-${pageNumber}-${index}`, (0, pager_buttons_cjs_namespaceObject.renderPagerPageButton)({
                    props: {
                        isSelected: pageNumber === this.pager.state.currentPage - 1,
                        ariaLabel: this.bindings.i18n.t('page-number', {
                            pageNumber: pageNumber + 1
                        }),
                        onChecked: async ()=>{
                            this.pager.selectPage(pageNumber + 1);
                            await this.focusOnFirstResultAndScrollToTop();
                        },
                        page: pageNumber,
                        groupName: this.radioGroupName,
                        text: (pageNumber + 1).toLocaleString(this.bindings.i18n.language),
                        onFocusCallback: this.handleFocus
                    }
                })))}
          `)}
          ${(0, pager_buttons_cjs_namespaceObject.renderPagerNextButton)({
                props: {
                    icon: arrow_right_rounded_cjs_default(),
                    disabled: !this.pagerState.hasNextPage,
                    i18n: this.bindings.i18n,
                    onClick: async ()=>{
                        this.pager.nextPage();
                        await this.focusOnFirstResultAndScrollToTop();
                    },
                    ref: (el)=>this.nextButton.setTarget(el)
                }
            })}
        `)}
      `)}`;
    }
    initFocusTargets() {
        if (!this.previousButton) this.previousButton = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        if (!this.nextButton) this.nextButton = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
    }
    async focusOnFirstResultAndScrollToTop() {
        await this.bindings.store.state.resultList?.focusOnFirstResultAfterNextSearch();
        this.dispatchEvent((0, event_utils_cjs_namespaceObject.buildCustomEvent)('atomic/scrollToTop'));
        this.announcePageLoaded();
    }
    announcePageLoaded() {
        this.ariaMessage.message = this.bindings.i18n.t('pager-page-loaded', {
            pageNumber: this.pagerState.currentPage
        });
    }
    constructor(){
        super(), this.numberOfPages = 5, this.ariaMessage = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'atomic-insight-pager'), this.radioGroupName = (0, utils_cjs_namespaceObject.randomID)('atomic-insight-pager-'), this.handleFocus = async (elements, currentFocus, newFocus)=>{
            const currentIndex = elements.indexOf(currentFocus);
            const newIndex = elements.indexOf(newFocus);
            if (currentIndex === elements.length - 1 && 0 === newIndex) await this.nextButton.focus();
            else if (0 === currentIndex && newIndex === elements.length - 1) await this.previousButton.focus();
            else newFocus.focus();
        };
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                numberOfPages: this.numberOfPages
            }), new bueno_namespaceObject.Schema({
            numberOfPages: new bueno_namespaceObject.NumberValue({
                min: 0
            })
        }));
    }
}
AtomicInsightPager.styles = (0, external_lit_namespaceObject.css)`
    @reference '../../../utils/tailwind.global.tw.css';

    [part='page-button'] {
      @apply bg-transparent;
    }

    :host {
      @apply bg-neutral-light box-content flex h-full items-center justify-center px-6 py-4;
    }
  `;
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightPager.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightPager.prototype, "error", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('pager'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof InsightPagerState ? Object : InsightPagerState)
], AtomicInsightPager.prototype, "pagerState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('searchStatus'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof InsightSearchStatusState ? Object : InsightSearchStatusState)
], AtomicInsightPager.prototype, "searchStatusState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true,
        attribute: 'number-of-pages',
        type: Number
    }),
    _ts_metadata("design:type", Number)
], AtomicInsightPager.prototype, "numberOfPages", void 0);
_ts_decorate([
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightPager.prototype, "render", null);
AtomicInsightPager = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-insight-pager'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicInsightPager);
exports.AtomicInsightPager = __webpack_exports__.AtomicInsightPager;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicInsightPager"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
