import type { FormInstance, FormItemProp } from 'element-plus';

import type { Ref } from 'vue';

import { nextTick } from 'vue';

type FormRef = Ref<FormInstance | null | undefined>;

export async function syncProgrammaticFieldValidation(
  formRef: FormRef,
  fields?: Array<FormItemProp>,
) {
  await nextTick();

  if (!formRef.value) {
    return;
  }

  if (!fields || fields.length === 0) {
    formRef.value.clearValidate();
    return;
  }

  formRef.value.clearValidate(fields);
}
