import { cloneVNode, isVNode, render, Comment, Fragment } from 'vue';
import { devWarning } from '../log.mjs';
import { filterEmpty } from './props.mjs';

function cloneElement(vnode, nodeProps = {}, override = true, mergeRef = false) {
  let ele = vnode;
  if (Array.isArray(vnode))
    ele = filterEmpty(vnode)[0];
  if (!ele)
    return null;
  const node = cloneVNode(ele, nodeProps, mergeRef);
  node.props = override ? { ...node.props, ...nodeProps } : node.props;
  devWarning(typeof node.props.class !== "object", "class must be string");
  return node;
}
function cloneVNodes(vnodes, nodeProps = {}, override = true) {
  return vnodes.map((vnode) => cloneElement(vnode, nodeProps, override));
}
function deepCloneElement(vnode, nodeProps = {}, override = true, mergeRef = false) {
  if (Array.isArray(vnode)) {
    return vnode.map((item) => deepCloneElement(item, nodeProps, override, mergeRef));
  } else {
    if (!isVNode(vnode))
      return vnode;
    const cloned = cloneElement(vnode, nodeProps, override, mergeRef);
    if (Array.isArray(cloned.children))
      cloned.children = deepCloneElement(cloned.children);
    return cloned;
  }
}
function triggerVNodeUpdate(vm, attrs, dom) {
  render(cloneVNode(vm, { ...attrs }), dom);
}
function ensureValidVNode(slot) {
  return (slot || []).some((child) => {
    if (!isVNode(child))
      return true;
    if (child.type === Comment)
      return false;
    if (child.type === Fragment && !ensureValidVNode(child.children))
      return false;
    return true;
  }) ? slot : null;
}
function customRenderSlot(slots, name, props, fallback) {
  var _a;
  const slot = (_a = slots[name]) == null ? void 0 : _a.call(slots, props);
  if (ensureValidVNode(slot))
    return slot;
  return fallback == null ? void 0 : fallback();
}

export { cloneElement, cloneVNodes, customRenderSlot, deepCloneElement, triggerVNodeUpdate };
//# sourceMappingURL=vnode.mjs.map
