"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _sonner = require("../../lib/sonner");
var _components = require("../components");
var _useI18n = require("../hooks/useI18n");
var _useThemeStyles = require("../hooks/useThemeStyles");
var _themeUtils = require("../utils/themeUtils");
var _OxyContext = require("../context/OxyContext");
var _jsxRuntime = require("react/jsx-runtime");
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 SearchSettingsScreen = ({
  onClose,
  theme,
  goBack
}) => {
  // Use useOxy() hook for OxyContext values
  const {
    oxyServices,
    user
  } = (0, _OxyContext.useOxy)();
  const {
    t
  } = (0, _useI18n.useI18n)();
  const [safeSearch, setSafeSearch] = (0, _react.useState)(false);
  const [searchPersonalization, setSearchPersonalization] = (0, _react.useState)(true);
  const [isLoading, setIsLoading] = (0, _react.useState)(true);
  const [isSaving, setIsSaving] = (0, _react.useState)(false);

  // Load settings
  (0, _react.useEffect)(() => {
    const loadSettings = async () => {
      try {
        setIsLoading(true);
        if (user?.id && oxyServices) {
          // Load from user's privacy settings
          const userData = await oxyServices.getCurrentUser();
          const privacySettings = userData?.privacySettings || {};

          // SafeSearch is typically stored in privacySettings.autoFilter or a separate field
          setSafeSearch(privacySettings.autoFilter ?? false);
          setSearchPersonalization(privacySettings.dataSharing ?? true);
        }
      } catch (error) {
        console.error('Failed to load search settings:', error);
      } finally {
        setIsLoading(false);
      }
    };
    loadSettings();
  }, [user?.id, oxyServices]);
  const handleSafeSearchToggle = (0, _react.useCallback)(async value => {
    try {
      setIsSaving(true);
      setSafeSearch(value);
      if (user?.id && oxyServices) {
        // Update privacy settings
        await oxyServices.updateProfile({
          privacySettings: {
            autoFilter: value
          }
        });
        _sonner.toast.success(t('searchSettings.safeSearch.updated') || 'SafeSearch setting updated');
      }
    } catch (error) {
      console.error('Failed to update SafeSearch:', error);
      _sonner.toast.error(t('searchSettings.safeSearch.error') || 'Failed to update SafeSearch');
      setSafeSearch(!value); // Revert on error
    } finally {
      setIsSaving(false);
    }
  }, [user?.id, oxyServices, t]);
  const handlePersonalizationToggle = (0, _react.useCallback)(async value => {
    try {
      setIsSaving(true);
      setSearchPersonalization(value);
      if (user?.id && oxyServices) {
        // Update privacy settings
        await oxyServices.updateProfile({
          privacySettings: {
            dataSharing: value
          }
        });
        _sonner.toast.success(t('searchSettings.personalization.updated') || 'Search personalization updated');
      }
    } catch (error) {
      console.error('Failed to update personalization:', error);
      _sonner.toast.error(t('searchSettings.personalization.error') || 'Failed to update personalization');
      setSearchPersonalization(!value); // Revert on error
    } finally {
      setIsSaving(false);
    }
  }, [user?.id, oxyServices, t]);
  const normalizedTheme = (0, _themeUtils.normalizeTheme)(theme);
  const themeStyles = (0, _useThemeStyles.useThemeStyles)(normalizedTheme);
  if (isLoading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: [styles.container, {
        backgroundColor: themeStyles.backgroundColor
      }],
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Header, {
        title: t('searchSettings.title') || 'Search Settings',
        onBack: goBack || onClose,
        variant: "minimal",
        elevation: "subtle"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.LoadingState, {
        color: themeStyles.textColor
      })]
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: [styles.container, {
      backgroundColor: themeStyles.backgroundColor
    }],
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Header, {
      title: t('searchSettings.title') || 'Search Settings',
      onBack: goBack || onClose,
      variant: "minimal",
      elevation: "subtle"
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.ScrollView, {
      style: styles.content,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Section, {
        title: t('searchSettings.safeSearch.title') || 'SafeSearch',
        isFirst: true,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.SettingRow, {
          title: t('searchSettings.safeSearch.label') || 'Enable SafeSearch',
          description: t('searchSettings.safeSearch.description') || 'Filter out explicit content from search results',
          value: safeSearch,
          onValueChange: handleSafeSearchToggle,
          disabled: isSaving,
          textColor: themeStyles.textColor,
          mutedTextColor: themeStyles.mutedTextColor,
          borderColor: themeStyles.borderColor
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Section, {
        title: t('searchSettings.personalization.title') || 'Search Personalization',
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.SettingRow, {
          title: t('searchSettings.personalization.label') || 'Personalized Search',
          description: t('searchSettings.personalization.description') || 'Use your activity to improve search results',
          value: searchPersonalization,
          onValueChange: handlePersonalizationToggle,
          disabled: isSaving,
          textColor: themeStyles.textColor,
          mutedTextColor: themeStyles.mutedTextColor,
          borderColor: themeStyles.borderColor
        })
      })]
    })]
  });
};
const styles = _reactNative.StyleSheet.create({
  container: {
    flex: 1
  },
  content: {
    flex: 1,
    padding: 16
  }
});
var _default = exports.default = /*#__PURE__*/_react.default.memo(SearchSettingsScreen);
//# sourceMappingURL=SearchSettingsScreen.js.map