import { useState } from 'react';
import { runScreener } from '../../../../api';

export function useScreenerRun() {
  const [result, setResult] = useState(null);
  const [running, setRunning] = useState(false);

  async function call(mode, settings) {
    setRunning(true);
    try {
      const body = {
        mode,
        weights: settings.weights,
        node_params: settings.node_params,
        gate_params: settings.gate_params,
        top_n: settings.top_n,
      };
      const r = await runScreener(body);
      setResult(r);
      return r;
    } finally {
      setRunning(false);
    }
  }

  return {
    result, running,
    runPreview: (s) => call('preview', s),
    runSave:    (s) => call('manual_save', s),
  };
}
