import { useStyles } from '../styles';
import { useRoute } from '@react-navigation/native';
import * as z from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { useUserCollection } from '../../../../hooks/collections/user/useUserCollection';
import { useSearchUserByDisplayNameCollection } from '../../../../hooks/collections/search/useSearchUserByDisplayNameCollection';
import { SearchUsersByEnum, UserRepository } from '@amityco/ts-sdk-react-native';
function useAddMember({
  communityId
} = {}) {
  var _route$params, _values$search;
  const {
    styles,
    theme
  } = useStyles();
  const route = useRoute();
  const schema = z.object({
    users: z.array(z.custom()).min(1),
    search: z.string().optional()
  });
  const {
    watch,
    control,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange',
    defaultValues: {
      search: '',
      users: ((_route$params = route.params) === null || _route$params === void 0 ? void 0 : _route$params.users) ?? []
    }
  });
  const values = watch();
  const userCollection = useUserCollection({
    enabled: !communityId,
    params: {
      limit: 20
    }
  });
  const searchUserDisplayNameCollection = useSearchUserByDisplayNameCollection({
    params: {
      displayName: values.search,
      limit: 20,
      searchBy: [SearchUsersByEnum.DISPLAY_NAME],
      matchType: UserRepository.AmityUserSearchMatchType.PARTIAL
    },
    enabled: !!values.search && !communityId
  });
  const onSubmit = () => {
    var _route$params2;
    (_route$params2 = route.params) !== null && _route$params2 !== void 0 && _route$params2.onAddedAction ? route.params.onAddedAction(watch('users')) : null;
  };
  const collection = ((_values$search = values.search) === null || _values$search === void 0 ? void 0 : _values$search.length) > 0 ? searchUserDisplayNameCollection : userCollection;
  return {
    styles,
    theme,
    isDirty,
    isSubmitting,
    isValid,
    handleSubmit,
    control,
    collection,
    onSubmit,
    values
  };
}
export default useAddMember;
//# sourceMappingURL=useAddMember.js.map