/**
 * DevExtreme (cjs/core/utils/queue.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.create = createQueue;
exports.enqueue = void 0;
var _errors = _interopRequireDefault(require("../errors"));
var _deferred = require("../../core/utils/deferred");

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

function createQueue(discardPendingTasks) {
    let _tasks = [];
    let _busy = false;

    function exec() {
        while (_tasks.length) {
            _busy = true;
            const task = _tasks.shift();
            const result = task();
            if (void 0 === result) {
                continue
            }
            if (result.then) {
                (0, _deferred.when)(result).always(exec);
                return
            }
            throw _errors.default.Error("E0015")
        }
        _busy = false
    }
    return {
        add: function(task, removeTaskCallback) {
            if (!discardPendingTasks) {
                _tasks.push(task)
            } else {
                if (_tasks[0] && removeTaskCallback) {
                    removeTaskCallback(_tasks[0])
                }
                _tasks = [task]
            }
            if (!_busy) {
                exec()
            }
        },
        busy: function() {
            return _busy
        }
    }
}
const enqueue = createQueue().add;
exports.enqueue = enqueue;
