export const webFormSubmission = (widgetType) => {
  let submitArray = [];
  if (widgetType[0].type === "TemplateStyle") {
    widgetType.shift();
  }
  widgetType.forEach((data, index) => {
    let type = data.type;
    if (type === "TextInputMultiLineWidget") {
      submitArray.push({
        id: index + 1,
        type: "text_area_widget",
        name: "text_area_widget" + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            id: 1,
            value: data.value,
          },
        ],
      });
    } else if (type === "TextInputWidget") {
      submitArray.push({
        id: index + 1,
        type: "single_input_widget",
        name: "single_input_widget" + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            id: 1,
            value: data.value,
          },
        ],
      });
    } else if (type === "CheckboxWidget") {
      let answers = [];
      data.value.response.forEach((elem) => {
        answers.push({
          id: elem.value,
        });
      });
      submitArray.push({
        id: index + 1,
        type: "checkbox_widget",
        name: "checkbox_widget" + (index + 1),
        value: data.text.heading,
        nameValues: answers,
      });
    } else if (type === "DropdownWidget") {
      submitArray.push({
        id: index + 1,
        type: "dropdown_widget",
        name: "dropdown_widget" + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            value: data.value.response,
            id:
              data.value.response != null
                ? data.text.options.findIndex(
                    (value) => value.value === data.value.response
                  ) + 1
                : null,
          },
        ],
      });
    } else if (type === "GroupsWidget") {
      submitArray.push({
        id: index + 1,
        type: "groups_widget",
        name: "groups_widget" + (index + 1),
        value: data.text.heading,
        valuesMap: {
          spaceId: data.text.spaceId,
        },
        nameValues: [
          {
            value: data.value.response,
          },
        ],
      });
    } else if (type === "RadioButtonWidget") {
      submitArray.push({
        id: index + 1,
        type: "radiogroup_widget",
        name: "radiogroup_widget" + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            id: data.value.response.value,
          },
        ],
      });
    } else if (type === "RattingWidget") {
      submitArray.push({
        id: index + 1,
        type: "ratting_widget",
        name: "ratting_widget" + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            id: data.value.response,
          },
        ],
      });
    } else if (type === "BooleanWidget") {
      submitArray.push({
        id: index + 1,
        type: "boolean_widget",
        name: "boolean_widget" + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            id: data.value.response.id,
            value: data.value.response.value,
          },
        ],
      });
    } else if (type === "DateWidget") {
      submitArray.push({
        id: index + 1,
        type,
        name: type + (index + 1),
        value: data.text.heading,
        nameValues: [
          {
            id: 1,
            value: data.value,
          },
        ],
      });
    } else if (type === "UploadWidget") {
      submitArray.push({
        id: index + 1,
        type: "file_upload_widget",
        name: "file_upload_widget" + (index + 1),
        value: "",
        nameValues: [
          {
            id: 1,
            value: "",
          },
        ],
      });
    } else if (type === "SliderWidget") {
      submitArray.push({
        id: index + 1,
        type: "slider_widget",
        name: "slider_widget" + (index + 1),
        value: "",
        nameValues: [
          {
            id: null,
          },
        ],
      });
    }
  });
  console.log("submitArray", submitArray);
  return submitArray;
};
