{"version":3,"file":"index.mjs","names":[],"sources":["../../src/render-shared/template.ts","../../src/render-shared/compile.ts","../../src/render-shared/component.ts","../../src/render-shared/props.ts"],"sourcesContent":["import type { SFCDescriptor } from '@vue/compiler-sfc'\n\n// Check if we can use compile template as inlined render function\n// inside <script setup>. This can only be done for build because\n// inlined template cannot be individually hot updated.\nexport function isUseInlineTemplate(\n  descriptor: SFCDescriptor,\n): boolean {\n  return (\n    !!descriptor.scriptSetup\n    && !descriptor.template?.src\n  )\n}\n","/// @vue/repl: https://github.com/vuejs/repl/blob/5e092b6111118f5bb5fc419f0f8f3f84cd539366/src/transform.ts\n\nimport type { CompilerOptions, SFCScriptBlock, SFCTemplateCompileResults } from '@vue/compiler-sfc'\n\nimport { testTs, transformTS } from '@velin-dev/utils/transformers/typescript'\nimport { compileScript, compileTemplate, parse } from '@vue/compiler-sfc'\n\nimport { isUseInlineTemplate } from './template'\n\nexport interface CompiledResult {\n  template: SFCTemplateCompileResults\n  script: SFCScriptBlock\n}\n\nexport async function compileSFC(source: string): Promise<CompiledResult> {\n  const { descriptor } = parse(source)\n\n  if (!descriptor.template) {\n    return await compileSFC(`${source}\\n<template><div /></template>`)\n  }\n\n  const templateOptions = {\n    source: descriptor.template.content,\n    filename: 'temp.vue',\n    id: `vue-component-${Date.now()}`,\n    compilerOptions: { runtimeModuleName: 'vue' },\n  }\n\n  const templateResult = compileTemplate(templateOptions)\n\n  const scriptLang = descriptor.script?.lang || descriptor.scriptSetup?.lang\n  const isTS = testTs(scriptLang)\n\n  const expressionPlugins: CompilerOptions['expressionPlugins'] = []\n  if (isTS) {\n    expressionPlugins.push('typescript')\n  }\n\n  const scriptResult = compileScript(descriptor, {\n    inlineTemplate: isUseInlineTemplate(descriptor),\n    ...{\n      ...templateOptions,\n      expressionPlugins,\n    },\n  })\n\n  if (isTS) {\n    scriptResult.content = transformTS(scriptResult.content)\n  }\n\n  return {\n    template: templateResult,\n    script: scriptResult,\n  }\n}\n","import type { ComponentPropsOptions, SetupContext } from '@vue/runtime-core'\n\nimport type {\n  InputProps,\n  LooseRequiredRenderComponentInputProps,\n  RenderComponentInputComponent,\n  ResolveRenderComponentInputProps,\n} from '../types'\n\nimport { toMarkdown } from '@velin-dev/utils/to-md'\nimport { toValue } from '@vue/reactivity'\nimport { renderToString } from '@vue/server-renderer'\nimport { createSSRApp } from 'vue'\n\ntype SetupFunction<Props> = (\n  props: LooseRequiredRenderComponentInputProps<Props>,\n  ctx: SetupContext,\n) => unknown\n\nexport function onlySetup<\n  RawProps = any,\n  ComponentProps = ComponentPropsOptions<RawProps>,\n  ResolvedProps = ResolveRenderComponentInputProps<RawProps, ComponentProps>,\n>(\n  promptComponent: RenderComponentInputComponent<ResolvedProps>,\n  props: InputProps<ResolvedProps>,\n) {\n  // WORKAROUND: Vue's DefineComponent setup type includes internal mixin props that TS cannot prove equivalent here.\n  const setup = promptComponent.setup as SetupFunction<ResolvedProps> | undefined\n\n  return setup?.(\n    toValue(props) as unknown as LooseRequiredRenderComponentInputProps<ResolvedProps>,\n    { attrs: {}, slots: {}, emit: () => { }, expose: () => { } },\n  )\n}\n\nexport function onlyRender<\n  RawProps = any,\n  ComponentProps = ComponentPropsOptions<RawProps>,\n  ResolvedProps = ResolveRenderComponentInputProps<RawProps, ComponentProps>,\n>(\n  promptComponent: RenderComponentInputComponent<ResolvedProps>,\n  props: InputProps<ResolvedProps>,\n) {\n  return createSSRApp(promptComponent, toValue(props) as Record<string, unknown>)\n}\n\nexport function renderComponent<\n  RawProps = any,\n  ComponentProps = ComponentPropsOptions<RawProps>,\n  ResolvedProps = ResolveRenderComponentInputProps<RawProps, ComponentProps>,\n>(\n  promptComponent: RenderComponentInputComponent<ResolvedProps>,\n  props: InputProps<ResolvedProps>,\n) {\n  return new Promise<string>((resolve, reject) => {\n    renderToString(onlyRender(promptComponent, props))\n      .then(toMarkdown)\n      .then(resolve)\n      .catch(reject)\n  })\n}\n","import type { App, ComponentInternalInstance } from 'vue'\n\nimport type { InputProps, RenderComponentInputComponent } from '../types'\n\nexport interface ComponentPropText {\n  type: 'string'\n  value?: string\n  required?: boolean\n}\n\nexport interface ComponentPropBool {\n  type: 'boolean'\n  value?: boolean\n  required?: boolean\n}\n\nexport interface ComponentPropNumber {\n  type: 'number'\n  value?: number\n  required?: boolean\n}\n\nexport interface ComponentPropUnknown {\n  type: 'unknown'\n  value?: unknown\n  required?: boolean\n}\n\nexport interface ComponentPropArray {\n  type: 'array'\n  value?: unknown[]\n  required?: boolean\n}\n\nexport type ComponentProp = (ComponentPropText | ComponentPropBool | ComponentPropNumber | ComponentPropArray | ComponentPropUnknown) & {\n  title: string\n  key: string\n  required?: boolean\n}\n\nfunction willTurnIntoNumber(value: unknown): boolean {\n  if (value === Number) {\n    return true\n  }\n\n  // it is possible value is { type: Number() }\n  if (typeof value === 'object' && value !== null && 'type' in value) {\n    // check if value.type is Number()\n    if (typeof (value as { type: unknown }).type === 'function' && (value as { type: unknown }).type === Number) {\n      return true\n    }\n  }\n\n  return false\n}\n\nfunction willTurnIntoBoolean(value: unknown): boolean {\n  if (value === Boolean) {\n    return true\n  }\n\n  // it is possible value is { type: Boolean() }\n  if (typeof value === 'object' && value !== null && 'type' in value) {\n    // check if value.type is Boolean()\n    if (typeof (value as { type: unknown }).type === 'function' && (value as { type: unknown }).type === Boolean) {\n      return true\n    }\n  }\n\n  return false\n}\n\nfunction willTurnIntoString(value: unknown): boolean {\n  if (value === String) {\n    return true\n  }\n\n  // it is possible value is { type: String() }\n  if (typeof value === 'object' && value !== null && 'type' in value) {\n    // check if value.type is String()\n    if (typeof (value as { type: unknown }).type === 'function' && (value as { type: unknown }).type === String) {\n      return true\n    }\n  }\n\n  return false\n}\n\nfunction willTurnIntoArray(value: unknown): boolean {\n  if (value === Array) {\n    return true\n  }\n\n  // it is possible value is { type: Array() }\n  if (typeof value === 'object' && value !== null && 'type' in value) {\n    // check if value.type is Array()\n    if (typeof (value as { type: unknown }).type === 'function' && (value as { type: unknown }).type === Array) {\n      return true\n    }\n  }\n\n  return false\n}\n\nfunction inferType(\n  propDef:\n    | {\n    // eslint-disable-next-line ts/no-unsafe-function-type\n      type: Function\n    }\n    | typeof String\n    | typeof Number\n    | typeof Boolean\n    | typeof Array\n    | unknown,\n) {\n  let type: 'unknown' | 'string' | 'number' | 'boolean' | 'array' = 'unknown'\n  if (willTurnIntoString(propDef)) {\n    type = 'string'\n  }\n  else if (willTurnIntoNumber(propDef)) {\n    type = 'number'\n  }\n  else if (willTurnIntoBoolean(propDef)) {\n    type = 'boolean'\n  }\n  else if (willTurnIntoArray(propDef)) {\n    type = 'array'\n  }\n  return type\n}\n\n/**\n * @see https://github.com/vuejs/devtools/blob/e7dffa24fe98b212404a1451818b6c66739f88ee/packages/devtools-kit/src/core/component/state/process.ts#L62\n * @see https://github.com/vuejs/devtools/blob/e7dffa24fe98b212404a1451818b6c66739f88ee/packages/devtools-kit/src/core/app/index.ts#L14\n *\n * @param component\n */\nexport function resolveProps(component: RenderComponentInputComponent<any> | App<any>): ComponentProp[] {\n  if (component._component && component._component.props && typeof component._component.props === 'object') {\n    return Object.entries(component._component.props).map(([key, propDef]) => {\n      return {\n        key,\n        title: key,\n        type: inferType(propDef),\n        required: propDef != null && typeof propDef === 'object' && 'required' in propDef ? (propDef as { required?: boolean }).required : false,\n      }\n    })\n  }\n  else if ((component as unknown as ComponentInternalInstance).props && typeof (component as unknown as ComponentInternalInstance).props === 'object') {\n    return Object.entries((component as unknown as ComponentInternalInstance).props).map(([key, propDef]) => {\n      return {\n        key,\n        title: key,\n        type: inferType(propDef),\n        required: propDef != null && typeof propDef === 'object' && 'required' in propDef ? (propDef as { required?: boolean }).required : false,\n      }\n    })\n  }\n  else {\n    return []\n  }\n}\n\nexport function normalizeProps<RawProps = any>(resolvedProps: ComponentProp[], data?: InputProps<RawProps> | null): InputProps<RawProps> {\n  const normalizedData: InputProps<RawProps> = data || {} as InputProps<RawProps>\n\n  resolvedProps.forEach((prop) => {\n    if (prop.required) {\n      const keyData = prop.key as keyof typeof normalizedData\n\n      if (!normalizedData || normalizedData[keyData] == null) {\n        if (prop.type === 'string') {\n          ;(normalizedData[keyData] as any) = ''\n        }\n        else if (prop.type === 'boolean') {\n          ;(normalizedData[keyData] as any) = false\n        }\n        else if (prop.type === 'number') {\n          ;(normalizedData[keyData] as any) = 0\n        }\n        else if (prop.type === 'array') {\n          ;(normalizedData[keyData] as any) = []\n        }\n      }\n    }\n  })\n\n  return normalizedData\n}\n"],"mappings":";;;;;;;AAKA,SAAgB,oBACd,YACS;AACT,QACE,CAAC,CAAC,WAAW,eACV,CAAC,WAAW,UAAU;;;;ACI7B,eAAsB,WAAW,QAAyC;CACxE,MAAM,EAAE,eAAe,MAAM,OAAO;AAEpC,KAAI,CAAC,WAAW,SACd,QAAO,MAAM,WAAW,GAAG,OAAO,gCAAgC;CAGpE,MAAM,kBAAkB;EACtB,QAAQ,WAAW,SAAS;EAC5B,UAAU;EACV,IAAI,iBAAiB,KAAK,KAAK;EAC/B,iBAAiB,EAAE,mBAAmB,OAAO;EAC9C;CAED,MAAM,iBAAiB,gBAAgB,gBAAgB;CAGvD,MAAM,OAAO,OADM,WAAW,QAAQ,QAAQ,WAAW,aAAa,KACvC;CAE/B,MAAM,oBAA0D,EAAE;AAClE,KAAI,KACF,mBAAkB,KAAK,aAAa;CAGtC,MAAM,eAAe,cAAc,YAAY;EAC7C,gBAAgB,oBAAoB,WAAW;EAE7C,GAAG;EACH;EAEH,CAAC;AAEF,KAAI,KACF,cAAa,UAAU,YAAY,aAAa,QAAQ;AAG1D,QAAO;EACL,UAAU;EACV,QAAQ;EACT;;;;AClCH,SAAgB,UAKd,iBACA,OACA;CAEA,MAAM,QAAQ,gBAAgB;AAE9B,QAAO,QACL,QAAQ,MAAM,EACd;EAAE,OAAO,EAAE;EAAE,OAAO,EAAE;EAAE,YAAY;EAAK,cAAc;EAAK,CAC7D;;AAGH,SAAgB,WAKd,iBACA,OACA;AACA,QAAO,aAAa,iBAAiB,QAAQ,MAAM,CAA4B;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