{"version":3,"file":"vnode.mjs","names":[],"sources":["../../../../../packages/utils/vue/vnode.ts"],"sourcesContent":["import {\n  Comment,\n  Fragment,\n  Text,\n  createBlock,\n  createCommentVNode,\n  isVNode,\n  openBlock,\n} from 'vue'\nimport { camelize } from '../strings'\nimport { isArray } from '../types'\nimport { hasOwn } from '../objects'\nimport { debugWarn } from '../error'\n\nimport type { VNode, VNodeChild, VNodeNormalizedChildren } from 'vue'\n\nconst SCOPE = 'utils/vue/vnode'\n\nexport enum PatchFlags {\n  TEXT = 1,\n  CLASS = 2,\n  STYLE = 4,\n  PROPS = 8,\n  FULL_PROPS = 16,\n  HYDRATE_EVENTS = 32,\n  STABLE_FRAGMENT = 64,\n  KEYED_FRAGMENT = 128,\n  UNKEYED_FRAGMENT = 256,\n  NEED_PATCH = 512,\n  DYNAMIC_SLOTS = 1024,\n  HOISTED = -1,\n  BAIL = -2,\n}\n\nexport type VNodeChildAtom = Exclude<VNodeChild, Array<any>>\nexport type RawSlots = Exclude<\n  VNodeNormalizedChildren,\n  Array<any> | null | string\n>\n\nexport function isFragment(node: VNode): boolean\nexport function isFragment(node: unknown): node is VNode\nexport function isFragment(node: unknown): node is VNode {\n  return isVNode(node) && node.type === Fragment\n}\n\nexport function isText(node: VNode): boolean\nexport function isText(node: unknown): node is VNode\nexport function isText(node: unknown): node is VNode {\n  return isVNode(node) && node.type === Text\n}\n\nexport function isComment(node: VNode): boolean\nexport function isComment(node: unknown): node is VNode\nexport function isComment(node: unknown): node is VNode {\n  return isVNode(node) && node.type === Comment\n}\n\nconst TEMPLATE = 'template'\nexport function isTemplate(node: VNode): boolean\nexport function isTemplate(node: unknown): node is VNode\nexport function isTemplate(node: unknown): node is VNode {\n  return isVNode(node) && node.type === TEMPLATE\n}\n\n/**\n * determine if the element is a valid element type rather than fragments and comment e.g. <template> v-if\n * @param node {VNode} node to be tested\n */\nexport function isValidElementNode(node: VNode): boolean\nexport function isValidElementNode(node: unknown): node is VNode\nexport function isValidElementNode(node: unknown): node is VNode {\n  return isVNode(node) && !isFragment(node) && !isComment(node)\n}\n\n/**\n * get a valid child node (not fragment nor comment)\n * @param node {VNode} node to be searched\n * @param depth {number} depth to be searched\n */\nfunction getChildren(\n  node: VNodeNormalizedChildren | VNodeChild,\n  depth: number\n): VNodeNormalizedChildren | VNodeChild {\n  if (isComment(node)) return\n  if (isFragment(node) || isTemplate(node)) {\n    return depth > 0 ? getFirstValidNode(node.children, depth - 1) : undefined\n  }\n  return node\n}\n\nexport const getFirstValidNode = (\n  nodes: VNodeNormalizedChildren,\n  maxDepth = 3\n) => {\n  if (isArray(nodes)) {\n    return getChildren(nodes[0], maxDepth)\n  } else {\n    return getChildren(nodes, maxDepth)\n  }\n}\n\nexport function renderIf(\n  condition: boolean,\n  ...args: Parameters<typeof createBlock>\n) {\n  return condition ? renderBlock(...args) : createCommentVNode('v-if', true)\n}\n\nexport function renderBlock(...args: Parameters<typeof createBlock>) {\n  return (openBlock(), createBlock(...args))\n}\n\nexport const getNormalizedProps = (node: VNode) => {\n  if (!isVNode(node)) {\n    debugWarn(SCOPE, '[getNormalizedProps] must be a VNode')\n    return {}\n  }\n\n  const raw = node.props || {}\n  const type = (isVNode(node.type) ? node.type.props : undefined) || {}\n  const props: Record<string, any> = {}\n\n  Object.keys(type).forEach((key) => {\n    if (hasOwn(type[key], 'default')) {\n      props[key] = type[key].default\n    }\n  })\n\n  Object.keys(raw).forEach((key) => {\n    props[camelize(key)] = raw[key]\n  })\n\n  return props\n}\n\nexport type FlattenVNodes = Array<VNodeChildAtom | RawSlots>\n\nexport const flattedChildren = (\n  children: FlattenVNodes | VNode | VNodeNormalizedChildren\n): FlattenVNodes => {\n  const vNodes = isArray(children) ? children : [children]\n  const result: FlattenVNodes = []\n\n  vNodes.forEach((child) => {\n    if (isArray(child)) {\n      result.push(...flattedChildren(child))\n    } else if (isVNode(child) && child.component?.subTree) {\n      result.push(child, ...flattedChildren(child.component.subTree))\n    } else if (isVNode(child) && isArray(child.children)) {\n      result.push(...flattedChildren(child.children))\n    } else if (isVNode(child) && child.shapeFlag === 2) {\n      // @ts-ignore\n      result.push(...flattedChildren(child.type()))\n    } else {\n      result.push(child)\n    }\n  })\n  return result\n}\n"],"mappings":";;;;;;;AAgBA,MAAM,QAAQ;AAEd,IAAY,aAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;KACD;AAUD,SAAgB,WAAW,MAA8B;AACvD,QAAO,QAAQ,KAAK,IAAI,KAAK,SAAS;;AAKxC,SAAgB,OAAO,MAA8B;AACnD,QAAO,QAAQ,KAAK,IAAI,KAAK,SAAS;;AAKxC,SAAgB,UAAU,MAA8B;AACtD,QAAO,QAAQ,KAAK,IAAI,KAAK,SAAS;;AAGxC,MAAM,WAAW;AAGjB,SAAgB,WAAW,MAA8B;AACvD,QAAO,QAAQ,KAAK,IAAI,KAAK,SAAS;;AASxC,SAAgB,mBAAmB,MAA8B;AAC/D,QAAO,QAAQ,KAAK,IAAI,CAAC,WAAW,KAAK,IAAI,CAAC,UAAU,KAAK;;;;;;;AAQ/D,SAAS,YACP,MACA,OACsC;AACtC,KAAI,UAAU,KAAK,CAAE;AACrB,KAAI,WAAW,KAAK,IAAI,WAAW,KAAK,CACtC,QAAO,QAAQ,IAAI,kBAAkB,KAAK,UAAU,QAAQ,EAAE,GAAG;AAEnE,QAAO;;AAGT,MAAa,qBACX,OACA,WAAW,MACR;AACH,KAAI,QAAQ,MAAM,CAChB,QAAO,YAAY,MAAM,IAAI,SAAS;KAEtC,QAAO,YAAY,OAAO,SAAS;;AAIvC,SAAgB,SACd,WACA,GAAG,MACH;AACA,QAAO,YAAY,YAAY,GAAG,KAAK,GAAG,mBAAmB,QAAQ,KAAK;;AAG5E,SAAgB,YAAY,GAAG,MAAsC;AACnE,QAAQ,WAAW,EAAE,YAAY,GAAG,KAAK;;AAG3C,MAAa,sBAAsB,SAAgB;AACjD,KAAI,CAAC,QAAQ,KAAK,EAAE;AAClB,YAAU,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