import type {
  EasyFlowFormSchema as FormSchema,
  EasyFlowFormProps,
} from '@easyflow/common-ui';

import type { ComponentType } from './component';

import { setupEasyFlowForm, useEasyFlowForm as useForm, z } from '@easyflow/common-ui';
import { $t } from '@easyflow/locales';

async function initSetupEasyFlowForm() {
  setupEasyFlowForm<ComponentType>({
    config: {
      modelPropNameMap: {
        Upload: 'fileList',
        CheckboxGroup: 'model-value',
      },
    },
    defineRules: {
      required: (value, _params, ctx) => {
        if (value === undefined || value === null || value.length === 0) {
          return $t('ui.formRules.required', [ctx.label]);
        }
        return true;
      },
      selectRequired: (value, _params, ctx) => {
        if (value === undefined || value === null) {
          return $t('ui.formRules.selectRequired', [ctx.label]);
        }
        return true;
      },
    },
  });
}

const useEasyFlowForm = useForm<ComponentType>;

export { initSetupEasyFlowForm, useEasyFlowForm, z };

export type EasyFlowFormSchema = FormSchema<ComponentType>;
export type { EasyFlowFormProps };
