{"version":3,"file":"types.mjs","names":[],"sources":["../../src/expression/types.ts"],"sourcesContent":["export type NullTypeT = {\n    kind: 'null';\n};\nexport type NumberTypeT = {\n    kind: 'number';\n};\nexport type StringTypeT = {\n    kind: 'string';\n};\nexport type BooleanTypeT = {\n    kind: 'boolean';\n};\nexport type ColorTypeT = {\n    kind: 'color';\n};\nexport type ProjectionDefinitionTypeT = {\n    kind: 'projectionDefinition';\n};\nexport type ObjectTypeT = {\n    kind: 'object';\n};\nexport type ValueTypeT = {\n    kind: 'value';\n};\nexport type ErrorTypeT = {\n    kind: 'error';\n};\nexport type CollatorTypeT = {\n    kind: 'collator';\n};\nexport type FormattedTypeT = {\n    kind: 'formatted';\n};\nexport type PaddingTypeT = {\n    kind: 'padding';\n};\nexport type NumberArrayTypeT = {\n    kind: 'numberArray';\n};\nexport type ColorArrayTypeT = {\n    kind: 'colorArray';\n};\nexport type ResolvedImageTypeT = {\n    kind: 'resolvedImage';\n};\nexport type VariableAnchorOffsetCollectionTypeT = {\n    kind: 'variableAnchorOffsetCollection';\n};\n\nexport type EvaluationKind = 'constant' | 'source' | 'camera' | 'composite';\n\nexport type Type =\n    | NullTypeT\n    | NumberTypeT\n    | StringTypeT\n    | BooleanTypeT\n    | ColorTypeT\n    | ProjectionDefinitionTypeT\n    | ObjectTypeT\n    | ValueTypeT\n    | ArrayType\n    | ErrorTypeT\n    | CollatorTypeT\n    | FormattedTypeT\n    | PaddingTypeT\n    | NumberArrayTypeT\n    | ColorArrayTypeT\n    | ResolvedImageTypeT\n    | VariableAnchorOffsetCollectionTypeT;\n\nexport interface ArrayType<T extends Type = Type> {\n    kind: 'array';\n    itemType: T;\n    N: number;\n}\n\nexport type NativeType = 'number' | 'string' | 'boolean' | 'null' | 'array' | 'object';\n\nexport const NullType = {kind: 'null'} as NullTypeT;\nexport const NumberType = {kind: 'number'} as NumberTypeT;\nexport const StringType = {kind: 'string'} as StringTypeT;\nexport const BooleanType = {kind: 'boolean'} as BooleanTypeT;\nexport const ColorType = {kind: 'color'} as ColorTypeT;\nexport const ProjectionDefinitionType = {\n    kind: 'projectionDefinition'\n} as ProjectionDefinitionTypeT;\nexport const ObjectType = {kind: 'object'} as ObjectTypeT;\nexport const ValueType = {kind: 'value'} as ValueTypeT;\nexport const ErrorType = {kind: 'error'} as ErrorTypeT;\nexport const CollatorType = {kind: 'collator'} as CollatorTypeT;\nexport const FormattedType = {kind: 'formatted'} as FormattedTypeT;\nexport const PaddingType = {kind: 'padding'} as PaddingTypeT;\nexport const ColorArrayType = {kind: 'colorArray'} as ColorArrayTypeT;\nexport const NumberArrayType = {kind: 'numberArray'} as NumberArrayTypeT;\nexport const ResolvedImageType = {kind: 'resolvedImage'} as ResolvedImageTypeT;\nexport const VariableAnchorOffsetCollectionType = {\n    kind: 'variableAnchorOffsetCollection'\n} as VariableAnchorOffsetCollectionTypeT;\n\nexport function array<T extends Type>(itemType: T, N?: number | null): ArrayType<T> {\n    return {\n        kind: 'array',\n        itemType,\n        N\n    };\n}\n\nexport function typeToString(type: Type): string {\n    if (type.kind === 'array') {\n        const itemType = typeToString(type.itemType);\n        return typeof type.N === 'number'\n            ? `array<${itemType}, ${type.N}>`\n            : type.itemType.kind === 'value'\n              ? 'array'\n              : `array<${itemType}>`;\n    } else {\n        return type.kind;\n    }\n}\n\nconst valueMemberTypes = [\n    NullType,\n    NumberType,\n    StringType,\n    BooleanType,\n    ColorType,\n    ProjectionDefinitionType,\n    FormattedType,\n    ObjectType,\n    array(ValueType),\n    PaddingType,\n    NumberArrayType,\n    ColorArrayType,\n    ResolvedImageType,\n    VariableAnchorOffsetCollectionType\n];\n\n/**\n * Returns null if `t` is a subtype of `expected`; otherwise returns an\n * error message.\n * @private\n */\nexport function checkSubtype(expected: Type, t: Type): string {\n    if (t.kind === 'error') {\n        // Error is a subtype of every type\n        return null;\n    } else if (expected.kind === 'array') {\n        if (\n            t.kind === 'array' &&\n            ((t.N === 0 && t.itemType.kind === 'value') ||\n                !checkSubtype(expected.itemType, t.itemType)) &&\n            (typeof expected.N !== 'number' || expected.N === t.N)\n        ) {\n            return null;\n        }\n    } else if (expected.kind === t.kind) {\n        return null;\n    } else if (expected.kind === 'value') {\n        for (const memberType of valueMemberTypes) {\n            if (!checkSubtype(memberType, t)) {\n                return null;\n            }\n        }\n    }\n\n    return `Expected ${typeToString(expected)} but found ${typeToString(t)} instead.`;\n}\n\nexport function isValidType(provided: Type, allowedTypes: Array<Type>): boolean {\n    return allowedTypes.some((t) => t.kind === provided.kind);\n}\n\nexport function isValidNativeType(provided: any, allowedTypes: Array<NativeType>): boolean {\n    return allowedTypes.some((t) => {\n        if (t === 'null') {\n            return provided === null;\n        } else if (t === 'array') {\n            return Array.isArray(provided);\n        } else if (t === 'object') {\n            return provided && !Array.isArray(provided) && typeof provided === 'object';\n        } else {\n            return t === typeof provided;\n        }\n    });\n}\n\n/**\n * Verify whether the specified type is of the same type as the specified sample.\n *\n * @param provided Type to verify\n * @param sample Sample type to reference\n * @returns `true` if both objects are of the same type, `false` otherwise\n * @example basic types\n * if (verifyType(outputType, ValueType)) {\n *     // type narrowed to:\n *     outputType.kind; // 'value'\n * }\n * @example array types\n * if (verifyType(outputType, array(NumberType))) {\n *     // type narrowed to:\n *     outputType.kind; // 'array'\n *     outputType.itemType; // NumberTypeT\n *     outputType.itemType.kind; // 'number'\n * }\n */\nexport function verifyType<T extends Type>(provided: Type, sample: T): provided is T {\n    if (provided.kind === 'array' && sample.kind === 'array') {\n        return provided.itemType.kind === sample.itemType.kind && typeof provided.N === 'number';\n    }\n    return provided.kind === sample.kind;\n}\n"],"mappings":";AA8EA,MAAa,WAAW,EAAC,MAAM,OAAM;AACrC,MAAa,aAAa,EAAC,MAAM,SAAQ;AACzC,MAAa,aAAa,EAAC,MAAM,SAAQ;AACzC,MAAa,cAAc,EAAC,MAAM,UAAS;AAC3C,MAAa,YAAY,EAAC,MAAM,QAAO;AACvC,MAAa,2BAA2B,EACpC,MAAM,uBACV;AACA,MAAa,aAAa,EAAC,MA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