import * as z from 'zod';
import { useStyles } from '../styles';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { useUser } from '../../../../hooks/objects';
import { useMutation } from '@tanstack/react-query';
import { UserRepository } from '@amityco/ts-sdk-react-native';
import { useNavigation } from '@react-navigation/native';
import { useToast } from '../../../../../core/stores/slices/toastSlice';
import { ERROR_CODE } from '../../../../../core/constants';
import { PageID } from '../../../../enums';
import { useAmityPage } from '../../../../hooks';
import { useState } from 'react';
const schema = z.object({
  image: z.custom().optional(),
  displayName: z.string().min(1).max(100),
  description: z.string().max(180).optional()
});
export const useEditUser = userId => {
  const {
    styles,
    theme
  } = useStyles();
  const {
    accessibilityId
  } = useAmityPage({
    pageId: PageID.edit_user_profile_page
  });
  const {
    user
  } = useUser({
    userId,
    enabled: !!userId
  });
  const navigation = useNavigation();
  const {
    showToast
  } = useToast();
  const [isDisplayNameDisabled, setIsDisplayNameDisabled] = useState(false);
  const {
    watch,
    control,
    resetField,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange',
    values: {
      image: (user === null || user === void 0 ? void 0 : user.avatar) || null,
      displayName: (user === null || user === void 0 ? void 0 : user.displayName) || '',
      description: (user === null || user === void 0 ? void 0 : user.description) || ''
    }
  });
  const {
    mutateAsync
  } = useMutation({
    mutationKey: ['edit-user', userId],
    mutationFn: payload => UserRepository.updateUser(userId, payload),
    onSuccess: () => {
      navigation.goBack();
      showToast({
        type: 'success',
        message: 'Successfully updated your profile!'
      });
    },
    onError: error => {
      var _error$message;
      if ((_error$message = error.message) !== null && _error$message !== void 0 && _error$message.includes(ERROR_CODE.DISPLAY_NAME_UPDATE)) {
        setIsDisplayNameDisabled(true);
        resetField('displayName');
        showToast({
          type: 'informative',
          message: 'Only administrator can update user display name.'
        });
      } else {
        showToast({
          type: 'informative',
          message: 'Failed to save your profile. Please try again.'
        });
      }
    }
  });
  const onSubmit = async data => {
    var _data$image;
    await mutateAsync({
      avatarFileId: (_data$image = data.image) === null || _data$image === void 0 ? void 0 : _data$image.fileId,
      ...(isDisplayNameDisabled ? {} : {
        displayName: data.displayName
      }),
      description: data.description
    });
  };
  return {
    styles,
    theme,
    user,
    watch,
    control,
    handleSubmit,
    isDirty,
    isSubmitting,
    isValid,
    onSubmit,
    accessibilityId,
    isDisplayNameDisabled
  };
};
//# sourceMappingURL=useEditUser.js.map