/**
 * DevExtreme (esm/viz/translators/category_translator.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 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 {
    adjust
} from "../../core/utils/math";
const round = Math.round;

function getValue(value) {
    return value
}
export default {
    translate: function(category, directionOffset) {
        const canvasOptions = this._canvasOptions;
        const categoryIndex = this._categoriesToPoints[null === category || void 0 === category ? void 0 : category.valueOf()];
        const specialValue = this.translateSpecialCase(category);
        const startPointIndex = canvasOptions.startPointIndex || 0;
        const stickInterval = this._options.stick ? 0 : .5;
        if (isDefined(specialValue)) {
            return round(specialValue)
        }
        if (!categoryIndex && 0 !== categoryIndex) {
            return null
        }
        directionOffset = directionOffset || 0;
        const stickDelta = categoryIndex + stickInterval - startPointIndex + .5 * directionOffset;
        return round(this._calculateProjection(canvasOptions.interval * stickDelta))
    },
    getInterval: function() {
        return this._canvasOptions.interval
    },
    getEventScale: function(zoomEvent) {
        const scale = zoomEvent.deltaScale || 1;
        return 1 - (1 - scale) / (.75 + this.visibleCategories.length / this._categories.length)
    },
    zoom: function(translate, scale) {
        const categories = this._categories;
        const canvasOptions = this._canvasOptions;
        const stick = this._options.stick;
        const invert = canvasOptions.invert;
        const interval = canvasOptions.interval * scale;
        const translateCategories = translate / interval;
        const visibleCount = (this.visibleCategories || []).length;
        let startCategoryIndex = parseInt((canvasOptions.startPointIndex || 0) + translateCategories + .5);
        const categoriesLength = parseInt(adjust(canvasOptions.canvasLength / interval) + (stick ? 1 : 0)) || 1;
        let endCategoryIndex;
        if (invert) {
            startCategoryIndex = parseInt((canvasOptions.startPointIndex || 0) + visibleCount - translateCategories + .5) - categoriesLength
        }
        if (startCategoryIndex < 0) {
            startCategoryIndex = 0
        }
        endCategoryIndex = startCategoryIndex + categoriesLength;
        if (endCategoryIndex > categories.length) {
            endCategoryIndex = categories.length;
            startCategoryIndex = endCategoryIndex - categoriesLength;
            if (startCategoryIndex < 0) {
                startCategoryIndex = 0
            }
        }
        const newVisibleCategories = categories.slice(parseInt(startCategoryIndex), parseInt(endCategoryIndex));
        const newInterval = this._getDiscreteInterval(newVisibleCategories.length, canvasOptions);
        scale = newInterval / canvasOptions.interval;
        translate = this.translate(!invert ? newVisibleCategories[0] : newVisibleCategories[newVisibleCategories.length - 1]) * scale - (canvasOptions.startPoint + (stick ? 0 : newInterval / 2));
        return {
            min: newVisibleCategories[0],
            max: newVisibleCategories[newVisibleCategories.length - 1],
            translate: translate,
            scale: scale
        }
    },
    getMinScale: function(zoom) {
        const canvasOptions = this._canvasOptions;
        let categoriesLength = (this.visibleCategories || this._categories).length;
        categoriesLength += (parseInt(.1 * categoriesLength) || 1) * (zoom ? -2 : 2);
        return canvasOptions.canvasLength / (Math.max(categoriesLength, 1) * canvasOptions.interval)
    },
    getScale: function(min, max) {
        const canvasOptions = this._canvasOptions;
        const visibleArea = this.getCanvasVisibleArea();
        const stickOffset = !this._options.stick && 1;
        let minPoint = isDefined(min) ? this.translate(min, -stickOffset) : null;
        let maxPoint = isDefined(max) ? this.translate(max, +stickOffset) : null;
        if (null === minPoint) {
            minPoint = canvasOptions.invert ? visibleArea.max : visibleArea.min
        }
        if (null === maxPoint) {
            maxPoint = canvasOptions.invert ? visibleArea.min : visibleArea.max
        }
        return this.canvasLength / Math.abs(maxPoint - minPoint)
    },
    isValid: function(value) {
        return isDefined(value) ? this._categoriesToPoints[value.valueOf()] >= 0 : false
    },
    getCorrectValue: getValue,
    to: function(value, direction) {
        const canvasOptions = this._canvasOptions;
        const categoryIndex = this._categoriesToPoints[null === value || void 0 === value ? void 0 : value.valueOf()];
        const startPointIndex = canvasOptions.startPointIndex || 0;
        const stickDelta = categoryIndex + (this._options.stick ? 0 : .5) - startPointIndex + (this._businessRange.invert ? -1 : 1) * direction * .5;
        return round(this._calculateProjection(canvasOptions.interval * stickDelta))
    },
    from: function(position) {
        let direction = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0;
        const canvasOptions = this._canvasOptions;
        const startPoint = canvasOptions.startPoint;
        const categories = this.visibleCategories || this._categories;
        const categoriesLength = categories.length;
        const stickInterval = this._options.stick ? .5 : 0;
        let result = round((position - startPoint) / canvasOptions.interval + stickInterval - .5 - .5 * direction);
        if (result >= categoriesLength) {
            result = categoriesLength - 1
        }
        if (result < 0) {
            result = 0
        }
        if (canvasOptions.invert) {
            result = categoriesLength - result - 1
        }
        return categories[result]
    },
    _add: function() {
        return NaN
    },
    toValue: getValue,
    isValueProlonged: true,
    getRangeByMinZoomValue(minZoom, visualRange) {
        const categories = this._categories;
        const minVisibleIndex = categories.indexOf(visualRange.minVisible);
        const maxVisibleIndex = categories.indexOf(visualRange.maxVisible);
        const startIndex = minVisibleIndex + minZoom - 1;
        const endIndex = maxVisibleIndex - minZoom + 1;
        if (categories[startIndex]) {
            return [visualRange.minVisible, categories[startIndex]]
        } else {
            return [categories[endIndex], visualRange.maxVisible]
        }
    }
};
