import {SemanticAnalyser} from './../processor/semantic/semanticAnalyser';
import {IVProgProcessor} from './../processor/ivprogProcessor';
import { InputTest } from './inputTest';
import { OutputTest } from './outputTest';
import { getInstance as LocalizedStrings } from './../services/localizedStringsService'

export async function autoGenerateTestCaseOutput (program_text, testCases, callback) {
  let copyTestCases = testCases.map((test) => Object.assign({}, test));
  try {
    const program = SemanticAnalyser.analyseFromSource(program_text);
    const resultList = testCases.map(async (test, id) => {
      const input = new InputTest(test.input);
      const output = new OutputTest();
      const exec =  new IVProgProcessor(program);
      exec.registerInput(input);
      exec.registerOutput(output);
      await exec.interpretAST();
      return { id: id, program: exec };
    });
    const result_final = await Promise.all(resultList);
    for (let i = 0; i < result_final.length; ++i) {
      const result_1 = result_final[i];
      const output_2 = result_1.program.output.list;
      const input_2 = result_1.program.input;
      if (input_2.index != input_2.inputList.length) {
        window.showAlert(LocalizedStrings().getMessage("testcase_autogen_unused_input", [result_1.id + 1]));
        return false;
      }
      if (output_2.length == 0) {
        window.showAlert(LocalizedStrings().getMessage("testcase_autogen_empty", [result_1.id + 1]));
      }
      copyTestCases[result_1.id].output = output_2;
    }
    callback(copyTestCases);
    return true;
  }catch (error) {
    return Promise.reject(error)
  }
}