/**
 * DevExtreme (cjs/__internal/ui/date_box/m_date_view_roller.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = void 0;
var _fx = _interopRequireDefault(require("../../../animation/fx"));
var _translator = require("../../../animation/translator");
var _component_registrator = _interopRequireDefault(require("../../../core/component_registrator"));
var _devices = _interopRequireDefault(require("../../../core/devices"));
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _extend = require("../../../core/utils/extend");
var _iterator = require("../../../core/utils/iterator");
var _size = require("../../../core/utils/size");
var _click = require("../../../events/click");
var _events_engine = _interopRequireDefault(require("../../../events/core/events_engine"));
var _index = require("../../../events/utils/index");
var _convert_location = require("../../../renovation/ui/scroll_view/utils/convert_location");
var _m_scrollable = _interopRequireDefault(require("../scroll_view/m_scrollable"));

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

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const DATEVIEW_ROLLER_CLASS = "dx-dateviewroller";
const DATEVIEW_ROLLER_ACTIVE_CLASS = "dx-state-active";
const DATEVIEW_ROLLER_CURRENT_CLASS = "dx-dateviewroller-current";
const DATEVIEW_ROLLER_ITEM_CLASS = "dx-dateview-item";
const DATEVIEW_ROLLER_ITEM_SELECTED_CLASS = "dx-dateview-item-selected";
const DATEVIEW_ROLLER_ITEM_SELECTED_FRAME_CLASS = "dx-dateview-item-selected-frame";
const DATEVIEW_ROLLER_ITEM_SELECTED_BORDER_CLASS = "dx-dateview-item-selected-border";
let DateViewRoller = function(_Scrollable) {
    _inheritsLoose(DateViewRoller, _Scrollable);

    function DateViewRoller() {
        return _Scrollable.apply(this, arguments) || this
    }
    var _proto = DateViewRoller.prototype;
    _proto._getDefaultOptions = function() {
        return (0, _extend.extend)(_Scrollable.prototype._getDefaultOptions.call(this), {
            showScrollbar: "never",
            useNative: false,
            selectedIndex: 0,
            bounceEnabled: false,
            items: [],
            showOnClick: false,
            onClick: null,
            onSelectedIndexChanged: null,
            scrollByContent: true
        })
    };
    _proto._init = function() {
        _Scrollable.prototype._init.call(this);
        this.option("onVisibilityChange", this._visibilityChangedHandler.bind(this));
        this.option("onEnd", this._endActionHandler.bind(this))
    };
    _proto._render = function() {
        _Scrollable.prototype._render.call(this);
        this._renderSelectedItemFrame();
        this.$element().addClass("dx-dateviewroller");
        this._renderContainerClick();
        this._renderItems();
        this._renderSelectedValue();
        this._renderItemsClick();
        this._renderWheelEvent();
        this._renderSelectedIndexChanged()
    };
    _proto._renderSelectedIndexChanged = function() {
        this._selectedIndexChanged = this._createActionByOption("onSelectedIndexChanged")
    };
    _proto._renderWheelEvent = function() {
        _events_engine.default.on((0, _renderer.default)(this.container()), "dxmousewheel", e => {
            this._isWheelScrolled = true
        })
    };
    _proto._renderContainerClick = function() {
        if (!this.option("showOnClick")) {
            return
        }
        const eventName = (0, _index.addNamespace)(_click.name, this.NAME);
        const clickAction = this._createActionByOption("onClick");
        _events_engine.default.off((0, _renderer.default)(this.container()), eventName);
        _events_engine.default.on((0, _renderer.default)(this.container()), eventName, e => {
            clickAction({
                event: e
            })
        })
    };
    _proto._renderItems = function() {
        const items = this.option("items") || [];
        let $items = (0, _renderer.default)();
        (0, _renderer.default)(this.content()).empty();
        items.forEach(item => {
            $items = $items.add((0, _renderer.default)("<div>").addClass("dx-dateview-item").append(item))
        });
        (0, _renderer.default)(this.content()).append($items);
        this._$items = $items;
        this.update()
    };
    _proto._renderSelectedItemFrame = function() {
        (0, _renderer.default)("<div>").addClass("dx-dateview-item-selected-frame").append((0, _renderer.default)("<div>").addClass("dx-dateview-item-selected-border")).appendTo((0, _renderer.default)(this.container()))
    };
    _proto._renderSelectedValue = function(selectedIndex) {
        const index = this._fitIndex(null !== selectedIndex && void 0 !== selectedIndex ? selectedIndex : this.option("selectedIndex"));
        this._moveTo({
            top: this._getItemPosition(index)
        });
        this._renderActiveStateItem()
    };
    _proto._fitIndex = function(index) {
        const items = this.option("items") || [];
        const itemCount = items.length;
        if (index >= itemCount) {
            return itemCount - 1
        }
        if (index < 0) {
            return 0
        }
        return index
    };
    _proto._getItemPosition = function(index) {
        return Math.round(this._itemHeight() * index)
    };
    _proto._renderItemsClick = function() {
        const itemSelector = this._getItemSelector();
        const eventName = (0, _index.addNamespace)(_click.name, this.NAME);
        _events_engine.default.off(this.$element(), eventName, itemSelector);
        _events_engine.default.on(this.$element(), eventName, itemSelector, this._itemClickHandler.bind(this))
    };
    _proto._getItemSelector = function() {
        return ".".concat("dx-dateview-item")
    };
    _proto._itemClickHandler = function(e) {
        this.option("selectedIndex", this._itemElementIndex(e.currentTarget))
    };
    _proto._itemElementIndex = function(itemElement) {
        return this._itemElements().index(itemElement)
    };
    _proto._itemElements = function() {
        return this.$element().find(this._getItemSelector())
    };
    _proto._renderActiveStateItem = function() {
        const selectedIndex = this.option("selectedIndex");
        (0, _iterator.each)(this._$items, (function(index) {
            (0, _renderer.default)(this).toggleClass("dx-dateview-item-selected", selectedIndex === index)
        }))
    };
    _proto._shouldScrollToNeighborItem = function() {
        return "desktop" === _devices.default.real().deviceType && this._isWheelScrolled
    };
    _proto._moveTo = function(targetLocation) {
        const {
            top: top,
            left: left
        } = (0, _convert_location.convertToLocation)(targetLocation);
        const location = this.scrollOffset();
        const delta = {
            x: location.left - left,
            y: location.top - top
        };
        if (this._isVisible() && (delta.x || delta.y)) {
            this._prepareDirections(true);
            if (this._animation && !this._shouldScrollToNeighborItem()) {
                const that = this;
                _fx.default.stop((0, _renderer.default)(this.content()));
                _fx.default.animate((0, _renderer.default)(this.content()), {
                    duration: 200,
                    type: "slide",
                    to: {
                        top: Math.floor(delta.y)
                    },
                    complete() {
                        (0, _translator.resetPosition)((0, _renderer.default)(that.content()));
                        that.handleMove({
                            delta: delta
                        })
                    }
                });
                delete this._animation
            } else {
                this.handleMove({
                    delta: delta
                })
            }
        }
    };
    _proto._validate = function(e) {
        return this._moveIsAllowed(e)
    };
    _proto._fitSelectedIndexInRange = function(index) {
        const itemsCount = this.option("items").length;
        return Math.max(Math.min(index, itemsCount - 1), 0)
    };
    _proto._isInNullNeighborhood = function(x) {
        return -.1 <= x && x <= .1
    };
    _proto._getSelectedIndexAfterScroll = function(currentSelectedIndex) {
        const locationTop = this.scrollOffset().top;
        const currentSelectedIndexPosition = currentSelectedIndex * this._itemHeight();
        const dy = locationTop - currentSelectedIndexPosition;
        if (this._isInNullNeighborhood(dy)) {
            return currentSelectedIndex
        }
        const direction = dy > 0 ? 1 : -1;
        const newSelectedIndex = this._fitSelectedIndexInRange(currentSelectedIndex + direction);
        return newSelectedIndex
    };
    _proto._getNewSelectedIndex = function(currentSelectedIndex) {
        if (this._shouldScrollToNeighborItem()) {
            return this._getSelectedIndexAfterScroll(currentSelectedIndex)
        }
        this._animation = true;
        const ratio = this.scrollOffset().top / this._itemHeight();
        return Math.round(ratio)
    };
    _proto._endActionHandler = function() {
        const currentSelectedIndex = this.option("selectedIndex");
        const newSelectedIndex = this._getNewSelectedIndex(currentSelectedIndex);
        if (newSelectedIndex === currentSelectedIndex) {
            this._renderSelectedValue(newSelectedIndex)
        } else {
            this.option("selectedIndex", newSelectedIndex)
        }
        this._isWheelScrolled = false
    };
    _proto._itemHeight = function() {
        const $item = this._$items.first();
        return (0, _size.getHeight)($item)
    };
    _proto._toggleActive = function(state) {
        this.$element().toggleClass("dx-state-active", state)
    };
    _proto._isVisible = function() {
        return (0, _renderer.default)(this.container()).is(":visible")
    };
    _proto._fireSelectedIndexChanged = function(value, previousValue) {
        this._selectedIndexChanged({
            value: value,
            previousValue: previousValue,
            event: void 0
        })
    };
    _proto._visibilityChanged = function(visible) {
        _Scrollable.prototype._visibilityChanged.call(this, visible);
        this._visibilityChangedHandler(visible)
    };
    _proto._visibilityChangedHandler = function(visible) {
        if (visible) {
            this._visibilityTimer = setTimeout(() => {
                this._renderSelectedValue(this.option("selectedIndex"))
            })
        }
        this.toggleActiveState(false)
    };
    _proto.toggleActiveState = function(state) {
        this.$element().toggleClass("dx-dateviewroller-current", state)
    };
    _proto._refreshSelectedIndex = function() {
        const selectedIndex = this.option("selectedIndex");
        const fitIndex = this._fitIndex(selectedIndex);
        if (fitIndex === selectedIndex) {
            this._renderActiveStateItem()
        } else {
            this.option("selectedIndex", fitIndex)
        }
    };
    _proto._optionChanged = function(args) {
        switch (args.name) {
            case "selectedIndex":
                this._fireSelectedIndexChanged(args.value, args.previousValue);
                this._renderSelectedValue(args.value);
                break;
            case "items":
                this._renderItems();
                this._refreshSelectedIndex();
                break;
            case "onClick":
            case "showOnClick":
                this._renderContainerClick();
                break;
            case "onSelectedIndexChanged":
                this._renderSelectedIndexChanged();
                break;
            default:
                _Scrollable.prototype._optionChanged.call(this, args)
        }
    };
    _proto._dispose = function() {
        clearTimeout(this._visibilityTimer);
        _Scrollable.prototype._dispose.call(this)
    };
    return DateViewRoller
}(_m_scrollable.default);
(0, _component_registrator.default)("dxDateViewRoller", DateViewRoller);
var _default = DateViewRoller;
exports.default = _default;
