import { ResultStatus } from './ResultStatus';
const isFullfilled = result => result.status === ResultStatus.FULFILLED;
const markFullfilled = value => ({
  status: ResultStatus.FULFILLED,
  value: value
});

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const markRejected = error => ({
  status: 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
 */
export const waitForAllPromises = promises => {
  return Promise.all(promises.map(result => result.then(markFullfilled).catch(markRejected)));
};

/**
 * Find all fullfilled promises and return their values
 * @param results
 */
export const getOnlyFulfilled = results => results.filter(isFullfilled).map(result => result.value);
// eslint-disable-next-line @atlaskit/editor/no-re-export
// eslint-disable-next-line @atlaskit/editor/no-re-export