import { CustomerModalSelector } from "@erp/customers/components";
import { FormField, FormItem } from "@repo/shadcn-ui/components";
import { CustomerSummary } from 'node_modules/@erp/customers/src/web/schemas';

import { Control, FieldPath, FieldValues } from "react-hook-form";

type CustomerModalSelectorFieldProps<TFormValues extends FieldValues> = {
  control: Control<TFormValues>;
  name: FieldPath<TFormValues>;
  disabled?: boolean;
  required?: boolean;
  readOnly?: boolean;
  className?: string;
  initialRecipient?: unknown;
};

export function RecipientModalSelectorField<TFormValues extends FieldValues>({
  control,
  name,
  disabled = false,
  required = false,
  readOnly = false,
  className,
  initialRecipient = {},
}: CustomerModalSelectorFieldProps<TFormValues>) {
  const isDisabled = disabled;
  const isReadOnly = readOnly && !disabled;


  return (
    <FormField
      control={control}
      name={name}
      render={({ field }) => {
        const { name, value, onChange, onBlur, ref } = field;
        //console.log({ name, value, onChange, onBlur, ref });
        return (
          <FormItem className={className}>
            <CustomerModalSelector
              value={value}
              disabled={isDisabled}
              readOnly={isReadOnly}
              onValueChange={onChange}
              initialCustomer={{
                ...initialRecipient as CustomerSummary
              }}
            />
          </FormItem>
        );
      }}
    />
  );
}
