|
| 1 | +import { isRenderUseBrick, type ParsedModule } from "@next-tsx/parser"; |
| 2 | +import { isObject } from "@next-core/utils/general"; |
| 3 | +import type { BrickConf } from "@next-core/types"; |
| 4 | +import type { ConvertOptions, ConvertState } from "./interfaces.js"; |
| 5 | +import { convertComponent } from "./convertComponent.js"; |
| 6 | +import { deepReplaceVariables } from "./deepReplaceVariables.js"; |
| 7 | + |
| 8 | +export async function convertProperties( |
| 9 | + properties: Record<string, unknown>, |
| 10 | + mod: ParsedModule, |
| 11 | + state: ConvertState, |
| 12 | + options: ConvertOptions, |
| 13 | + scope: "page" | "view" | "template" |
| 14 | +) { |
| 15 | + const convertPropValue = async (value: unknown): Promise<unknown> => { |
| 16 | + if (Array.isArray(value)) { |
| 17 | + return Promise.all(value.map((item) => convertPropValue(item))); |
| 18 | + } |
| 19 | + |
| 20 | + if (isObject(value)) { |
| 21 | + return Object.fromEntries( |
| 22 | + await Promise.all( |
| 23 | + Object.entries(value).map(async ([k, v]) => { |
| 24 | + if (k === "render" && isObject(v) && isRenderUseBrick(v)) { |
| 25 | + const patterns = new Map<string, string>(); |
| 26 | + if (v.params.length > 0) { |
| 27 | + patterns.set(v.params[0], "DATA"); |
| 28 | + } |
| 29 | + const useBrick = ( |
| 30 | + await Promise.all( |
| 31 | + v.children.map( |
| 32 | + (child) => |
| 33 | + convertComponent( |
| 34 | + child, |
| 35 | + mod, |
| 36 | + state, |
| 37 | + options, |
| 38 | + scope |
| 39 | + ) as Promise<BrickConf | BrickConf[]> |
| 40 | + ) |
| 41 | + ) |
| 42 | + ).flatMap((child) => deepReplaceVariables(child, patterns)); |
| 43 | + return ["useBrick", useBrick]; |
| 44 | + } |
| 45 | + return [k, await convertPropValue(v)]; |
| 46 | + }) |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + return value; |
| 52 | + }; |
| 53 | + |
| 54 | + const props = (await convertPropValue(properties)) as Record<string, unknown>; |
| 55 | + |
| 56 | + return props; |
| 57 | +} |
0 commit comments