{"version":3,"file":"format.mjs","names":[],"sources":["../../../src/expression/definitions/format.ts"],"sourcesContent":["import {\n    NumberType,\n    ValueType,\n    FormattedType,\n    array,\n    StringType,\n    ColorType,\n    ResolvedImageType\n} from '../types';\nimport {\n    Formatted,\n    FormattedSection,\n    VERTICAL_ALIGN_OPTIONS,\n    VerticalAlign\n} from '../types/formatted';\nimport {valueToString, typeOf} from '../values';\n\nimport type {Expression} from '../expression';\nimport type {EvaluationContext} from '../evaluation_context';\nimport type {ParsingContext} from '../parsing_context';\nimport type {Type} from '../types';\n\ntype FormattedSectionExpression = {\n    // Content of a section may be Image expression or other\n    // type of expression that is coercable to 'string'.\n    content: Expression;\n    scale: Expression | null;\n    font: Expression | null;\n    textColor: Expression | null;\n    verticalAlign: Expression | null;\n};\n\nexport class FormatExpression implements Expression {\n    type: Type;\n    sections: Array<FormattedSectionExpression>;\n\n    constructor(sections: Array<FormattedSectionExpression>) {\n        this.type = FormattedType;\n        this.sections = sections;\n    }\n\n    static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression {\n        if (args.length < 2) {\n            return context.error('Expected at least one argument.') as null;\n        }\n\n        const firstArg = args[1];\n        if (!Array.isArray(firstArg) && typeof firstArg === 'object') {\n            return context.error('First argument must be an image or text section.') as null;\n        }\n\n        const sections: Array<FormattedSectionExpression> = [];\n        let nextTokenMayBeObject = false;\n        for (let i = 1; i <= args.length - 1; ++i) {\n            const arg = args[i] as any;\n\n            if (nextTokenMayBeObject && typeof arg === 'object' && !Array.isArray(arg)) {\n                nextTokenMayBeObject = false;\n\n                let scale = null;\n                if (arg['font-scale']) {\n                    scale = context.parse(arg['font-scale'], 1, NumberType);\n                    if (!scale) return null;\n                }\n\n                let font = null;\n                if (arg['text-font']) {\n                    font = context.parse(arg['text-font'], 1, array(StringType));\n                    if (!font) return null;\n                }\n\n                let textColor = null;\n                if (arg['text-color']) {\n                    textColor = context.parse(arg['text-color'], 1, ColorType);\n                    if (!textColor) return null;\n                }\n\n                let verticalAlign = null;\n                if (arg['vertical-align']) {\n                    if (\n                        typeof arg['vertical-align'] === 'string' &&\n                        !VERTICAL_ALIGN_OPTIONS.includes(arg['vertical-align'] as VerticalAlign)\n                    ) {\n                        return context.error(\n                            `'vertical-align' must be one of: 'bottom', 'center', 'top' but found '${arg['vertical-align']}' instead.`\n                        ) as null;\n                    }\n\n                    verticalAlign = context.parse(arg['vertical-align'], 1, StringType);\n                    if (!verticalAlign) return null;\n                }\n\n                const lastExpression = sections[sections.length - 1];\n                lastExpression.scale = scale;\n                lastExpression.font = font;\n                lastExpression.textColor = textColor;\n                lastExpression.verticalAlign = verticalAlign;\n            } else {\n                const content = context.parse(args[i], 1, ValueType);\n                if (!content) return null;\n\n                const kind = content.type.kind;\n                if (\n                    kind !== 'string' &&\n                    kind !== 'value' &&\n                    kind !== 'null' &&\n                    kind !== 'resolvedImage'\n                )\n                    return context.error(\n                        \"Formatted text type must be 'string', 'value', 'image' or 'null'.\"\n                    ) as null;\n\n                nextTokenMayBeObject = true;\n                sections.push({\n                    content,\n                    scale: null,\n                    font: null,\n                    textColor: null,\n                    verticalAlign: null\n                });\n            }\n        }\n\n        return new FormatExpression(sections);\n    }\n\n    evaluate(ctx: EvaluationContext) {\n        const evaluateSection = (section) => {\n            const evaluatedContent = section.content.evaluate(ctx);\n            if (typeOf(evaluatedContent) === ResolvedImageType) {\n                return new FormattedSection(\n                    '',\n                    evaluatedContent,\n                    null,\n                    null,\n                    null,\n                    section.verticalAlign ? section.verticalAlign.evaluate(ctx) : null\n                );\n            }\n\n            return new FormattedSection(\n                valueToString(evaluatedContent),\n                null,\n                section.scale ? section.scale.evaluate(ctx) : null,\n                section.font ? section.font.evaluate(ctx).join(',') : null,\n                section.textColor ? section.textColor.evaluate(ctx) : null,\n                section.verticalAlign ? section.verticalAlign.evaluate(ctx) : null\n            );\n        };\n\n        return new Formatted(this.sections.map(evaluateSection));\n    }\n\n    eachChild(fn: (_: Expression) => void) {\n        for (const section of this.sections) {\n            fn(section.content);\n            if (section.scale) {\n                fn(section.scale);\n            }\n            if (section.font) {\n                fn(section.font);\n            }\n            if (section.textColor) {\n                fn(section.textColor);\n            }\n            if (section.verticalAlign) {\n                fn(section.verticalAlign);\n            }\n        }\n    }\n\n    outputDefined() {\n        // Technically the combinatoric set of all children\n        // Usually, this.text will be undefined anyway\n        return false;\n    }\n}\n"],"mappings":";;;;AAgCA,IAAa,mBAAb,MAAa,iBAAuC;CAIhD,YAAY,UAA6C;EACrD,KAAK,OAAO;EACZ,KAAK,WAAW;CACpB;CAEA,OAAO,MAAM,MAA8B,SAAqC;EAC5E,IAAI,KAAK,SAAS,GACd,OAAO,QAAQ,MAAM,iCAAiC;EAG1D,MAAM,WAAW,KAAK;EACtB,IAAI,CAAC,MAAM,QAAQ,QAAQ,KAAK,OAAO,aAAa,UAChD,OAAO,QAAQ,MAAM,kDAAkD;EAG3E,MAAM,WAA8C,CAAC;EACrD,IAAI,uBAAuB;EAC3B,KAAK,IAAI,IAAI,GAAG,KAAK,KAAK,SAAS,GAAG,EAAE,GAAG;GACvC,MAAM,MAAM,KAAK;GAEjB,IAAI,wBAAwB,OAAO,QAAQ,YAAY,CAAC,MAAM,QAAQ,GAAG,GAAG;IACxE,uBAAuB;IAEvB,IAAI,QAAQ;IACZ,IAAI,IAAI,eAAe;KACnB,QAAQ,QAAQ,MAAM,IAAI,eAAe,GAAG,UAAU;KACtD,IAAI,CAAC,OAAO,OAAO;IACvB;IAEA,IAAI,OAAO;IACX,IAAI,IAAI,cAAc;KAClB,OAAO,QAAQ,MAAM,IAAI,cAAc,GAAG,MAAM,UAAU,CAAC;KAC3D,IAAI,CAAC,MAAM,OAAO;IACtB;IAEA,IAAI,YAAY;IAChB,IAAI,IAAI,eAAe;KACnB,YAAY,QAAQ,MAAM,IAAI,eAAe,GAAG,SAAS;KACzD,IAAI,CAAC,WAAW,OAAO;IAC3B;IAEA,IAAI,gBAAgB;IACpB,IAAI,IAAI,mBAAmB;KACvB,IACI,OAAO,IAAI,s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