{"version":3,"file":"coalesce.mjs","names":[],"sources":["../../../src/expression/definitions/coalesce.ts"],"sourcesContent":["import {checkSubtype, ValueType} from '../types';\nimport {ResolvedImage} from '../types/resolved_image';\n\nimport type {Expression} from '../expression';\nimport type {ParsingContext} from '../parsing_context';\nimport type {EvaluationContext} from '../evaluation_context';\nimport type {Type} from '../types';\n\nexport class Coalesce implements Expression {\n    type: Type;\n    args: Array<Expression>;\n\n    constructor(type: Type, args: Array<Expression>) {\n        this.type = type;\n        this.args = args;\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        let outputType: Type = null;\n        const expectedType = context.expectedType;\n        if (expectedType && expectedType.kind !== 'value') {\n            outputType = expectedType;\n        }\n        const parsedArgs = [];\n\n        for (const arg of args.slice(1)) {\n            const parsed = context.parse(arg, 1 + parsedArgs.length, outputType, undefined, {\n                typeAnnotation: 'omit'\n            });\n            if (!parsed) return null;\n            outputType = outputType || parsed.type;\n            parsedArgs.push(parsed);\n        }\n        if (!outputType) throw new Error('No output type');\n\n        // Above, we parse arguments without inferred type annotation so that\n        // they don't produce a runtime error for `null` input, which would\n        // preempt the desired null-coalescing behavior.\n        // Thus, if any of our arguments would have needed an annotation, we\n        // need to wrap the enclosing coalesce expression with it instead.\n        const needsAnnotation =\n            expectedType && parsedArgs.some((arg) => checkSubtype(expectedType, arg.type));\n\n        return needsAnnotation\n            ? new Coalesce(ValueType, parsedArgs)\n            : new Coalesce(outputType as any, parsedArgs);\n    }\n\n    evaluate(ctx: EvaluationContext) {\n        let result = null;\n        let argCount = 0;\n        let requestedImageName;\n        for (const arg of this.args) {\n            argCount++;\n            result = arg.evaluate(ctx);\n            // we need to keep track of the first requested image in a coalesce statement\n            // if coalesce can't find a valid image, we return the first image name so styleimagemissing can fire\n            if (result && result instanceof ResolvedImage && !result.available) {\n                if (!requestedImageName) {\n                    requestedImageName = result.name;\n                }\n                result = null;\n                if (argCount === this.args.length) {\n                    result = requestedImageName;\n                }\n            }\n\n            if (result !== null) break;\n        }\n        return result;\n    }\n\n    eachChild(fn: (_: Expression) => void) {\n        this.args.forEach(fn);\n    }\n\n    outputDefined(): boolean {\n        return this.args.every((arg) => arg.outputDefined());\n    }\n}\n"],"mappings":";;;AAQA,IAAa,WAAb,MAAa,SAA+B;CAIxC,YAAY,MAAY,MAAyB;EAC7C,KAAK,OAAO;EACZ,KAAK,OAAO;CAChB;CAEA,OAAO,MAAM,MAA8B,SAAqC;EAC5E,IAAI,KAAK,SAAS,GACd,OAAO,QAAQ,MAAM,iCAAiC;EAE1D,IAAI,aAAmB;EACvB,MAAM,eAAe,QAAQ;EAC7B,IAAI,gBAAgB,aAAa,SAAS,SACtC,aAAa;EAEjB,MAAM,aAAa,CAAC;EAEpB,KAAK,MAAM,OAAO,KAAK,MAAM,CAAC,GAAG;GAC7B,MAAM,SAAS,QAAQ,MAAM,KAAK,IAAI,WAAW,QAAQ,YAAY,KAAA,GAAW,EAC5E,gBAAgB,OACpB,CAAC;GACD,IAAI,CAAC,QAAQ,OAAO;GACpB,aAAa,cAAc,OAAO;GAClC,WAAW,KAAK,MAAM;EAC1B;EACA,IAAI,CAAC,YAAY,MAAM,IAAI,MAAM,gBAAgB;EAUjD,OAFI,gBAAgB,WAAW,MAAM,QAAQ,aAAa,cAAc,IAAI,IAAI,CAAC,IAG3E,IAAI,SAAS,WAAW,UAAU,IAClC,IAAI,SAAS,YAAmB,UAAU;CACpD;CAEA,SAAS,KAAwB;EAC7B,IAAI,SAAS;EACb,IAAI,WAAW;EACf,IAAI;EACJ,KAAK,MAAM,OAAO,KAAK,MAAM;GACzB;GACA,SAAS,IAAI,SAAS,GAAG;GAGzB,IAAI,UAAU,kBAAkB,iBAAiB,CAAC,OAAO,WAAW;IAChE,IAAI,CAAC,oBACD,qBAAqB,OAAO;IAEhC,SAAS;IACT,IAAI,aAAa,KAAK,KAAK,QACvB,SAAS;GAEjB;GAEA,IAAI,WAAW,MAAM;EACzB;EACA,OAAO;CACX;CAEA,UAAU,IAA6B;EACnC,KAAK,KAAK,QAAQ,EAAE;CACxB;CAEA,gBAAyB;EACrB,OAAO,KAAK,KAAK,OAAO,QAAQ,IAAI,cAAc,CAAC;CACvD;AACJ"}