"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useStoriesNotificationSetting = useStoriesNotificationSetting;
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 _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
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 _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({
  newStories: z.enum(Object.values(_constants2.NOTIFICATION_RADIO_OPTIONS)),
  storyReaction: z.enum(Object.values(_constants2.NOTIFICATION_RADIO_OPTIONS)),
  storyComments: z.enum(Object.values(_constants2.NOTIFICATION_RADIO_OPTIONS))
});
function useStoriesNotificationSetting({
  community
}) {
  const {
    styles
  } = (0, _styles.useStyles)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    accessibilityId
  } = (0, _hooks.useAmityPage)({
    pageId: _enums.PageID.community_stories_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 ? {
    newStories: (0, _utils.getNotificationEventValue)(settings.events, _tsSdkReactNative.CommunityNotificationEventNameEnum.STORY_CREATED),
    storyReaction: (0, _utils.getNotificationEventValue)(settings.events, _tsSdkReactNative.CommunityNotificationEventNameEnum.STORY_REACTED),
    storyComments: (0, _utils.getNotificationEventValue)(settings.events, _tsSdkReactNative.CommunityNotificationEventNameEnum.STORY_COMMENT_CREATED)
  } : undefined, [settings]);
  const {
    control,
    handleSubmit,
    formState: {
      isDirty,
      isValid,
      isSubmitting
    }
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod.zodResolver)(schema),
    mode: 'onChange',
    values: formValues
  });
  const notifications = [{
    title: 'New stories',
    description: 'Receive notifications when someone creates a new story in this community.',
    radio: {
      name: 'newStories',
      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: 'Story reactions',
    description: 'Receive notifications when someone reacts to your story in this community.',
    radio: {
      name: 'storyReaction',
      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: 'Story comments',
    description: 'Receive notifications when someone comments on your story in this community.',
    radio: {
      name: 'storyComments',
      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.STORY_CREATED, data.newStories), (0, _utils.buildNotificationEvent)(_tsSdkReactNative.CommunityNotificationEventNameEnum.STORY_REACTED, data.storyReaction), (0, _utils.buildNotificationEvent)(_tsSdkReactNative.CommunityNotificationEventNameEnum.STORY_COMMENT_CREATED, data.storyComments)]
      });
      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=useStoriesNotificationSetting.js.map