import {
  IAction,
  IActionResult,
  IActionNode,
  IActionProperty,
  IContext,
} from "../types/ActionTypes";
import { actionAddins } from ".";


interface Props {
  displayName: string;
  field: Field;
  verName: VerName;
}

interface VerName {
  name: string;
}

interface Field {
  name: string;
  type: string;
  code: string;
  label: string;
  noLabel: boolean;
  required: boolean;
  minLength: string;
  maxLength: string;
  expression: string;
  hideExpression: boolean;
  unique: boolean;
  defaultValue: string;
}

export class CurrentFieldGetAction implements IAction {
  name: string;
  actionProps: IActionProperty[];
  currentFieldGetProps: Props;
  constructor() {
    this.name = "フィールドの値を取得する";
    this.actionProps = [];
    this.currentFieldGetProps = {} as Props;
    this.register();
  }

  async process(
    prop: IActionNode,
    event: any,
    context: IContext
  ): Promise<IActionResult> {
    this.currentFieldGetProps = prop.ActionValue as Props;
    this.actionProps = prop.actionProps;

    let result = {
      canNext: true,
      result: "",
    } as IActionResult;
    
    try {
      context.variables[this.currentFieldGetProps.verName.name] = context.record[this.currentFieldGetProps.field.code].value;
    } catch (error) {
      console.error("CurrentFieldGetAction error", error);
      result.canNext = false;
    }
    return result;
  }

  register(): void {
    actionAddins[this.name] = this;
  }
}

new CurrentFieldGetAction();
