import { CompilationTemplateFormField } from '@/components/compilation-template-form-field';
import { Form } from '@/components/ui/form';
import { zodResolver } from '@hookform/resolvers/zod';
import { memo } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { initialCompilationValues } from '../../constant/pipeline';
import { useFormValues } from '../../hooks/use-form-values';
import { useWatchFormChange } from '../../hooks/use-watch-form-change';
import { INextOperatorForm } from '../../interface';
import { buildOutputList } from '../../utils/build-output-list';
import { FormWrapper } from '../components/form-wrapper';
import { Output } from '../components/output';

export const FormSchema = z.object({
  compilation_template_group_id: z.string().optional(),
});

export type CompilationFormSchemaType = z.infer<typeof FormSchema>;

const outputList = buildOutputList(initialCompilationValues.outputs);

const CompilationForm = ({ node }: INextOperatorForm) => {
  const defaultValues = useFormValues(initialCompilationValues, node);

  const form = useForm<CompilationFormSchemaType>({
    defaultValues,
    resolver: zodResolver(FormSchema),
  });

  useWatchFormChange(node?.id, form);

  return (
    <Form {...form}>
      <FormWrapper>
        <CompilationTemplateFormField name="compilation_template_group_id"></CompilationTemplateFormField>
        <Output list={outputList}></Output>
      </FormWrapper>
    </Form>
  );
};

export default memo(CompilationForm);
