import { FormItem } from "/@/components/BasicForm";

export function createFormData(
  formItems: Array<FormItem>,
  source: Record<string, string> = null,
  lockKey = []
) {
  const formData: Record<string, any> = {};

  for (const item of formItems) {
    const isCheckBox = item.type === "checkbox";
    const isDatepicker = item.type === "date_picker";
    const isRange = item.type === "range";
    const isInputGroup = item.type === "input_group";

    if (item.defaultValue && !source) {
      formData[item.field] = item.defaultValue;
    } else if (lockKey && lockKey.includes(item.field)) {
      formData[item.field] = source[item.field] || "";
    } else if (isInputGroup) {
      const defaultSelectType = item.otherOptions?.inputGroupOptions[0]?.value || "";
      formData[item.field] = "";
      formData[item.type_field] = defaultSelectType;
    } else {
      formData[item.field] = isCheckBox || isDatepicker || isRange ? [] : "";
    }
  }

  return formData;
}
