{"version":3,"file":"install.mjs","names":[],"sources":["../../../../../packages/utils/vue/install.ts"],"sourcesContent":["import { hasOwn, isArray } from '@vue/shared'\nimport { fromPairs, isPlainObject } from 'lodash-unified'\nimport { NOOP } from '../functions'\n\nimport type { App, Directive } from 'vue'\nimport type { SFCInstallWithContext, SFCWithInstall } from './typescript'\n\nexport const withPropsDefaultsSetter = (target: any) => {\n  const _p = target.props\n  const props = isArray(_p) ? fromPairs(_p.map((key) => [key, {}])) : _p\n\n  target.setPropsDefaults = (defaults: Record<string, any>) => {\n    if (!props) {\n      return\n    }\n\n    for (const [key, value] of Object.entries(defaults)) {\n      const prop = props[key]\n\n      if (!hasOwn(props, key)) {\n        continue\n      }\n\n      if (isPlainObject(prop)) {\n        // e.g. { type: String }\n        props[key] = {\n          ...prop,\n          default: value,\n        }\n        continue\n      }\n\n      props[key] = {\n        type: prop,\n        default: value,\n      }\n    }\n\n    target.props = props\n  }\n}\n\nexport const withInstall = <T, E extends Record<string, any>>(\n  main: T,\n  extra?: E\n) => {\n  ;(main as SFCWithInstall<T>).install = (app): void => {\n    for (const comp of [main, ...Object.values(extra ?? {})]) {\n      app.component(comp.name, comp)\n    }\n  }\n\n  if (extra) {\n    for (const [key, comp] of Object.entries(extra)) {\n      ;(main as any)[key] = comp\n    }\n  }\n  withPropsDefaultsSetter(main)\n  return main as SFCWithInstall<T> & E\n}\n\nexport const withInstallFunction = <T>(fn: T, name: string) => {\n  ;(fn as SFCWithInstall<T>).install = (app: App) => {\n    ;(fn as SFCInstallWithContext<T>)._context = app._context\n    app.config.globalProperties[name] = fn\n  }\n\n  return fn as SFCInstallWithContext<T>\n}\n\nexport const withInstallDirective = <T extends Directive>(\n  directive: T,\n  name: string\n) => {\n  ;(directive as SFCWithInstall<T>).install = (app: App): void => {\n    app.directive(name, directive)\n  }\n\n  return directive as SFCWithInstall<T>\n}\n\nexport const withNoopInstall = <T>(component: T) => {\n  ;(component as SFCWithInstall<T>).install = NOOP\n  withPropsDefaultsSetter(component)\n  return component as SFCWithInstall<T>\n}\n"],"mappings":";;;;AAOA,MAAa,2BAA2B,WAAgB;CACtD,MAAM,KAAK,OAAO;CAClB,MAAM,QAAQ,QAAQ,GAAG,GAAG,UAAU,GAAG,KAAK,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG;CAEpE,OAAO,oBAAoB,aAAkC;EAC3D,IAAI,CAAC,OACH;EAGF,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,EAAE;GACnD,MAAM,OAAO,MAAM;GAEnB,IAAI,CAAC,OAAO,OAAO,IAAI,EACrB;GAGF,IAAI,cAAc,KAAK,EAAE;IAEvB,MAAM,OAAO;KACX,GAAG;KACH,SAAS;KACV;IACD;;GAGF,MAAM,OAAO;IACX,MAAM;IACN,SAAS;IACV;;EAGH,OAAO,QAAQ;;;AAInB,MAAa,eACX,MACA,UACG;CACF,KAA4B,WAAW,QAAc;EACpD,KAAK,MAAM,QAAQ,CAAC,MAAM,GAAG,OAAO,OAAO,SAAS,EAAE,CAAC,CAAC,EACtD,IAAI,UAAU,KAAK,MAAM,KAAK;;CAIlC,IAAI,OACF,KAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,MAAM,EAC5C,KAAc,OAAO;CAG1B,wBAAwB,KAAK;CAC7B,OAAO;;AAGT,MAAa,uBAA0B,IAAO,SAAiB;CAC5D,GAA0B,WAAW,QAAa;EAChD,GAAiC,WAAW,IAAI;EACjD,IAAI,OAAO,iBAAiB,QAAQ;;CAGtC,OAAO;;AAGT,MAAa,wBACX,WACA,SACG;CACF,UAAiC,WAAW,QAAmB;EAC9D,IAAI,UAAU,MAAM,UAAU;;CAGhC,OAAO;;AAGT,MAAa,mBAAsB,cAAiB;CACjD,UAAiC,UAAU;CAC5C,wBAAwB,UAAU;CAClC,OAAO"}