const { selectCombiner } = require('../src/lib/combiner_selectors');

function readArg(name, fallback) {
  const index = process.argv.indexOf(`--${name}`);
  if (index === -1 || index + 1 >= process.argv.length) return fallback;
  return process.argv[index + 1];
}

function readNumber(name, fallback) {
  const value = Number(readArg(name, fallback));
  return Number.isFinite(value) ? value : fallback;
}

function parseLayers(value) {
  return String(value || '1,100,300,700,1000')
    .split(',')
    .map((item) => Number(item.trim()))
    .filter((item) => Number.isInteger(item) && item > 0);
}

function main() {
  const layers = parseLayers(readArg('layers', '1,100,300,700,1000'));
  const stepCount = readNumber('steps', 30);
  const componentCount = readNumber('components', 4);
  const seed = readArg('seed', 'mazex-combiner-selector-demo');
  const checkpoints = [1, Math.ceil(stepCount / 2), stepCount];

  const result = layers.map((layerNo) => ({
    layer_no: layerNo,
    step_count: stepCount,
    component_count: componentCount,
    checkpoints: checkpoints.map((stepNo) => ({
      step_no: stepNo,
      ...selectCombiner({
        layerNo,
        stepNo,
        stepCount,
        componentCount,
        seed,
      }),
    })),
  }));

  console.log(JSON.stringify(result, null, 2));
}

main();
