'use strict';

var elementPlus = require('element-plus');

const validateFormWithScroll = async (formRef, options) => {
  if (!formRef) {
    console.warn("Form ref is undefined");
    return false;
  }
  const { showMessage = true, message = "\u8BF7\u68C0\u67E5\u8868\u5355\u586B\u5199\u662F\u5426\u6B63\u786E" } = options || {};
  try {
    await formRef.validate();
    return true;
  } catch (invalidFields) {
    if (invalidFields && typeof invalidFields === "object") {
      const firstErrorField = Object.keys(invalidFields)[0];
      if (firstErrorField) {
        formRef.scrollToField(firstErrorField);
      }
    }
    if (showMessage) {
      elementPlus.ElMessage.warning(message);
    }
    return false;
  }
};
const validateFormWithScrollCallback = async (formRef, onSuccess, options) => {
  const isValid = await validateFormWithScroll(formRef, options);
  if (isValid) {
    await onSuccess();
  }
};

exports.validateFormWithScroll = validateFormWithScroll;
exports.validateFormWithScrollCallback = validateFormWithScrollCallback;
