{"version":3,"file":"runtime.mjs","sources":["../../../../../packages/utils/props/runtime.ts"],"sourcesContent":["import { hasOwn, isObject } from '@vue/shared'\r\nimport { fromPairs } from 'lodash-es'\r\nimport { warn } from 'vue'\r\n\r\nimport type { PropType } from 'vue'\r\nimport type {\r\n  EpProp,\r\n  EpPropConvert,\r\n  EpPropFinalized,\r\n  EpPropInput,\r\n  EpPropMergeType,\r\n  IfEpProp,\r\n  IfNativePropType,\r\n  NativePropType,\r\n} from './types'\r\n\r\nexport const epPropKey = '__epPropKey'\r\n\r\nexport const definePropType = <T>(val: any): PropType<T> => val\r\n\r\nexport const isEpProp = (val: unknown): val is EpProp<any, any, any> =>\r\n  isObject(val) && !!(val as any)[epPropKey]\r\n\r\n/**\r\n * @description Build prop. It can better optimize prop types\r\n * @description 生成 prop，能更好地优化类型\r\n * @example\r\n  // limited options\r\n  // the type will be PropType<'light' | 'dark'>\r\n  buildProp({\r\n    type: String,\r\n    values: ['light', 'dark'],\r\n  } as const)\r\n  * @example\r\n  // limited options and other types\r\n  // the type will be PropType<'small' | 'large' | number>\r\n  buildProp({\r\n    type: [String, Number],\r\n    values: ['small', 'large'],\r\n    validator: (val: unknown): val is number => typeof val === 'number',\r\n  } as const)\r\n  @link see more: https://github.com/element-plus/element-plus/pull/3341\r\n */\r\nexport const buildProp = <\r\n  Type = never,\r\n  Value = never,\r\n  Validator = never,\r\n  Default extends EpPropMergeType<Type, Value, Validator> = never,\r\n  Required extends boolean = false\r\n>(\r\n  prop: EpPropInput<Type, Value, Validator, Default, Required>,\r\n  key?: string\r\n): EpPropFinalized<Type, Value, Validator, Default, Required> => {\r\n  // filter native prop type and nested prop, e.g `null`, `undefined` (from `buildProps`)\r\n  if (!isObject(prop) || isEpProp(prop)) return prop as any\r\n\r\n  const { values, required, default: defaultValue, type, validator } = prop\r\n\r\n  const _validator =\r\n    values || validator\r\n      ? (val: unknown) => {\r\n          let valid = false\r\n          let allowedValues: unknown[] = []\r\n\r\n          if (values) {\r\n            allowedValues = Array.from(values)\r\n            if (hasOwn(prop, 'default')) {\r\n              allowedValues.push(defaultValue)\r\n            }\r\n            valid ||= allowedValues.includes(val)\r\n          }\r\n          if (validator) valid ||= validator(val)\r\n\r\n          if (!valid && allowedValues.length > 0) {\r\n            const allowValuesText = [...new Set(allowedValues)]\r\n              .map((value) => JSON.stringify(value))\r\n              .join(', ')\r\n            warn(\r\n              `Invalid prop: validation failed${\r\n                key ? ` for prop \"${key}\"` : ''\r\n              }. Expected one of [${allowValuesText}], got value ${JSON.stringify(\r\n                val\r\n              )}.`\r\n            )\r\n          }\r\n          return valid\r\n        }\r\n      : undefined\r\n\r\n  const epProp: any = {\r\n    type,\r\n    required: !!required,\r\n    validator: _validator,\r\n    [epPropKey]: true,\r\n  }\r\n  if (hasOwn(prop, 'default')) epProp.default = defaultValue\r\n  return epProp\r\n}\r\n\r\nexport type TestProps = Record<\r\n  string,\r\n  { [epPropKey]: true } | NativePropType | EpPropInput<any, any, any, any, any>\r\n>\r\nexport const buildProps = <\r\n  Props extends Record<\r\n    string,\r\n    | { [epPropKey]: true }\r\n    | NativePropType\r\n    | EpPropInput<any, any, any, any, any>\r\n  >\r\n>(\r\n  props: Props\r\n): {\r\n  [K in keyof Props]: IfEpProp<\r\n    Props[K],\r\n    Props[K],\r\n    IfNativePropType<Props[K], Props[K], EpPropConvert<Props[K]>>\r\n  >\r\n} =>\r\n  fromPairs(\r\n    Object.entries(props).map(([key, option]) => [\r\n      key,\r\n      buildProp(option as any, key),\r\n    ])\r\n  ) as any\r\n"],"names":[],"mappings":";;;;;AAgBO,MAAM,SAAA,GAAY;AAElB,MAAM,cAAA,GAAiB,CAAI,GAAA,KAA0B;AAErD,MAAM,QAAA,GAAW,CAAC,GAAA,KACvB,QAAA,CAAS,GAAG,CAAA,IAAK,CAAC,CAAE,GAAA,CAAY,SAAS;AAsBpC,MAAM,SAAA,GAAY,CAOvB,IAAA,EACA,GAAA,KAC+D;AAE/D,EAAA,IAAI,CAAC,QAAA,C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