/**
 * DevExtreme (renovation/pager/page-index-selector.p.js)
 * Version: 20.2.0 (build 20182-0310)
 * Build date: Tue Jun 30 2020
 *
 * Copyright (c) 2012 - 2020 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";

function _typeof(obj) {
    "@babel/helpers - typeof";
    if ("function" === typeof Symbol && "symbol" === typeof Symbol.iterator) {
        _typeof = function(obj) {
            return typeof obj
        }
    } else {
        _typeof = function(obj) {
            return obj && "function" === typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
        }
    }
    return _typeof(obj)
}
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.PageIndexSelectorProps = exports.viewFunction = exports.PAGER_BUTTON_DISABLE_CLASS = void 0;
exports.default = PageIndexSelector;
var _lightButton = require("./light-button.p");
var _lightButton2 = _interopRequireDefault(_lightButton);
var _pagesLarge = require("./pages-large.p");
var _pagesLarge2 = _interopRequireDefault(_pagesLarge);
var _pagesSmall = require("./pages-small.p");
var _pagesSmall2 = _interopRequireDefault(_pagesSmall);
var _message = require("../../localization/message");
var _message2 = _interopRequireDefault(_message);
var _preact = require("preact");
var Preact = _interopRequireWildcard(_preact);
var _hooks = require("preact/hooks");

function _getRequireWildcardCache() {
    if ("function" !== typeof WeakMap) {
        return null
    }
    var cache = new WeakMap;
    _getRequireWildcardCache = function() {
        return cache
    };
    return cache
}

function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj
    }
    if (null === obj || "object" !== _typeof(obj) && "function" !== typeof obj) {
        return {
            "default": obj
        }
    }
    var cache = _getRequireWildcardCache();
    if (cache && cache.has(obj)) {
        return cache.get(obj)
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc)
            } else {
                newObj[key] = obj[key]
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj)
    }
    return newObj
}

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        "default": obj
    }
}

function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable
            })
        }
        keys.push.apply(keys, symbols)
    }
    return keys
}

function _objectSpread(target) {
    for (var i = 1; i < arguments.length; i++) {
        var source = null != arguments[i] ? arguments[i] : {};
        if (i % 2) {
            ownKeys(Object(source), true).forEach(function(key) {
                _defineProperty(target, key, source[key])
            })
        } else {
            if (Object.getOwnPropertyDescriptors) {
                Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
            } else {
                ownKeys(Object(source)).forEach(function(key) {
                    Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))
                })
            }
        }
    }
    return target
}

function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        })
    } else {
        obj[key] = value
    }
    return obj
}

function _objectWithoutProperties(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = _objectWithoutPropertiesLoose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
        for (i = 0; i < sourceSymbolKeys.length; i++) {
            key = sourceSymbolKeys[i];
            if (excluded.indexOf(key) >= 0) {
                continue
            }
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) {
                continue
            }
            target[key] = source[key]
        }
    }
    return target
}

function _objectWithoutPropertiesLoose(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) {
            continue
        }
        target[key] = source[key]
    }
    return target
}
var PAGER_NAVIGATE_BUTTON = "dx-navigate-button";
var PAGER_PREV_BUTTON_CLASS = "dx-prev-button";
var PAGER_NEXT_BUTTON_CLASS = "dx-next-button";
var PAGER_BUTTON_DISABLE_CLASS = exports.PAGER_BUTTON_DISABLE_CLASS = "dx-button-disable";
var nextButtonClassName = "".concat(PAGER_NAVIGATE_BUTTON, " ").concat(PAGER_NEXT_BUTTON_CLASS);
var prevButtonClassName = "".concat(PAGER_NAVIGATE_BUTTON, " ").concat(PAGER_PREV_BUTTON_CLASS);
var nextButtonDisabledClassName = "".concat(PAGER_BUTTON_DISABLE_CLASS, " ").concat(PAGER_NAVIGATE_BUTTON, " ").concat(PAGER_NEXT_BUTTON_CLASS);
var prevButtonDisabledClassName = "".concat(PAGER_BUTTON_DISABLE_CLASS, " ").concat(PAGER_NAVIGATE_BUTTON, " ").concat(PAGER_PREV_BUTTON_CLASS);
var viewFunction = exports.viewFunction = function(_ref) {
    var navigateToNextPage = _ref.navigateToNextPage,
        navigateToPrevPage = _ref.navigateToPrevPage,
        nextClassName = _ref.nextClassName,
        pageIndexChange = _ref.pageIndexChange,
        prevClassName = _ref.prevClassName,
        _ref$props = _ref.props,
        isLargeDisplayMode = _ref$props.isLargeDisplayMode,
        maxPagesCount = _ref$props.maxPagesCount,
        pageCount = _ref$props.pageCount,
        pageIndex = _ref$props.pageIndex,
        pagesCountText = _ref$props.pagesCountText,
        rtlEnabled = _ref$props.rtlEnabled,
        renderNavButtons = _ref.renderNavButtons;
    return Preact.h(Preact.Fragment, null, renderNavButtons && Preact.h(_lightButton2.default, {
        className: prevClassName,
        label: "Previous page",
        onClick: navigateToPrevPage
    }), isLargeDisplayMode && Preact.h(_pagesLarge2.default, {
        maxPagesCount: maxPagesCount,
        pageCount: pageCount,
        pageIndex: pageIndex,
        pageIndexChange: pageIndexChange,
        rtlEnabled: rtlEnabled
    }), !isLargeDisplayMode && Preact.h(_pagesSmall2.default, {
        pageCount: pageCount,
        pageIndex: pageIndex,
        pageIndexChange: pageIndexChange,
        pagesCountText: pagesCountText,
        rtlEnabled: rtlEnabled
    }), renderNavButtons && Preact.h(_lightButton2.default, {
        className: nextClassName,
        label: "Next page",
        onClick: navigateToNextPage
    }))
};
var PageIndexSelectorProps = exports.PageIndexSelectorProps = {
    hasKnownLastPage: true,
    isLargeDisplayMode: true,
    maxPagesCount: 10,
    pageCount: 10,
    pageIndex: 0,
    pagesCountText: _message2.default.getFormatter("dxPager-pagesCountText")(),
    rtlEnabled: false,
    showNavigationButtons: false,
    totalCount: 0
};

function getIncrement(direction) {
    return "next" === direction ? 1 : -1
}

function PageIndexSelector(props) {
    var getNextDirection = (0, _hooks.useCallback)(function() {
        return !props.rtlEnabled ? "next" : "prev"
    }, [props.rtlEnabled]);
    var getPrevDirection = (0, _hooks.useCallback)(function() {
        return !props.rtlEnabled ? "prev" : "next"
    }, [props.rtlEnabled]);
    var canNavigateToPage = (0, _hooks.useCallback)(function(pageIndex) {
        return pageIndex >= 0 && pageIndex <= props.pageCount - 1
    }, [props.pageCount]);
    var getNextPageIndex = (0, _hooks.useCallback)(function(direction) {
        return props.pageIndex + getIncrement(direction)
    }, [props.pageIndex]);
    var canNavigateTo = (0, _hooks.useCallback)(function(direction) {
        return canNavigateToPage(getNextPageIndex(direction))
    }, [props.pageCount, props.pageIndex]);
    var navigateToPage = (0, _hooks.useCallback)(function(direction) {
        pageIndexChange(getNextPageIndex(direction))
    }, [props.pageCount, props.pageIndex]);
    var __renderNavButtons = (0, _hooks.useCallback)(function() {
        var isLargeDisplayMode = props.isLargeDisplayMode,
            showNavigationButtons = props.showNavigationButtons;
        return !isLargeDisplayMode || showNavigationButtons
    }, [props.isLargeDisplayMode, props.showNavigationButtons]);
    var __nextClassName = (0, _hooks.useCallback)(function() {
        var direction = getNextDirection();
        var canNavigate = canNavigateTo(direction);
        return canNavigate ? nextButtonClassName : nextButtonDisabledClassName
    }, [props.rtlEnabled, props.pageCount, props.pageIndex]);
    var __prevClassName = (0, _hooks.useCallback)(function() {
        var direction = getPrevDirection();
        var canNavigate = canNavigateTo(direction);
        return canNavigate ? prevButtonClassName : prevButtonDisabledClassName
    }, [props.rtlEnabled, props.pageCount, props.pageIndex]);
    var pageIndexChange = (0, _hooks.useCallback)(function(pageIndex) {
        if (canNavigateToPage(pageIndex)) {
            var _props$pageIndexChang;
            null === (_props$pageIndexChang = props.pageIndexChange) || void 0 === _props$pageIndexChang ? void 0 : _props$pageIndexChang.call(props, pageIndex)
        }
    }, [props.pageCount]);
    var navigateToNextPage = (0, _hooks.useCallback)(function() {
        navigateToPage(getNextDirection())
    }, [props.pageCount, props.pageIndex, props.rtlEnabled]);
    var navigateToPrevPage = (0, _hooks.useCallback)(function() {
        navigateToPage(getPrevDirection())
    }, [props.pageCount, props.pageIndex, props.rtlEnabled]);
    var __restAttributes = (0, _hooks.useCallback)(function() {
        var restProps = (props.hasKnownLastPage, props.isLargeDisplayMode, props.maxPagesCount, props.pageCount, props.pageIndex, props.pagesCountText, props.rtlEnabled, props.showNavigationButtons, props.totalCount, _objectWithoutProperties(props, ["hasKnownLastPage", "isLargeDisplayMode", "maxPagesCount", "pageCount", "pageIndex", "pagesCountText", "rtlEnabled", "showNavigationButtons", "totalCount"]));
        return restProps
    }, [props]);
    return viewFunction({
        props: _objectSpread({}, props),
        getNextDirection: getNextDirection,
        getPrevDirection: getPrevDirection,
        canNavigateToPage: canNavigateToPage,
        getNextPageIndex: getNextPageIndex,
        canNavigateTo: canNavigateTo,
        navigateToPage: navigateToPage,
        renderNavButtons: __renderNavButtons(),
        nextClassName: __nextClassName(),
        prevClassName: __prevClassName(),
        pageIndexChange: pageIndexChange,
        navigateToNextPage: navigateToNextPage,
        navigateToPrevPage: navigateToPrevPage,
        restAttributes: __restAttributes()
    })
}
PageIndexSelector.defaultProps = _objectSpread({}, PageIndexSelectorProps);
