"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCommentsNotificationSetting = useCommentsNotificationSetting;
var _react = require("react");
var _native = require("@react-navigation/native");
var _reactHookForm = require("react-hook-form");
var _zod = require("@hookform/resolvers/zod");
var z = _interopRequireWildcard(require("zod"));
var _styles = require("../styles");
var _hooks = require("../../../../hooks");
var _enums = require("../../../../enums");
var _useCommunityNotificationSettingsQuery = require("../../../../hooks/queries/useCommunityNotificationSettingsQuery");
var _toastSlice = require("../../../../../core/stores/slices/toastSlice");
var _constants = require("../../../../../core/constants");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _constants2 = require("../../shared/constants");
var _utils = require("../../shared/utils");
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({
  reactComments: z.enum(Object.values(_constants2.NOTIFICATION_RADIO_OPTIONS)),
  newComments: z.enum(Object.values(_constants2.NOTIFICATION_RADIO_OPTIONS)),
  replies: z.enum(Object.values(_constants2.NOTIFICATION_RADIO_OPTIONS))
});
function useCommentsNotificationSetting({
  community
}) {
  const {
    styles
  } = (0, _styles.useStyles)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    accessibilityId
  } = (0, _hooks.useAmityPage)({
    pageId: _enums.PageID.community_comments_notification_page
  });
  const navigation = (0, _native.useNavigation)();
  const {
    isLoading,
    isPending,
    data: settings,
    enableNotifications
  } = (0, _useCommunityNotificationSettingsQuery.useCommunityNotificationSettingsQuery)({
    communityId: community.communityId
  });
  const formValues = (0, _react.useMemo)(() => settings ? {
    reactComments: (0, _utils.getNotificationEventValue)(settings.events, _tsSdkReactNative.CommunityNotificationEventNameEnum.COMMENT_REACTED),
    newComments: (0, _utils.getNotificationEventValue)(settings.events, _tsSdkReactNative.CommunityNotificationEventNameEnum.COMMENT_CREATED),
    replies: (0, _utils.getNotificationEventValue)(settings.events, _tsSdkReactNative.CommunityNotificationEventNameEnum.COMMENT_REPLIED)
  } : undefined, [settings]);
  const {
    control,
    handleSubmit,
    formState: {
      isDirty,
      isValid,
      isSubmitting
    }
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod.zodResolver)(schema),
    mode: 'onChange',
    values: formValues
  });
  const notifications = [{
    title: 'React comments',
    description: 'Receive notifications when someone like your comment in this community.',
    radio: {
      name: 'reactComments',
      items: [{
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.everyone,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.everyone
      }, {
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.onlyModerator,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.onlyModerator
      }, {
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.off,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.off
      }]
    }
  }, {
    title: 'New comments',
    description: 'Receive notifications when someone comment to your posts in this community.',
    radio: {
      name: 'newComments',
      items: [{
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.everyone,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.everyone
      }, {
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.onlyModerator,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.onlyModerator
      }, {
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.off,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.off
      }]
    }
  }, {
    title: 'Replies',
    description: 'Receive notifications when someone comment to your comments in this community.',
    radio: {
      name: 'replies',
      items: [{
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.everyone,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.everyone
      }, {
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.onlyModerator,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.onlyModerator
      }, {
        label: _constants2.NOTIFICATION_RADIO_OPTIONS.off,
        value: _constants2.NOTIFICATION_RADIO_OPTIONS.off
      }]
    }
  }];
  const onSubmit = async data => {
    try {
      await enableNotifications({
        communityId: community.communityId,
        events: [(0, _utils.buildNotificationEvent)(_tsSdkReactNative.CommunityNotificationEventNameEnum.COMMENT_REACTED, data.reactComments), (0, _utils.buildNotificationEvent)(_tsSdkReactNative.CommunityNotificationEventNameEnum.COMMENT_CREATED, data.newComments), (0, _utils.buildNotificationEvent)(_tsSdkReactNative.CommunityNotificationEventNameEnum.COMMENT_REPLIED, data.replies)]
      });
      navigation.navigate('CommunityProfilePage', {
        communityId: community.communityId
      });
      showToast({
        type: 'success',
        message: _constants.TOAST.COMMUNITY.PROFILE.UPDATE.SUCCESS
      });
    } catch {
      showToast({
        type: 'informative',
        message: _constants.TOAST.COMMUNITY.PROFILE.UPDATE.FAILED
      });
    }
  };
  return {
    styles,
    control,
    isDirty,
    onSubmit,
    isLoading,
    handleSubmit,
    notifications,
    accessibilityId,
    disabled: !isDirty || !isValid || isSubmitting || isPending
  };
}
//# sourceMappingURL=useCommentsNotificationSetting.js.map