'use strict';

/**
 * Copyright IBM Corp. 2016, 2023
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

/**
 * @param refs Refs to merge.
 * @returns Merged ref.
 */
const mergeRefs = (...refs) => {
  return value => {
    refs.forEach(ref => {
      if (!ref) return;

      // https://github.com/facebook/react/issues/13029#issuecomment-410002316
      if (typeof ref === 'function') {
        ref(value);
      } else if (typeof ref === 'object' && 'current' in ref) {
        ref.current = value;
      }
    });
  };
};

exports.mergeRefs = mergeRefs;
