import { useRefWithInit as r } from "./useRefWithInit.js";
function b(n, e, c, t) {
  const l = r(i).current;
  return s(l, n, e, c, t) && o(l, [n, e, c, t]), l.callback;
}
function h(n) {
  const e = r(i).current;
  return f(e, n) && o(e, n), e.callback;
}
function i() {
  return {
    callback: null,
    cleanup: null,
    refs: []
  };
}
function s(n, e, c, t, l) {
  return n.refs[0] !== e || n.refs[1] !== c || n.refs[2] !== t || n.refs[3] !== l;
}
function f(n, e) {
  return n.refs.length !== e.length || n.refs.some((c, t) => c !== e[t]);
}
function o(n, e) {
  if (n.refs = e, e.every((c) => c == null)) {
    n.callback = null;
    return;
  }
  n.callback = (c) => {
    if (n.cleanup && (n.cleanup(), n.cleanup = null), c != null) {
      const t = Array(e.length).fill(null);
      for (let l = 0; l < e.length; l += 1) {
        const u = e[l];
        if (u != null)
          switch (typeof u) {
            case "function": {
              const a = u(c);
              typeof a == "function" && (t[l] = a);
              break;
            }
            case "object": {
              u.current = c;
              break;
            }
          }
      }
      n.cleanup = () => {
        for (let l = 0; l < e.length; l += 1) {
          const u = e[l];
          if (u != null)
            switch (typeof u) {
              case "function": {
                const a = t[l];
                typeof a == "function" ? a() : u(null);
                break;
              }
              case "object": {
                u.current = null;
                break;
              }
            }
        }
      };
    }
  };
}
export {
  b as useMergedRefs,
  h as useMergedRefsN
};
