import { api } from 'boot/axios';
import { ActionFlow } from 'src/types/ActionTypes';

export class FlowCtrl {
  async getFlows(appId: string): Promise<ActionFlow[]> {
    const flows: ActionFlow[] = [];
    try {
      const result = await api.get(`api/flows/${appId}`);
      //console.info(result.data);
      if (!result.data || !Array.isArray(result.data)) {
        return [];
      }

      for (const flow of result.data) {
        flows.push(ActionFlow.fromJSON(flow.content));
      }
      return flows;
    } catch (error) {
      console.error(error);
      return flows;
    }
  }

  async SaveFlow(jsonData: any): Promise<boolean> {
    const result = await api.post('api/flow', jsonData);
    console.info(result.data);
    return true;
  }
  /**
   * フローを更新する
   * @param jsonData
   * @returns
   */
  async UpdateFlow(jsonData: any): Promise<boolean> {
    const result = await api.put('api/flow', jsonData);
    console.info(result.data);
    return true;
  }
  /**
   * フローを消去する
   * @param flowId
   * @returns
   */
  async DeleteFlow(flowId: string): Promise<boolean> {
    const result = await api.delete('api/flow/' + flowId);
    console.info(result.data);
    return true;
  }
  /**
   * デプロイ
   * @param appid
   * @returns
   */
  async depoly(appid: string): Promise<boolean> {
    const result = await api.post(`api/v1/createjstokintone?app=${appid}`);
    console.info(result.data);
    return true;
  }
}
