/**
 * DevExtreme (esm/__internal/grids/grid_core/pager/m_pager.js)
 * Version: 22.1.13
 * Build date: Fri Feb 09 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    isDefined
} from "../../../../core/utils/type";
import {
    hasWindow
} from "../../../../core/utils/window";
import messageLocalization from "../../../../localization/message";
import Pager from "../../../../ui/pager";
import modules from "../m_modules";
var PAGER_CLASS = "pager";
var MAX_PAGES_COUNT = 10;
var getPageIndex = function(dataController) {
    return 1 + (parseInt(dataController.pageIndex()) || 0)
};
var PagerView = modules.View.inherit({
    init() {
        var dataController = this.getController("data");
        dataController.changed.add(e => {
            if (e && e.repaintChangesOnly) {
                var pager = this._pager;
                if (pager) {
                    pager.option({
                        pageIndex: getPageIndex(dataController),
                        pageSize: dataController.pageSize(),
                        pageCount: dataController.pageCount(),
                        totalCount: dataController.totalCount(),
                        hasKnownLastPage: dataController.hasKnownLastPage()
                    })
                } else {
                    this.render()
                }
            } else if (!e || "update" !== e.changeType && "updateSelection" !== e.changeType && "updateFocusedRow" !== e.changeType) {
                this._pager = null;
                this.render()
            }
        })
    },
    _renderCore() {
        var $element = this.element().addClass(this.addWidgetPrefix(PAGER_CLASS));
        var pagerOptions = this.option("pager") || {};
        var dataController = this.getController("data");
        var keyboardController = this.getController("keyboardNavigation");
        var options = {
            maxPagesCount: MAX_PAGES_COUNT,
            pageIndex: getPageIndex(dataController),
            pageCount: dataController.pageCount(),
            pageSize: dataController.pageSize(),
            showPageSizes: pagerOptions.showPageSizeSelector,
            showInfo: pagerOptions.showInfo,
            displayMode: pagerOptions.displayMode,
            pagesNavigatorVisible: pagerOptions.visible,
            showNavigationButtons: pagerOptions.showNavigationButtons,
            label: pagerOptions.label,
            pageSizes: this.getPageSizes(),
            totalCount: dataController.totalCount(),
            hasKnownLastPage: dataController.hasKnownLastPage(),
            pageIndexChanged(pageIndex) {
                if (dataController.pageIndex() !== pageIndex - 1) {
                    dataController.pageIndex(pageIndex - 1)
                }
            },
            pageSizeChanged(pageSize) {
                dataController.pageSize(pageSize)
            },
            onKeyDown: e => keyboardController && keyboardController.executeAction("onKeyDown", e),
            useLegacyKeyboardNavigation: this.option("useLegacyKeyboardNavigation"),
            useKeyboard: this.option("keyboardNavigation.enabled")
        };
        if (isDefined(pagerOptions.infoText)) {
            options.infoText = pagerOptions.infoText
        }
        if (this._pager) {
            this._pager.repaint();
            return
        }
        if (hasWindow()) {
            this._pager = this._createComponent($element, Pager, options)
        } else {
            $element.addClass("dx-pager").html('<div class="dx-pages"><div class="dx-page"></div></div>')
        }
    },
    getPager() {
        return this._pager
    },
    getPageSizes() {
        var dataController = this.getController("data");
        var pagerOptions = this.option("pager");
        var allowedPageSizes = pagerOptions && pagerOptions.allowedPageSizes;
        var pageSize = dataController.pageSize();
        if (!isDefined(this._pageSizes) || !this._pageSizes.includes(pageSize)) {
            this._pageSizes = [];
            if (pagerOptions) {
                if (Array.isArray(allowedPageSizes)) {
                    this._pageSizes = allowedPageSizes
                } else if (allowedPageSizes && pageSize > 1) {
                    this._pageSizes = [Math.floor(pageSize / 2), pageSize, 2 * pageSize]
                }
            }
        }
        return this._pageSizes
    },
    isVisible() {
        var dataController = this.getController("data");
        var pagerOptions = this.option("pager");
        var pagerVisible = pagerOptions && pagerOptions.visible;
        var scrolling = this.option("scrolling");
        if ("auto" === pagerVisible) {
            if (scrolling && ("virtual" === scrolling.mode || "infinite" === scrolling.mode)) {
                pagerVisible = false
            } else {
                pagerVisible = dataController.pageCount() > 1 || dataController.isLoaded() && !dataController.hasKnownLastPage()
            }
        }
        return pagerVisible
    },
    getHeight() {
        return this.getElementHeight()
    },
    optionChanged(args) {
        var {
            name: name
        } = args;
        var isPager = "pager" === name;
        var isPaging = "paging" === name;
        var isDataSource = "dataSource" === name;
        var isScrolling = "scrolling" === name;
        var dataController = this.getController("data");
        if (isPager || isPaging || isScrolling || isDataSource) {
            args.handled = true;
            if (dataController.skipProcessingPagingChange(args.fullName)) {
                return
            }
            if (isPager || isPaging) {
                this._pageSizes = null
            }
            if (!isDataSource) {
                this._pager = null;
                this._invalidate();
                if (hasWindow() && isPager && this.component) {
                    this.component.resize()
                }
            }
        }
    },
    dispose() {
        this._pager = null
    }
});
export var pagerModule = {
    defaultOptions: () => ({
        pager: {
            visible: "auto",
            showPageSizeSelector: false,
            allowedPageSizes: "auto",
            label: messageLocalization.format("dxPager-ariaLabel")
        }
    }),
    views: {
        pagerView: PagerView
    }
};
