{"version":3,"file":"MatcherScopeSelector.cjs","sources":["../../../../src/components/MatchersUI/MatcherScopeSelector.tsx"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { type SelectableValue } from '@grafana/data';\nimport { type MatcherScope } from '@grafana/schema';\n\nimport { RadioButtonGroup, type RadioButtonGroupProps } from '../Forms/RadioButtonGroup/RadioButtonGroup';\n\nimport { getGroupDescriptionForScope, getGroupLabelForScope } from './utils';\n\nexport interface MatcherScopeSelectorProps extends Omit<RadioButtonGroupProps<MatcherScope>, 'options'> {\n  scopes: Set<MatcherScope>;\n  allowedScopes?: MatcherScope[];\n}\n\nexport function buildScopeOptions(\n  providedUniqScopes: Set<MatcherScope>,\n  currentScope?: MatcherScope,\n  allowedScopes: MatcherScope[] = Array.from(providedUniqScopes)\n): Array<SelectableValue<MatcherScope>> {\n  const uniqScopes = new Set(providedUniqScopes);\n\n  // we remove series from the list to then add it at the beginning of the returned options.\n  if (allowedScopes) {\n    uniqScopes.forEach((scope) => {\n      if (!allowedScopes.includes(scope)) {\n        uniqScopes.delete(scope);\n      }\n    });\n  }\n  const scopeNotFound = currentScope && !uniqScopes.has(currentScope);\n  if (scopeNotFound) {\n    uniqScopes.add(currentScope);\n  }\n\n  // always add series at the beginning of the list\n  const arr: Array<SelectableValue<MatcherScope>> = [\n    {\n      label: getGroupLabelForScope('series'),\n      description: getGroupDescriptionForScope('series'),\n      value: 'series',\n    },\n  ];\n\n  for (const scope of uniqScopes) {\n    if (scope === 'series') {\n      continue;\n    }\n    arr.push({\n      label: getGroupLabelForScope(scope),\n      description: getGroupDescriptionForScope(scope),\n      value: scope,\n    });\n  }\n\n  return arr;\n}\n\nfunction useMatcherScopesOptions(\n  providedUniqScopes: Set<MatcherScope>,\n  currentScope?: MatcherScope,\n  allowedScopes: MatcherScope[] = Array.from(providedUniqScopes)\n): Array<SelectableValue<MatcherScope>> {\n  return useMemo(\n    () => buildScopeOptions(providedUniqScopes, currentScope, allowedScopes),\n    [providedUniqScopes, currentScope, allowedScopes]\n  );\n}\n\nexport function MatcherScopeSelector({ value, scopes, allowedScopes, ...rest }: MatcherScopeSelectorProps) {\n  const options = useMatcherScopesOptions(scopes, value, allowedScopes);\n  return <RadioButtonGroup {...rest} options={options} value={value ?? options[0]?.value} />;\n}\n\nexport { getUniqueMatcherScopes } from './utils';\n"],"names":["getGroupLabelForScope","getGroupDescriptionForScope","useMemo","jsx","RadioButtonGroup"],"mappings":";;;;;;;;;;AAcO,SAAS,kBACd,kBAAA,EACA,YAAA,EACA,gBAAgC,KAAA,CAAM,IAAA,CAAK,kBAAkB,CAAA,EACvB;AACtC,EAAA,MAAM,UAAA,GAAa,IAAI,GAAA,CAAI,kBAAkB,CAAA;AAG7C,EAAA,IAAI,aAAA,EAAe;AACjB,IAAA,UAAA,CAAW,OAAA,CAAQ,CAAC,KAAA,KAAU;AAC5B,MAAA,IAAI,CAAC,aAAA,CAAc,QAAA,CAAS,KAAK,CAAA,EAAG;AAClC,QAAA,UAAA,CAAW,OAAO,KAAK,CAAA;AAAA,MACzB;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AACA,EAAA,MAAM,aAAA,GAAgB,YAAA,IAAgB,CAAC,UAAA,CAAW,IAAI,YAAY,CAAA;AAClE,EAAA,IAAI,aAAA,EAAe;AACjB,IAAA,UAAA,CAAW,IAAI,YAAY,CAAA;AAAA,EAC7B;AAGA,EAAA,MAAM,GAAA,GAA4C;AAAA,IAChD;AAAA,MACE,KAAA,EAAOA,4BAAsB,QAAQ,CAAA;AAAA,MACrC,WAAA,EAAaC,kCAA4B,QAAQ,CAAA;AAAA,MACjD,KAAA,EAAO;AAAA;AACT,GACF;AAEA,EAAA,KAAA,MAAW,SAAS,UAAA,EAAY;AAC9B,IAAA,IAAI,UAAU,QAAA,EAAU;AACtB,MAAA;AAAA,IACF;AACA,IAAA,GAAA,CAAI,IAAA,CAAK;AAAA,MACP,KAAA,EAAOD,4BAAsB,KAAK,CAAA;AAAA,MAClC,WAAA,EAAaC,kCAA4B,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