import { sceneGraph } from '../core/sceneGraph/index.js';

function getSimpleVariableNameFromDatasourceUid(uid) {
  if (!uid) {
    return void 0;
  }
  const shortMatch = uid.match(/^\$([A-Za-z0-9_]+)$/);
  if (shortMatch) {
    return shortMatch[1];
  }
  const bracketMatch = uid.match(/^\$\{([A-Za-z0-9_]+)\}$/);
  if (bracketMatch) {
    return bracketMatch[1];
  }
  return void 0;
}
function getDatasourceVariableSelectedUids(sceneObject, variableName) {
  const variable = sceneGraph.lookupVariable(variableName, sceneObject);
  if (!variable) {
    return void 0;
  }
  const value = variable.getValue();
  if (Array.isArray(value)) {
    return value.filter((item) => item !== "default").map(String);
  }
  if (value === null || value === void 0) {
    return void 0;
  }
  if (value === "default") {
    return [];
  }
  return [String(value)];
}
function getDatasourceVariableSelectionCount(sceneObject, variableName) {
  const uids = getDatasourceVariableSelectedUids(sceneObject, variableName);
  return uids == null ? void 0 : uids.length;
}
function expandMultiValueDatasourceUids(sceneObject, datasource) {
  const variableName = getSimpleVariableNameFromDatasourceUid(datasource == null ? void 0 : datasource.uid);
  if (!variableName) {
    return void 0;
  }
  const uids = getDatasourceVariableSelectedUids(sceneObject, variableName);
  if (!uids || uids.length <= 1) {
    return void 0;
  }
  return uids;
}

export { expandMultiValueDatasourceUids, getDatasourceVariableSelectedUids, getDatasourceVariableSelectionCount, getSimpleVariableNameFromDatasourceUid };
//# sourceMappingURL=datasourceVariableRef.js.map
