{"version":3,"file":"propsFactory.mjs","names":["propsFactory","props","source","defaults","Object","keys","reduce","obj","prop","isObjectDefinition","_typeof","Array","isArray","definition","type","_objectSpread"],"sources":["../../src/utils/propsFactory.ts"],"sourcesContent":["// Types\nimport type { IfAny } from '@vue/shared' // eslint-disable-line vue/prefer-import-from-vue\nimport type { ComponentObjectPropsOptions, Prop, PropType } from 'vue'\n\n/**\n * Creates a factory function for props definitions.\n * This is used to define props in a composable then override\n * default values in an implementing component.\n *\n * @example Simplified signature\n * (props: Props) => (defaults?: Record<keyof props, any>) => Props\n *\n * @example Usage\n * const makeProps = propsFactory({\n *   foo: String,\n * })\n *\n * defineComponent({\n *   props: {\n *     ...makeProps({\n *       foo: 'a',\n *     }),\n *   },\n *   setup (props) {\n *     // would be \"string | undefined\", now \"string\" because a default has been provided\n *     props.foo\n *   },\n * }\n */\n\nexport function propsFactory<PropsOptions extends ComponentObjectPropsOptions>(\n  props: PropsOptions,\n  source: string\n) {\n  return <Defaults extends PartialKeys<PropsOptions> = {}>(\n    defaults?: Defaults\n  ): AppendDefault<PropsOptions, Defaults> => {\n    return Object.keys(props).reduce<any>((obj, prop) => {\n      const isObjectDefinition =\n        typeof props[prop] === 'object' &&\n        props[prop] != null &&\n        !Array.isArray(props[prop])\n      const definition = isObjectDefinition\n        ? props[prop]\n        : { type: props[prop] }\n\n      if (defaults && prop in defaults) {\n        obj[prop] = {\n          ...definition,\n          default: defaults[prop],\n        }\n      } else {\n        obj[prop] = definition\n      }\n\n      if (source && !obj[prop].source) {\n        obj[prop].source = source\n      }\n\n      return obj\n    }, {})\n  }\n}\n\ntype AppendDefault<\n  T extends ComponentObjectPropsOptions,\n  D extends PartialKeys<T>\n> = {\n  [P in keyof T]-?: unknown extends D[P]\n    ? T[P]\n    : T[P] extends Record<string, unknown>\n    ? Omit<T[P], 'type' | 'default'> & {\n        type: PropType<MergeDefault<T[P], D[P]>>\n        default: MergeDefault<T[P], D[P]>\n      }\n    : {\n        type: PropType<MergeDefault<T[P], D[P]>>\n        default: MergeDefault<T[P], D[P]>\n      }\n}\n\ntype MergeDefault<T, D> = unknown extends D\n  ? InferPropType<T>\n  : NonNullable<InferPropType<T>> | D\n\n/**\n * Like `Partial<T>` but doesn't care what the value is\n */\ntype PartialKeys<T> = { [P in keyof T]?: unknown }\n\n// Copied from Vue\ntype InferPropType<T> = [T] extends [null]\n  ? any // null & true would fail to infer\n  : [T] extends [{ type: null | true }]\n  ? // As TS issue https://github.com/Microsoft/TypeScript/issues/14829\n    // somehow `ObjectConstructor` when inferred from { (): T } becomes `any`\n    // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`\n    any\n  : [T] extends [ObjectConstructor | { type: ObjectConstructor }]\n  ? Record<string, any>\n  : [T] extends [BooleanConstructor | { type: BooleanConstructor }]\n  ? boolean\n  : [T] extends [DateConstructor | { type: DateConstructor }]\n  ? Date\n  : [T] extends [(infer U)[] | { type: (infer U)[] }]\n  ? U extends DateConstructor\n    ? Date | InferPropType<U>\n    : InferPropType<U>\n  : [T] extends [Prop<infer V, infer D>]\n  ? unknown extends V\n    ? IfAny<V, V, D>\n    : V\n  : T\n"],"mappings":";;;;;;AAAA;AACyC;;AAGzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,OAAO,SAASA,YAAYA,CAC1BC,KAAmB,EACnBC,MAAc,EACd;EACA,OAAO,UACLC,QAAmB,EACuB;IAC1C,OAAOC,MAAM,CAACC,IAAI,CAACJ,KAAK,CAAC,CAACK,MAAM,CAAM,UAACC,GAAG,EAAEC,IAAI,EAAK;MACnD,IAAMC,kBAAkB,GACtBC,OAAA,CAAOT,KAAK,CAACO,IAAI,CAAC,MAAK,QAAQ,IAC/BP,KAAK,CAACO,IAAI,CAAC,IAAI,IAAI,IACnB,CAACG,KAAK,CAACC,OAAO,CAACX,KAAK,CAACO,IAAI,CAAC,CAAC;MAC7B,IAAMK,UAAU,GAAGJ,kBAAkB,GACjCR,KAAK,CAACO,IAAI,CAAC,GACX;QAAEM,IAAI,EAAEb,KAAK,CAACO,IAAI;MAAE,CAAC;MAEzB,IAAIL,QAAQ,IAAIK,IAAI,IAAIL,QAAQ,EAAE;QAChCI,GAAG,CAACC,IAAI,CAAC,GAAAO,aAAA,CAAAA,aAAA,KACJF,UAAU;UACb,WAASV,QAAQ,CAACK,IAAI;QAAC,EACxB;MACH,CAAC,MAAM;QACLD,GAAG,CAACC,IAAI,CAAC,GAAGK,UAAU;MACxB;MAEA,IAAIX,MAAM,IAAI,CAACK,GAAG,CAACC,IAAI,CAAC,CAACN,MAAM,EAAE;QAC/BK,GAAG,CAACC,IAAI,CAAC,CAACN,MAAM,GAAGA,MAAM;MAC3B;MAEA,OAAOK,GAAG;IACZ,CAAC,EAAE,CAAC,CAAC,CAAC;EACR,CAAC;AACH;;AAuBA;AACA;AACA;;AAGA"}