"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEditCommunity = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _toastSlice = require("../../../../../core/stores/slices/toastSlice");
var _useImagePicker = _interopRequireDefault(require("../../../../hooks/useImagePicker"));
var _reactHookForm = require("react-hook-form");
var _native = require("@react-navigation/native");
var z = _interopRequireWildcard(require("zod"));
var _zod2 = require("@hookform/resolvers/zod");
var _types = require("../../shared/types");
var _styles = require("../styles");
var _reactQuery = require("@tanstack/react-query");
var _useFile = _interopRequireDefault(require("../../../../hooks/files/useFile"));
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); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const schema = z.object({
  image: z.custom().optional(),
  displayName: z.string().trim().min(1).max(30),
  description: z.string().trim().max(180).optional(),
  categories: z.array(z.custom()),
  privacy: z.enum(_types.AmityCommunityPrivacyEnum)
});
const useEditCommunity = community => {
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const image = (0, _useFile.default)(community.avatarFileId);
  const {
    watch,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    },
    handleSubmit,
    setValue,
    control
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod2.zodResolver)(schema),
    mode: 'onChange',
    values: {
      image,
      displayName: community.displayName || '',
      description: community.description || '',
      categories: community.categories,
      privacy: community.isPublic ? _types.AmityCommunityPrivacyEnum.PUBLIC : _types.AmityCommunityPrivacyEnum.PRIVATE
    }
  });
  const navigation = (0, _native.useNavigation)();
  const {
    openImageGallery,
    progress,
    openCamera
  } = (0, _useImagePicker.default)();
  const values = watch();
  const {
    mutateAsync
  } = (0, _reactQuery.useMutation)({
    mutationFn: payload => _tsSdkReactNative.CommunityRepository.updateCommunity(community.communityId, payload),
    onSuccess: () => {
      navigation.navigate('CommunityProfilePage', {
        communityId: community.communityId
      });
      showToast({
        type: 'success',
        message: 'Successfully updated community profile.'
      });
    },
    onError: () => {
      showToast({
        type: 'failed',
        message: 'Failed to save your community profile. Please try again.'
      });
    }
  });
  const onSubmit = async () => {
    var _values$image;
    const isPublic = values.privacy === _types.AmityCommunityPrivacyEnum.PUBLIC;
    await mutateAsync({
      isPublic,
      isDiscoverable: isPublic,
      requiresJoinApproval: false,
      description: values.description,
      displayName: values.displayName,
      avatarFileId: (_values$image = values.image) === null || _values$image === void 0 ? void 0 : _values$image.fileId,
      categoryIds: values.categories.map(category => category.categoryId)
    });
  };
  return {
    isDirty,
    isSubmitting,
    isValid,
    handleSubmit,
    setValue,
    control,
    onSubmit,
    openImageGallery,
    progress,
    openCamera,
    styles,
    theme,
    navigation,
    values
  };
};
exports.useEditCommunity = useEditCommunity;
//# sourceMappingURL=useEditCommunity.js.map