| 15 | } from "./builders/index.js"; |
| 16 | |
| 17 | function flattenDoc(doc) { |
| 18 | if (!doc) { |
| 19 | return ""; |
| 20 | } |
| 21 | |
| 22 | if (Array.isArray(doc)) { |
| 23 | const res = []; |
| 24 | for (const part of doc) { |
| 25 | if (Array.isArray(part)) { |
| 26 | res.push(...flattenDoc(part)); |
| 27 | } else { |
| 28 | const flattened = flattenDoc(part); |
| 29 | if (flattened !== "") { |
| 30 | res.push(flattened); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return res; |
| 36 | } |
| 37 | |
| 38 | if (doc.type === DOC_TYPE_IF_BREAK) { |
| 39 | return { |
| 40 | ...doc, |
| 41 | breakContents: flattenDoc(doc.breakContents), |
| 42 | flatContents: flattenDoc(doc.flatContents), |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | if (doc.type === DOC_TYPE_GROUP) { |
| 47 | return { |
| 48 | ...doc, |
| 49 | contents: flattenDoc(doc.contents), |
| 50 | expandedStates: doc.expandedStates?.map(flattenDoc), |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | if (doc.type === DOC_TYPE_FILL) { |
| 55 | return { type: "fill", parts: doc.parts.map(flattenDoc) }; |
| 56 | } |
| 57 | |
| 58 | if (doc.contents) { |
| 59 | return { ...doc, contents: flattenDoc(doc.contents) }; |
| 60 | } |
| 61 | |
| 62 | return doc; |
| 63 | } |
| 64 | |
| 65 | function printDocToDebug(doc) { |
| 66 | /** @type Record<symbol, string> */ |