import { FormItem } from "./types";
import { isArray } from "/@/utils/validate";
export function createDefaultData(formItems: FormItem[]) {
  console.log(formItems);
  const data: Record<string, any> = {};
  //创建默认值策略
  for (const item of formItems) {
    switch (item.type) {
      case "checkbox":
      case "array":
      case "img_upload_list":
        data[item.field] = [];
        break;
      case "number":
        data[item.field] = 0;
        break;
      default:
        data[item.field] = "";
        break;
    }
  }

  return data;
}

export function transform(
  formItems: FormItem[],
  data: Record<string, any>,
  transform?: Record<string, any>
) {
  const detail: Record<string, any> = {};
  for (let i = 0; i < formItems.length; i++) {
    const { field: source, type } = formItems[i];
    switch (type) {
      case "checkbox":
      case "array":
      case "img_upload_list":
        detail[source] = data[transform[source]] || data[source] || [];
        break;
      case "number":
        detail[source] =
          Number(data[transform[source]]) || Number(data[source]) || 0;
        break;
      default:
        detail[source] = data[transform[source]] || data[source] || "";
        break;
    }
  }

  return detail;
}
