"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEditUser = void 0;
var z = _interopRequireWildcard(require("zod"));
var _styles = require("../styles");
var _reactHookForm = require("react-hook-form");
var _zod2 = require("@hookform/resolvers/zod");
var _objects = require("../../../../hooks/objects");
var _reactQuery = require("@tanstack/react-query");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _native = require("@react-navigation/native");
var _toastSlice = require("../../../../../core/stores/slices/toastSlice");
var _constants = require("../../../../../core/constants");
var _enums = require("../../../../enums");
var _hooks = require("../../../../hooks");
var _react = require("react");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const schema = z.object({
  image: z.custom().optional(),
  displayName: z.string().min(1).max(100),
  description: z.string().max(180).optional()
});
const useEditUser = userId => {
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const {
    accessibilityId
  } = (0, _hooks.useAmityPage)({
    pageId: _enums.PageID.edit_user_profile_page
  });
  const {
    user
  } = (0, _objects.useUser)({
    userId,
    enabled: !!userId
  });
  const navigation = (0, _native.useNavigation)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const [isDisplayNameDisabled, setIsDisplayNameDisabled] = (0, _react.useState)(false);
  const {
    watch,
    control,
    resetField,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod2.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
  } = (0, _reactQuery.useMutation)({
    mutationKey: ['edit-user', userId],
    mutationFn: payload => _tsSdkReactNative.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(_constants.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
  };
};
exports.useEditUser = useEditUser;
//# sourceMappingURL=useEditUser.js.map