{"version":3,"file":"vnode.mjs","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, isArray } from '@vue/shared'\nimport { hasOwn } from '../objects'\nimport { debugWarn } from '../error'\nimport type {\n  VNode,\n  VNodeArrayChildren,\n  VNodeChild,\n  VNodeNormalizedChildren,\n} 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 (Array.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 const ensureOnlyChild = (children: VNodeArrayChildren | undefined) => {\n  if (!isArray(children) || children.length > 1) {\n    throw new Error('expect to receive a single Vue element child')\n  }\n  return children[0]\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) && isArray(child.children)) {\n      result.push(...flattedChildren(child.children))\n    } else {\n      result.push(child)\n      if (isVNode(child) && child.component?.subTree) {\n        result.push(...flattedChildren(child.component.subTree))\n      }\n    }\n  })\n  return result\n}\n"],"names":["o","PatchFlags","TEXT","CLASS","STYLE","PROPS","FULL_PROPS","HYDRATE_EVENTS","STABLE_FRAGMENT","KEYED_FRAGMENT","UNKEYED_FRAGMENT","NEED_PATCH","DYNAMIC_SLOTS","HOISTED","BAIL","isFragment","e","d","type","m","isText","f","isComment","N","isTemplate","isValidElementNode","p","r","getFirstValidNode","children","Array","isArray","renderIf","renderBlock","V","l","s","getNormalizedProps","C","props","t","n","Object","keys","forEach","i","E","default","x","ensureOnlyChild","u","length","Error","flattedChildren","push","component","subTree"],"mappings":"2PAA4R,IAAaA,EAAZC,IAAYD,EAAyZC,GAAY,CAAE,GAAjaD,EAAEE,KAAK,GAAG,OAAOF,EAAEA,EAAEG,MAAM,GAAG,QAAQH,EAAEA,EAAEI,MAAM,GAAG,QAAQJ,EAAEA,EAAEK,MAAM,GAAG,QAAQL,EAAEA,EAAEM,WAAW,IAAI,aAAaN,EAAEA,EAAEO,eAAe,IAAI,iBAAiBP,EAAEA,EAAEQ,gBAAgB,IAAI,kBAAkBR,EAAEA,EAAES,eAAe,KAAK,iBAAiBT,EAAEA,EAAEU,iBAAiB,KAAK,mBAAmBV,EAAEA,EAAEW,WAAW,KAAK,aAAaX,EAAEA,EAAEY,cAAc,MAAM,gBAAgBZ,EAAEA,EAAEa,SAAS,GAAG,UAAUb,EAAEA,EAAEc,MAAM,GAAG,OAAOd,GAA2B,SAASe,EAAWC,GAAG,OAAOC,EAAED,IAAIA,EAAEE,OAAOC,CAAC,CAAQ,SAASC,EAAOJ,GAAG,OAAOC,EAAED,IAAIA,EAAEE,OAAOG,CAAC,CAAQ,SAASC,EAAUN,GAAG,OAAOC,EAAED,IAAIA,EAAEE,OAAOK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