import { Controller, useFormContext } from 'react-hook-form';

import { type FormSignProps } from './types';
import HuSignDialog from '.';

const FormSign = ({ name, signProps, disabled }: FormSignProps) => {
  const form = useFormContext();

  const onHandleSave = (signature: string | undefined) => {
    form.setValue(name, signature);
    signProps?.onSave?.(signature);
  };

  const onHandleClose = () => {
    form.setValue(name, undefined);
    signProps?.onClose?.();
  };

  return (
    <Controller
      render={({ field }) => (
        <HuSignDialog
          {...field}
          {...signProps}
          onSave={onHandleSave}
          onClose={onHandleClose}
          disabled={disabled}
        />
      )}
      name={name}
    />
  );
};

export default FormSign;
