{"version":3,"file":"convert.mjs","names":[],"sources":["../../src/function/convert.ts"],"sourcesContent":["import type {StylePropertySpecification} from '..';\n\nfunction convertLiteral(value) {\n    return typeof value === 'object' ? ['literal', value] : value;\n}\n\nexport function convertFunction(parameters: any, propertySpec: StylePropertySpecification) {\n    let stops = parameters.stops;\n    if (!stops) {\n        // identity function\n        return convertIdentityFunction(parameters, propertySpec);\n    }\n\n    const zoomAndFeatureDependent = stops && typeof stops[0][0] === 'object';\n    const featureDependent = zoomAndFeatureDependent || parameters.property !== undefined;\n    const zoomDependent = zoomAndFeatureDependent || !featureDependent;\n\n    stops = stops.map((stop) => {\n        if (!featureDependent && (propertySpec as any).tokens && typeof stop[1] === 'string') {\n            return [stop[0], convertTokenString(stop[1])];\n        }\n        return [stop[0], convertLiteral(stop[1])];\n    });\n\n    if (zoomAndFeatureDependent) {\n        return convertZoomAndPropertyFunction(parameters, propertySpec, stops);\n    } else if (zoomDependent) {\n        return convertZoomFunction(parameters, propertySpec, stops);\n    } else {\n        return convertPropertyFunction(parameters, propertySpec, stops);\n    }\n}\n\nfunction convertIdentityFunction(parameters, propertySpec): Array<unknown> {\n    const get = ['get', parameters.property];\n\n    if (parameters.default === undefined) {\n        // By default, expressions for string-valued properties get coerced. To preserve\n        // legacy function semantics, insert an explicit assertion instead.\n        return propertySpec.type === 'string' ? ['string', get] : get;\n    } else if (propertySpec.type === 'enum') {\n        return ['match', get, Object.keys(propertySpec.values), get, parameters.default];\n    } else {\n        const expression = [\n            propertySpec.type === 'color' ? 'to-color' : propertySpec.type,\n            get,\n            convertLiteral(parameters.default)\n        ];\n        if (propertySpec.type === 'array') {\n            expression.splice(1, 0, propertySpec.value, propertySpec.length || null);\n        }\n        return expression;\n    }\n}\n\nfunction getInterpolateOperator(parameters) {\n    switch (parameters.colorSpace) {\n        case 'hcl':\n            return 'interpolate-hcl';\n        case 'lab':\n            return 'interpolate-lab';\n        case 'oklch':\n            return 'interpolate-oklch';\n        case 'oklab':\n            return 'interpolate-oklab';\n        default:\n            return 'interpolate';\n    }\n}\n\nfunction convertZoomAndPropertyFunction(parameters, propertySpec, stops) {\n    const featureFunctionParameters = {};\n    const featureFunctionStops = {};\n    const zoomStops = [];\n    for (let s = 0; s < stops.length; s++) {\n        const stop = stops[s];\n        const zoom = stop[0].zoom;\n        if (featureFunctionParameters[zoom] === undefined) {\n            featureFunctionParameters[zoom] = {\n                zoom,\n                type: parameters.type,\n                property: parameters.property,\n                default: parameters.default,\n                colorSpace: parameters.colorSpace\n            };\n            featureFunctionStops[zoom] = [];\n            zoomStops.push(zoom);\n        }\n        featureFunctionStops[zoom].push([stop[0].value, stop[1]]);\n    }\n\n    // the interpolation type for the zoom dimension of a zoom-and-property\n    // function is determined directly from the style property specification\n    // for which it's being used: linear for interpolatable properties, step\n    // otherwise.\n    const functionType = getFunctionType({}, propertySpec);\n    if (functionType === 'exponential') {\n        const expression = [getInterpolateOperator(parameters), ['linear'], ['zoom']];\n\n        for (const z of zoomStops) {\n            const output = convertPropertyFunction(\n                featureFunctionParameters[z],\n                propertySpec,\n                featureFunctionStops[z]\n            );\n            appendStopPair(expression, z, output, false);\n        }\n\n        return expression;\n    } else {\n        const expression = ['step', ['zoom']];\n\n        for (const z of zoomStops) {\n            const output = convertPropertyFunction(\n                featureFunctionParameters[z],\n                propertySpec,\n                featureFunctionStops[z]\n            );\n            appendStopPair(expression, z, output, true);\n        }\n\n        fixupDegenerateStepCurve(expression);\n\n        return expression;\n    }\n}\n\nfunction coalesce(a, b) {\n    if (a !== undefined) return a;\n    if (b !== undefined) return b;\n}\n\nfunction getFallback(parameters, propertySpec) {\n    const defaultValue = convertLiteral(coalesce(parameters.default, propertySpec.default));\n\n    /*\n     * Some fields with type: resolvedImage have an undefined default.\n     * Because undefined is an invalid value for resolvedImage, set fallback to\n     * an empty string instead of undefined to ensure output\n     * passes validation.\n     */\n    if (defaultValue === undefined && propertySpec.type === 'resolvedImage') {\n        return '';\n    }\n    return defaultValue;\n}\n\nfunction convertPropertyFunction(parameters, propertySpec, stops) {\n    const type = getFunctionType(parameters, propertySpec);\n    const get = ['get', parameters.property];\n    if (type === 'categorical' && typeof stops[0][0] === 'boolean') {\n        const expression: any = ['case'];\n        for (const stop of stops) {\n            expression.push(['==', get, stop[0]], stop[1]);\n        }\n\n        expression.push(getFallback(parameters, propertySpec));\n        return expression;\n    } else if (type === 'categorical') {\n        const expression = ['match', get];\n        for (const stop of stops) {\n            appendStopPair(expression, stop[0], stop[1], false);\n        }\n        expression.push(getFallback(parameters, propertySpec));\n        return expression;\n    } else if (type === 'interval') {\n        const expression = ['step', ['number', get]];\n        for (const stop of stops) {\n            appendStopPair(expression, stop[0], stop[1], true);\n        }\n        fixupDegenerateStepCurve(expression);\n        return parameters.default === undefined\n            ? expression\n            : [\n                  'case',\n                  ['==', ['typeof', get], 'number'],\n                  expression,\n                  convertLiteral(parameters.default)\n              ];\n    } else if (type === 'exponential') {\n        const base = parameters.base !== undefined ? parameters.base : 1;\n        const expression = [\n            getInterpolateOperator(parameters),\n            base === 1 ? ['linear'] : ['exponential', base],\n            ['number', get]\n        ];\n\n        for (const stop of stops) {\n            appendStopPair(expression, stop[0], stop[1], false);\n        }\n        return parameters.default === undefined\n            ? expression\n            : [\n                  'case',\n                  ['==', ['typeof', get], 'number'],\n                  expression,\n                  convertLiteral(parameters.default)\n              ];\n    } else {\n        throw new Error(`Unknown property function type ${type}`);\n    }\n}\n\nfunction convertZoomFunction(parameters, propertySpec, stops, input = ['zoom']) {\n    const type = getFunctionType(parameters, propertySpec);\n    let expression;\n    let isStep = false;\n    if (type === 'interval') {\n        expression = ['step', input];\n        isStep = true;\n    } else if (type === 'exponential') {\n        const base = parameters.base !== undefined ? parameters.base : 1;\n        expression = [\n            getInterpolateOperator(parameters),\n            base === 1 ? ['linear'] : ['exponential', base],\n            input\n        ];\n    } else {\n        throw new Error(`Unknown zoom function type \"${type}\"`);\n    }\n\n    for (const stop of stops) {\n        appendStopPair(expression, stop[0], stop[1], isStep);\n    }\n\n    fixupDegenerateStepCurve(expression);\n\n    return expression;\n}\n\nfunction fixupDegenerateStepCurve(expression) {\n    // degenerate step curve (i.e. a constant function): add a noop stop\n    if (expression[0] === 'step' && expression.length === 3) {\n        expression.push(0);\n        expression.push(expression[2]);\n    }\n}\n\nfunction appendStopPair(curve, input, output, isStep) {\n    // Skip duplicate stop values. They were not validated for functions, but they are for expressions.\n    // https://github.com/mapbox/mapbox-gl-js/issues/4107\n    if (curve.length > 3 && input === curve[curve.length - 2]) {\n        return;\n    }\n    // step curves don't get the first input value, as it is redundant.\n    if (!(isStep && curve.length === 2)) {\n        curve.push(input);\n    }\n    curve.push(output);\n}\n\nfunction getFunctionType(parameters, propertySpec) {\n    if (parameters.type) {\n        return parameters.type;\n    } else {\n        return (propertySpec.expression as any).interpolated ? 'exponential' : 'interval';\n    }\n}\n\n// \"String with {name} token\" => [\"concat\", \"String with \", [\"get\", \"name\"], \" token\"]\nexport function convertTokenString(s: string) {\n    const result: any = ['concat'];\n    const re = /{([^{}]+)}/g;\n    let pos = 0;\n    for (let match = re.exec(s); match !== null; match = re.exec(s)) {\n        const literal = s.slice(pos, re.lastIndex - match[0].length);\n        pos = re.lastIndex;\n        if (literal.length > 0) result.push(literal);\n        result.push(['get', match[1]]);\n    }\n\n    if (result.length === 1) {\n        return s;\n    }\n\n    if (pos < s.length) {\n        result.push(s.slice(pos));\n    } else if (result.length === 2) {\n        return ['to-string', result[1]];\n    }\n\n    return result;\n}\n"],"mappings":";AAEA,SAAS,eAAe,OAAO;CAC3B,OAAO,OAAO,UAAU,WAAW,CAAC,WAAW,KAAK,IAAI;AAC5D;AAEA,SAAgB,gBAAgB,YAAiB,cAA0C;CACvF,IAAI,QAAQ,WAAW;CACvB,IAAI,CAAC,OAED,OAAO,wBAAwB,YAAY,YAAY;CAG3D,MAAM,0BAA0B,SAAS,OAAO,MAAM,EAAE,CAAC,OAAO;CAChE,MAAM,mBAAmB,2BAA2B,WAAW,aAAa,KAAA;CAC5E,MAAM,g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