import {
  getPlotConfig
} from "./chunk-BWGU5NJS.js";
import {
  fillTermWrapper
} from "./chunk-SA7APTJR.js";
import {
  copyMerge
} from "./chunk-M3J4MINX.js";
import {
  TermTypes,
  numericTypes
} from "./chunk-NOEAT6CX.js";

// plots/matrix/hierCluster.config.js
async function getPlotConfig2(opts = {}, app) {
  opts.chartType = "hierCluster";
  const config = await getPlotConfig(opts, app);
  delete config.genes;
  config.settings.hierCluster = {
    /* type of data used for clustering
    exciting todo: (to introduce new dt values)
    - gene dependency
    - numeric dic term
    - non-gene genomic stuff that resolves into numeric quantities (cpg meth)
    - metabolite
    */
    dataType: config.dataType,
    // adjust the default group name based on automatically detected term types
    // Done in matrix.cells.js: setHierClusterCellProps
    // termGroupName: 'Expression',
    clusterSamples: true,
    clusterRows: true,
    clusterMethod: "average",
    // complete
    distanceMethod: "euclidean",
    zScoreCap: 5,
    zScoreTransformation: true,
    xDendrogramHeight: 100,
    yDendrogramHeight: 200,
    colorScale: "blueWhiteRed"
  };
  const overrides = app.vocabApi.termdbConfig.hierCluster || {};
  const hierClusterSubTypeOverrides = app.vocabApi.termdbConfig[`${config.dataType}Cluster`] || {};
  copyMerge(
    config.settings.hierCluster,
    overrides.settings,
    opts.settings?.hierCluster || {},
    hierClusterSubTypeOverrides.settings
  );
  {
    const c = config.settings.hierCluster.colorScale;
    if (!c) throw "colorScale missing";
  }
  config.settings.matrix.collabelpos = "top";
  const termGroupName = config.settings.hierCluster.termGroupName;
  const hcTermGroup = config.termgroups.find((g) => g.type == "hierCluster" || g.name == termGroupName) || {
    name: termGroupName
  };
  hcTermGroup.type = "hierCluster";
  if (!hcTermGroup.lst?.length) {
    if (!Array.isArray(opts.terms)) throw "opts.terms[] not array (may show geneset edit ui)";
    const promises = [];
    for (const i of opts.terms) {
      const tw = i.term ? i : { term: i };
      if (!tw.term.type) {
        if (config.dataType == TermTypes.GENE_EXPRESSION || config.dataType == TermTypes.METABOLITE_INTENSITY || config.dataType == TermTypes.PROTEOME_ABUNDANCE || config.dataType == TermTypes.SSGSEA) {
          tw.term.type = config.dataType;
        } else {
          throw `term type missing and cannot be assigned by dataType`;
        }
      } else if (!numericTypes.has(tw.term.type)) {
        throw "term type is not numeric";
      } else if (config.dataType && !canTermBeInHierGrp(config.dataType, tw.term.type)) {
        throw `cannot have term type ${tw.term.type} in ${config.dataType} term group`;
      }
      promises.push(fillTermWrapper(tw, app.vocabApi));
    }
    hcTermGroup.lst = await Promise.all(promises);
    if (config.termgroups.indexOf(hcTermGroup) == -1) config.termgroups.unshift(hcTermGroup);
  }
  config.settings.matrix.maxSample = 1e5;
  return config;
}
function canTermBeInHierGrp(grpType, twType) {
  if (grpType == "numericDictTerm") {
    if (twType == "float" || twType == "integer") return true;
  }
  return twType == grpType;
}

export {
  getPlotConfig2 as getPlotConfig
};
//# sourceMappingURL=chunk-XAVNZNXQ.js.map
