import { Database } from "bridge-common";
import {
  createAction,
  updateAction,
  deleteAction,
  selectAllAction,
} from "../actions/service";
import { FieldDescription, Entity } from "./service";

type GenerateCreateActionArgs = {
  entity: Entity;
  table: keyof Database;
  fields: FieldDescription[];
};

export function generateCreateAction({
  entity,
  table,
  fields,
}: GenerateCreateActionArgs) {
  return async (currentState: any, formData: FormData) => {
    return createAction({
      entity,
      table,
      fields,
      currentState,
      formData,
    });
  };
}

type GenerateUpdateActionArgs = {
  entity: Entity;
  table: keyof Database;
  fields: FieldDescription[];
};

export function generateUpdateAction({
  entity,
  table,
  fields,
}: GenerateUpdateActionArgs) {
  return async (currentState: any, formData: FormData) => {
    return updateAction({
      entity,
      table,
      fields,
      currentState,
      formData,
    });
  };
}

type GenerateDeleteActionArgs = {
  entity: Entity;
  table: keyof Database;
};

export function generateDeleteAction({
  entity,
  table,
}: GenerateDeleteActionArgs) {
  return async (id: string) => {
    return deleteAction({ entity, table, id });
  };
}

export function generateSelectAllAction(table: keyof Database) {
  return async () => {
    return selectAllAction(table);
  };
}
