import axios from 'B/axios';

import { AllCondition, LogicForm } from './types';

export type Dyn = {
  agb_id: number;
  agb_name: string;
};

export async function getDyns(agbID: number): Promise<false | Dyn[]> {
  try {
    const { data: dyns } = await axios.get<Dyn[]>(`/agbs/${agbID}/dyns`);

    return dyns;
  } catch (_) {
    return false;
  }
}

export type Logic = {
  sub_conditions: any;
  id: number;
  name: string;
  condition: AllCondition;
  value: string | number;
  order: number;
  next_task_id: number;
};

export async function getLogics(agbID: number): Promise<false | Logic[]> {
  try {
    const { data: logics } = await axios.get<Logic[]>(`/agbs/${agbID}/logics`);
    console.log(logics)
    return logics;
  } catch (_) {
    return false;
  }
}

export function storeLogics(agbID: number, forms: LogicForm[]) {
  return axios.post(`/agbs/${agbID}/logics`, { forms });
}
