{"version":3,"file":"useChildComponents.mjs","sources":["../../../../src/composables/useChildComponents.ts"],"sourcesContent":["import { ComputedRef, InjectionKey, PropType, computed, getCurrentInstance, inject, provide } from 'vue'\nimport { DefineComponentOptions, ExtractComponentPropTypes } from '../utils/component-options'\n\nconst CHILD_COMPONENT_PROP_PREFIX = 'child:'\n\ntype ChildProps = Record<string, Record<string, any>>\ntype ChildComponents = Record<string, DefineComponentOptions>\ntype NonSymbol<T> = T extends symbol ? never : T\n\n/** @example Converts `{ closeButton: VaButton }` to `{ child:closeButton: ExtractComponentPropTypes<VaButton> }` */\ntype ChildComponentsPropsDefinition<T extends ChildComponents> = {\n  [K in keyof T as `${typeof CHILD_COMPONENT_PROP_PREFIX}${NonSymbol<K>}`]: {\n    type: PropType<ExtractComponentPropTypes<T[K]>>,\n    required: false,\n    default: undefined\n  }\n}\n\nconst CHILD_COMPONENTS_INJECT_KEY = '$va:childComponents' as any as InjectionKey<ComputedRef<ChildProps>>\n\n/**\n * Creates type definition for props. No runtime code is generated.\n * Creates new props with `child:` prefix, add names of child components and their props.\n *\n * @example\n *\n * Following code will generate new prop `child:closeButton` with type equal to `VaButton` props object.\n *\n * ```ts\n * const defineChildProps = defineChildComponents<{\n *  closeButton: VaButton\n * }>()\n * ```\n */\nexport const defineChildProps = <T extends ChildComponents>(obj: T): ChildComponentsPropsDefinition<T> => {\n  return Object.keys(obj).reduce((acc, key) => {\n    const childName = `${CHILD_COMPONENT_PROP_PREFIX}${key}` as string\n    acc[childName] = {\n      type: Object,\n      required: false,\n      default: undefined,\n    }\n    return acc\n  }, {} as any)\n}\n\n/** @notice No chaining for now. We assume component names as uniq across component tree */\nexport const useChildComponents = (props: Record<`${typeof CHILD_COMPONENT_PROP_PREFIX}${string}`, any>) => {\n  const childProps = computed(() => {\n    const propNames = Object.keys(props)\n\n    return propNames.reduce((acc, propName) => {\n      if (propName.startsWith(CHILD_COMPONENT_PROP_PREFIX)) {\n        const childName = propName.slice(CHILD_COMPONENT_PROP_PREFIX.length)\n\n        acc[childName] = props[propName as any]\n      }\n\n      return acc\n    }, {} as ChildProps)\n  })\n\n  provide(CHILD_COMPONENTS_INJECT_KEY, childProps)\n}\n\nexport const injectChildPropsFromParent = () => {\n  const childName = getCurrentInstance()?.attrs['va-child'] as string\n\n  if (!childName) {\n    return null\n  }\n\n  const childProps = inject(CHILD_COMPONENTS_INJECT_KEY)\n\n  if (!childProps?.value) {\n    return null\n  }\n\n  return computed(() => childProps.value[childName])\n}\n"],"names":[],"mappings":";AAGA,MAAM,8BAA8B;AAepC,MAAM,8BAA8B;AAgBvB,MAAA,mBAAmB,CAA4B,QAA8C;AACxG,SAAO,OAAO,KAAK,GAAG,EAAE,OAAO,CAAC,KAAK,QAAQ;AAC3C,UAAM,YAAY,GAAG,2BAA2B,GAAG,GAAG;AACtD,QAAI,SAAS,IAAI;AAAA,MACf,MAAM;AAAA,MACN,UAAU;AAAA,MACV,SAAS;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT,GAAG,CAAS,CAAA;AACd;AAGa,MAAA,qBAAqB,CAAC,UAAyE;AACpG,QAAA,aAAa,SAAS,MAAM;AAC1B,UAAA,YAAY,OAAO,KAAK,KAAK;AAEnC,WAAO,UAAU,OAAO,CAAC,KAAK,aAAa;AACrC,UAAA,SAAS,WAAW,2BAA2B,GAAG;AACpD,cAAM,YAAY,SAAS,MAAM,4BAA4B,MAAM;AAE/D,YAAA,SAAS,IAAI,MAAM,QAAe;AAAA,MACxC;AAEO,aAAA;AAAA,IACT,GAAG,CAAgB,CAAA;AAAA,EAAA,CACpB;AAED,UAAQ,6BAA6B,UAAU;AACjD;AAEO,MAAM,6BAA6B,MAAM;;AAC9C,QAAM,aAAY,wBAAA,MAAA,mBAAsB,MAAM;AAE9C,MAAI,CAAC,WAAW;AACP,WAAA;AAAA,EACT;AAEM,QAAA,aAAa,OAAO,2BAA2B;AAEjD,MAAA,EAAC,yCAAY,QAAO;AACf,WAAA;AAAA,EACT;AAEA,SAAO,SAAS,MAAM,WAAW,MAAM,SAAS,CAAC;AACnD;"}