import {
  appInit
} from "./chunk-N2SKTCDK.js";
import {
  showErrorsWithCounter
} from "./chunk-SA7APTJR.js";
import {
  mclass
} from "./chunk-4UWS5Y3N.js";

// plots/disco/launch.adhoc.ts
async function launch(arg, genomeObj, holder) {
  const [mlst, errors] = await getMlst(arg);
  if (errors?.length) {
    return showErrorsWithCounter(errors, holder);
  }
  const opts = {
    holder,
    vocabApi: {
      // api is required by plot.app.js, so create a mock one for the adhoc data
      vocab: { terms: [] },
      main: () => {
        return;
      },
      getTermdbConfig: () => {
        return {};
      }
    },
    state: {
      args: {
        data: mlst,
        genome: genomeObj
      },
      plots: [
        {
          chartType: "Disco",
          subfolder: "disco",
          extension: "ts",
          /** NOTE: Users should only override the settings in the default
           * settings.Disco:{}, not the entire settings:{} object.*/
          overrides: { Disco: arg?.settings || {} }
        }
      ]
    }
  };
  const plotAppApi = await appInit(opts);
  return plotAppApi;
}
async function getMlst(arg) {
  if (Array.isArray(arg.mlst)) {
    return [arg.mlst, null];
  }
  const mlst = [];
  const errors = [];
  if (arg.snvText) parseSnvText(arg.snvText, mlst, errors);
  if (arg.svText) parseSvText(arg.svText, mlst, errors);
  if (arg.cnvText) parseCnvText(arg.cnvText, mlst, errors);
  return [mlst, errors];
}
function parseSnvText(text, mlst, errors) {
  for (const line of text.trim().split("\n")) {
    const l = line.trim().split("	");
    if (l.length != 5) {
      errors.push("snv input not equal to 5 columns");
      continue;
    }
    let m;
    try {
      m = {
        dt: 1,
        chr: l[0],
        position: Number(l[1]),
        gene: l[2],
        mname: l[3],
        class: validateMutation(l[4], errors)
      };
    } catch (e) {
      errors.push(e);
      continue;
    }
    mlst.push(m);
  }
}
function parseSvText(text, mlst, errors) {
  for (const line of text.trim().split("\n")) {
    const l = line.trim().split("	");
    if (l.length < 4 || l.length > 6) {
      errors.push("sv input not equal to 4 or 6 columns");
      continue;
    }
    let m;
    try {
      const length = l.length;
      if (length == 4) {
        m = {
          dt: 2,
          chrA: l[0],
          posA: Number(l[1]),
          chrB: l[2],
          posB: Number(l[3])
        };
      } else {
        m = {
          dt: 2,
          chrA: l[0],
          posA: Number(l[1]),
          geneA: l[2],
          chrB: l[3],
          posB: Number(l[4]),
          geneB: l[5]
        };
      }
    } catch (e) {
      errors.push(e);
      continue;
    }
    mlst.push(m);
  }
}
function parseCnvText(text, mlst, errors) {
  for (const line of text.trim().split("\n")) {
    const l = line.trim().split("	");
    if (l.length != 4) {
      errors.push("cnv input not equal to 4 columns");
      continue;
    }
    let m;
    try {
      m = {
        dt: 4,
        chr: l[0],
        start: Number(l[1]),
        stop: Number(l[2]),
        value: Number(l[3])
      };
    } catch (e) {
      errors.push(e);
      continue;
    }
    mlst.push(m);
  }
}
function validateMutation(mutation, errors) {
  const mut2check = mutation.toLowerCase();
  const foundMutation = Object.values(mclass).find(
    (m) => m.key.toLowerCase() === mut2check || m.label.toLowerCase() === mut2check
  );
  if (foundMutation) {
    return foundMutation.key;
  } else {
    errors.push(`Invalid mutation class: ${mutation}`);
  }
}

export {
  launch
};
//# sourceMappingURL=chunk-FJP77STI.js.map
