/**
 * @dyna-grid/core - Advanced Data Grid / Data Table supporting Javascript / Web Components
 * @version v1.1.0
 * @link 
 * @license 
 */
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var array_1 = require("./array");
var DyPromiseStatus;
(function (DyPromiseStatus) {
    DyPromiseStatus[DyPromiseStatus["IN_PROGRESS"] = 0] = "IN_PROGRESS";
    DyPromiseStatus[DyPromiseStatus["RESOLVED"] = 1] = "RESOLVED";
})(DyPromiseStatus = exports.DyPromiseStatus || (exports.DyPromiseStatus = {}));
var DyPromise = /** @class */ (function () {
    function DyPromise(callback) {
        var _this = this;
        this.status = DyPromiseStatus.IN_PROGRESS;
        this.resolution = null;
        this.waiters = [];
        callback(function (value) { return _this.onDone(value); }, function (params) { return _this.onReject(params); });
    }
    DyPromise.all = function (promises) {
        return new DyPromise(function (resolve) {
            var remainingToResolve = promises.length;
            var combinedValues = new Array(remainingToResolve);
            array_1.forEach(promises, function (promise, index) {
                promise.then(function (value) {
                    combinedValues[index] = value;
                    remainingToResolve--;
                    if (remainingToResolve === 0) {
                        resolve(combinedValues);
                    }
                });
            });
        });
    };
    DyPromise.resolve = function (value) {
        if (value === void 0) { value = null; }
        return new DyPromise(function (resolve) { return resolve(value); });
    };
    DyPromise.prototype.then = function (func) {
        var _this = this;
        return new DyPromise(function (resolve) {
            if (_this.status === DyPromiseStatus.RESOLVED) {
                resolve(func(_this.resolution));
            }
            else {
                _this.waiters.push(function (value) { return resolve(func(value)); });
            }
        });
    };
    DyPromise.prototype.resolveNow = function (ifNotResolvedValue, ifResolved) {
        return this.status === DyPromiseStatus.RESOLVED ? ifResolved(this.resolution) : ifNotResolvedValue;
    };
    DyPromise.prototype.onDone = function (value) {
        this.status = DyPromiseStatus.RESOLVED;
        this.resolution = value;
        array_1.forEach(this.waiters, function (waiter) { return waiter(value); });
    };
    DyPromise.prototype.onReject = function (params) {
        console.warn('TBI');
    };
    return DyPromise;
}());
exports.DyPromise = DyPromise;

//# sourceMappingURL=promise.js.map
