"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.waitForAllPromises = exports.getOnlyFulfilled = void 0;
var _ResultStatus = require("./ResultStatus");
var isFullfilled = function isFullfilled(result) {
  return result.status === _ResultStatus.ResultStatus.FULFILLED;
};
var markFullfilled = function markFullfilled(value) {
  return {
    status: _ResultStatus.ResultStatus.FULFILLED,
    value: value
  };
};

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var markRejected = function markRejected(error) {
  return {
    status: _ResultStatus.ResultStatus.FAILED,
    reason: error
  };
};

/**
 * Will wait for all promises to resolve or reject, wrapping their real results in
 * object containing the status so it's easy to filter it later. Loosely inspired by
 * [Promise.allSettled](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)
 * which can replace this implementation once it makes to the browsers.
 * @param promises
 */
var waitForAllPromises = exports.waitForAllPromises = function waitForAllPromises(promises) {
  return Promise.all(promises.map(function (result) {
    return result.then(markFullfilled).catch(markRejected);
  }));
};

/**
 * Find all fullfilled promises and return their values
 * @param results
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var getOnlyFulfilled = exports.getOnlyFulfilled = function getOnlyFulfilled(results) {
  return results.filter(isFullfilled).map(function (result) {
    return result.value;
  });
};
// eslint-disable-next-line @atlaskit/editor/no-re-export
// eslint-disable-next-line @atlaskit/editor/no-re-export