import { nextTick, reactive, ref, unref, watch } from 'vue';
import { FormActionType, FormProps } from '/@/components/Form';
import { getDynamicProps } from '/@/utils';

export function useTimePeriodForm(props: FormProps): [Fn, FormActionType] {
  const method = reactive<Partial<FormActionType>>({} as any);
  const isInitialize = ref(false);
  async function register(instance: FormActionType) {
    if (unref(isInitialize)) return;
    await nextTick();
    Object.assign(method, instance);
    isInitialize.value = true;
    watch(
      () => props,
      () => {
        props && instance.setProps(getDynamicProps(props));
      },
      {
        immediate: true,
        deep: true,
      }
    );
  }

  return [register, method as FormActionType];
}
