{"version":3,"file":"formatTime.mjs","sources":["../../../../src/transformations/transformers/formatTime.ts"],"sourcesContent":["import { map } from 'rxjs/operators';\n\nimport { TimeZone } from '@grafana/schema';\n\nimport { cacheFieldDisplayNames } from '../../field/fieldState';\nimport { DataFrame } from '../../types/dataFrame';\nimport {\n  DataTransformContext,\n  DataTransformerInfo,\n  TransformationApplicabilityLevels,\n} from '../../types/transformations';\n\nimport { fieldToStringField } from './convertFieldType';\nimport { DataTransformerID } from './ids';\n\nexport interface FormatTimeTransformerOptions {\n  timeField: string;\n  outputFormat: string;\n  timezone: TimeZone;\n}\n\nexport const formatTimeTransformer: DataTransformerInfo<FormatTimeTransformerOptions> = {\n  id: DataTransformerID.formatTime,\n  name: 'Format time',\n  description: 'Set the output format of a time field',\n  defaultOptions: { timeField: '', outputFormat: '', useTimezone: true },\n  isApplicable: (data: DataFrame[]) => {\n    // Search for a time field\n    // if there is one then we can use this transformation\n    for (const frame of data) {\n      for (const field of frame.fields) {\n        if (field.type === 'time') {\n          return TransformationApplicabilityLevels.Applicable;\n        }\n      }\n    }\n\n    return TransformationApplicabilityLevels.NotApplicable;\n  },\n  isApplicableDescription:\n    'The Format time transformation requires a time field to work. No time field could be found.',\n  operator: (options, ctx) => (source) =>\n    source.pipe(\n      map((data) => {\n        return applyFormatTime(options, data, ctx);\n      })\n    ),\n};\n\n/**\n * @internal\n */\nexport const applyFormatTime = (\n  { timeField, outputFormat, timezone }: FormatTimeTransformerOptions,\n  data: DataFrame[],\n  ctx?: DataTransformContext\n) => {\n  if (!Array.isArray(data) || data.length === 0) {\n    return data;\n  }\n\n  cacheFieldDisplayNames(data);\n\n  outputFormat = ctx?.interpolate(outputFormat) ?? outputFormat;\n\n  return data.map((frame) => ({\n    ...frame,\n    fields: frame.fields.map((field) => {\n      if (field.state?.displayName === timeField) {\n        field = fieldToStringField(field, outputFormat, { timeZone: timezone });\n      }\n\n      return field;\n    }),\n  }));\n};\n"],"names":["_a"],"mappings":";;;;;;;AAqBO,MA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