/**
 * DevExtreme (cjs/ui/progress_bar.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _renderer = _interopRequireDefault(require("../core/renderer"));
var _track_bar = _interopRequireDefault(require("./track_bar"));
var _extend = require("../core/utils/extend");
var _type = require("../core/utils/type");
var _component_registrator = _interopRequireDefault(require("../core/component_registrator"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const PROGRESSBAR_CLASS = "dx-progressbar";
const PROGRESSBAR_CONTAINER_CLASS = "dx-progressbar-container";
const PROGRESSBAR_RANGE_CONTAINER_CLASS = "dx-progressbar-range-container";
const PROGRESSBAR_RANGE_CLASS = "dx-progressbar-range";
const PROGRESSBAR_WRAPPER_CLASS = "dx-progressbar-wrapper";
const PROGRESSBAR_STATUS_CLASS = "dx-progressbar-status";
const PROGRESSBAR_INDETERMINATE_SEGMENT_CONTAINER = "dx-progressbar-animating-container";
const PROGRESSBAR_INDETERMINATE_SEGMENT = "dx-progressbar-animating-segment";
const ProgressBar = _track_bar.default.inherit({
    _getDefaultOptions: function() {
        return (0, _extend.extend)(this.callBase(), {
            value: 0,
            statusFormat: function(ratio) {
                return "Progress: " + Math.round(100 * ratio) + "%"
            },
            showStatus: true,
            onComplete: null,
            activeStateEnabled: false,
            statusPosition: "bottom left",
            _animatingSegmentCount: 0
        })
    },
    _defaultOptionsRules: function() {
        return this.callBase().concat([{
            device: function(device) {
                return "android" === device.platform
            },
            options: {
                _animatingSegmentCount: 2
            }
        }])
    },
    _initMarkup: function() {
        this._renderStatus();
        this._createCompleteAction();
        this.callBase();
        this.$element().addClass("dx-progressbar");
        this._$wrapper.addClass("dx-progressbar-wrapper");
        this._$bar.addClass("dx-progressbar-container");
        this.setAria("role", "progressbar");
        (0, _renderer.default)("<div>").addClass("dx-progressbar-range-container").appendTo(this._$wrapper).append(this._$bar);
        this._$range.addClass("dx-progressbar-range");
        this._toggleStatus(this.option("showStatus"))
    },
    _useTemplates: function() {
        return false
    },
    _createCompleteAction: function() {
        this._completeAction = this._createActionByOption("onComplete")
    },
    _renderStatus: function() {
        this._$status = (0, _renderer.default)("<div>").addClass("dx-progressbar-status")
    },
    _renderIndeterminateState: function() {
        this._$segmentContainer = (0, _renderer.default)("<div>").addClass("dx-progressbar-animating-container");
        const segments = this.option("_animatingSegmentCount");
        for (let i = 0; i < segments; i++) {
            (0, _renderer.default)("<div>").addClass(PROGRESSBAR_INDETERMINATE_SEGMENT).addClass("dx-progressbar-animating-segment-" + (i + 1)).appendTo(this._$segmentContainer)
        }
        this._$segmentContainer.appendTo(this._$wrapper)
    },
    _toggleStatus: function(value) {
        const splitPosition = this.option("statusPosition").split(" ");
        if (value) {
            if ("top" === splitPosition[0] || "left" === splitPosition[0]) {
                this._$status.prependTo(this._$wrapper)
            } else {
                this._$status.appendTo(this._$wrapper)
            }
        } else {
            this._$status.detach()
        }
        this._togglePositionClass()
    },
    _togglePositionClass: function() {
        const position = this.option("statusPosition");
        const splitPosition = position.split(" ");
        this._$wrapper.removeClass("dx-position-top-left dx-position-top-right dx-position-bottom-left dx-position-bottom-right dx-position-left dx-position-right");
        let positionClass = "dx-position-" + splitPosition[0];
        if (splitPosition[1]) {
            positionClass += "-" + splitPosition[1]
        }
        this._$wrapper.addClass(positionClass)
    },
    _toggleIndeterminateState: function(value) {
        if (value) {
            this._renderIndeterminateState();
            this._$bar.toggle(false)
        } else {
            this._$bar.toggle(true);
            this._$segmentContainer.remove();
            delete this._$segmentContainer
        }
    },
    _renderValue: function() {
        const val = this.option("value");
        const max = this.option("max");
        if (!val && 0 !== val) {
            this._toggleIndeterminateState(true);
            return
        }
        if (this._$segmentContainer) {
            this._toggleIndeterminateState(false)
        }
        if (val === max) {
            this._completeAction()
        }
        this.callBase();
        this._setStatus()
    },
    _setStatus: function() {
        let format = this.option("statusFormat");
        if ((0, _type.isFunction)(format)) {
            format = format.bind(this)
        } else {
            format = function(value) {
                return value
            }
        }
        const statusText = format(this._currentRatio, this.option("value"));
        this._$status.text(statusText)
    },
    _dispose: function() {
        this._$status.remove();
        this.callBase()
    },
    _optionChanged: function(args) {
        switch (args.name) {
            case "statusFormat":
                this._setStatus();
                break;
            case "showStatus":
                this._toggleStatus(args.value);
                break;
            case "statusPosition":
                this._toggleStatus(this.option("showStatus"));
                break;
            case "onComplete":
                this._createCompleteAction();
                break;
            case "_animatingSegmentCount":
                break;
            default:
                this.callBase(args)
        }
    }
});
(0, _component_registrator.default)("dxProgressBar", ProgressBar);
var _default = ProgressBar;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
