{"size": 1303, "ext": "js", "max_stars_repo_path": "packages/terra-application/src/disclosure-manager/_disclosureCallbacks.js", "max_stars_repo_name": "neilpfeiffer/terra-application", "max_stars_repo_stars_event_min_datetime": "2016-10-26T16:35:47.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-28T20:39:52.000Z", "max_issues_repo_path": "packages/terra-application/src/disclosure-manager/_disclosureCallbacks.js", "max_issues_repo_name": "neilpfeiffer/terra-application", "max_issues_repo_issues_event_min_datetime": "2016-09-17T01:18:10.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-04T18:25:29.000Z", "max_forks_repo_path": "packages/terra-application/src/disclosure-manager/_disclosureCallbacks.js", "max_forks_repo_name": "neilpfeiffer/terra-application", "max_forks_repo_forks_event_min_datetime": "2016-10-21T02:42:02.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-29T17:07:58.000Z"}
{"max_stars_count": 9, "max_issues_count": 147, "max_forks_count": 11, "avg_line_length": 32.575, "max_line_length": 118, "alphanum_fraction": 0.7536454336}
let disclosureCallbacks = [];

const addCallback = (callback) => {
  disclosureCallbacks = [...disclosureCallbacks, callback];
};

const removeCallback = (callback) => {
  disclosureCallbacks = disclosureCallbacks.filter(disclosureCallback => disclosureCallback !== callback);
};

/**
 * Closes the most recent disclosure for any instance of a terra-application
 * disclosure manager.  This should be used only as a last resort to hook up external events to the disclosure manager
 * functionality.
 * @returns {Promise} A promise that will be resolved once the disclosure is successfully dismissed.
 * If the disclosure is not dismissed the promise will be rejected.  If there is no disclosure available to close,
 * the promise will be resolved.
 */
const closeMostRecentDisclosure = () => {
  if (!disclosureCallbacks.length) {
    return Promise.resolve();
  }

  return disclosureCallbacks[disclosureCallbacks.length - 1]();
};

/**
 * Returns the number of disclosures that are currently being disclosed through any instance of a terra-application
 * disclosure manager.
 * @returns {number} The active disclosure count.
 */
const getActiveDisclosureCount = () => (disclosureCallbacks.length);

export {
  addCallback,
  removeCallback,
  closeMostRecentDisclosure,
  getActiveDisclosureCount,
};
