"use strict";

exports.__esModule = true;
exports.default = Settings;

var _react = _interopRequireWildcard(require("react"));

var _styles = require("@mui/material/styles");

var _material = require("@mui/material");

var _reactIntl = require("react-intl");

var _core = require("@selfcommunity/core");

var _UserProfile = require("../../../constants/UserProfile");

var _classnames = _interopRequireDefault(require("classnames"));

var _SettingsSkeleton = _interopRequireDefault(require("./SettingsSkeleton"));

var _notistack = require("notistack");

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

const _excluded = ["id", "className", "fields"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const PREFIX = 'SCUserProfileEditSectionSettings';
const classes = {
  root: `${PREFIX}-root`,
  control: `${PREFIX}-control`
};
const Root = (0, _styles.styled)(_material.Box, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .${classes.control}`]: {
    margin: theme.spacing(0, 0, 2, 0),
    ['& .MuiFormControl-root']: {
      display: 'block'
    }
  }
}));

function Settings(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    id = null,
    className = null,
    fields = [..._UserProfile.DEFAULT_FIELDS]
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


  const scUserContext = (0, _core.useSCUser)(); // STATE

  const [settings, setSetting] = (0, _react.useState)(null); // HOOKS

  const {
    enqueueSnackbar
  } = (0, _notistack.useSnackbar)(); // EFFECTS

  (0, _react.useEffect)(() => {
    if (scUserContext.user) {
      _core.http.request({
        url: _core.Endpoints.UserSettings.url({
          id: scUserContext.user.id
        }),
        method: _core.Endpoints.UserSettings.method
      }).then(res => {
        setSetting(res.data);
      }).catch(error => {
        console.log(error);
      });
    }
  }, [scUserContext.user]); // HANDLERS

  const handleChange = event => {
    setSetting(Object.assign({}, settings, {
      [event.target.name]: event.target.value
    }));

    _core.http.request({
      url: _core.Endpoints.UserSettingsPatch.url({
        id: scUserContext.user.id
      }),
      method: _core.Endpoints.UserSettingsPatch.method,
      data: {
        [event.target.name]: parseInt(event.target.value, 10)
      }
    }).then(res => {
      setSetting(res.data);
      enqueueSnackbar( /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
        id: "ui.userProfileEditSettings.saved",
        defaultMessage: "ui.userProfileEditSettings.saved"
      }), {
        variant: 'success',
        autoHideDuration: 3000
      });
    }).catch(error => {
      console.log(error);
    });
  };

  if (!scUserContext.user) {
    return null;
  }

  if (settings === null) {
    return /*#__PURE__*/_react.default.createElement(_SettingsSkeleton.default, null);
  }

  return /*#__PURE__*/_react.default.createElement(Root, _extends({
    id: id,
    className: (0, _classnames.default)(classes.root, className)
  }, rest), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    gutterBottom: true,
    variant: "body1"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.notification.title",
    defaultMessage: "ui.userProfileEditSettings.notification.title"
  })), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.control
  }, /*#__PURE__*/_react.default.createElement(_material.FormControl, null, /*#__PURE__*/_react.default.createElement(_material.FormLabel, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.notification.label",
    defaultMessage: "ui.userProfileEditSettings.notification.label"
  })), /*#__PURE__*/_react.default.createElement(_material.RadioGroup, {
    "aria-labelledby": "notification settings",
    value: settings.show_toast_notifications,
    name: "show_toast_notifications",
    onChange: handleChange
  }, /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 1,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.common.yes",
      defaultMessage: "ui.common.yes"
    })
  }), /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 0,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.common.no",
      defaultMessage: "ui.common.no"
    })
  }))), /*#__PURE__*/_react.default.createElement(_material.FormControl, null, /*#__PURE__*/_react.default.createElement(_material.FormLabel, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.notificationSound.label",
    defaultMessage: "ui.userProfileEditSettings.notificationSound.label"
  })), /*#__PURE__*/_react.default.createElement(_material.RadioGroup, {
    "aria-labelledby": "notification sound settings",
    value: settings.toast_notifications_emit_sound,
    name: "toast_notifications_emit_sound",
    onChange: handleChange
  }, /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 1,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.common.yes",
      defaultMessage: "ui.common.yes"
    })
  }), /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 0,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.common.no",
      defaultMessage: "ui.common.no"
    })
  })))), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    gutterBottom: true,
    variant: "body1"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.interaction.title",
    defaultMessage: "ui.userProfileEditSettings.interaction.title"
  })), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.control
  }, /*#__PURE__*/_react.default.createElement(_material.FormControl, null, /*#__PURE__*/_react.default.createElement(_material.FormLabel, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.interaction.label",
    defaultMessage: "ui.userProfileEditSettings.interaction.label"
  })), /*#__PURE__*/_react.default.createElement(_material.RadioGroup, {
    "aria-labelledby": "email notification settings",
    value: settings.qa_frequency,
    name: "qa_frequency",
    onChange: handleChange
  }, /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: -1,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.userProfileEditSettings.interaction.immediatly",
      defaultMessage: "ui.userProfileEditSettings.interaction.immediatly"
    })
  }), /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 1,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.userProfileEditSettings.interaction.daily",
      defaultMessage: "ui.userProfileEditSettings.interaction.daily"
    })
  }), /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 0,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.userProfileEditSettings.interaction.never",
      defaultMessage: "ui.userProfileEditSettings.interaction.never"
    })
  })))), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    gutterBottom: true,
    variant: "body1"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.privateMessage.title",
    defaultMessage: "ui.userProfileEditSettings.privateMessage.title"
  })), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.control
  }, /*#__PURE__*/_react.default.createElement(_material.FormControl, null, /*#__PURE__*/_react.default.createElement(_material.FormLabel, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.userProfileEditSettings.privateMessage.label",
    defaultMessage: "ui.userProfileEditSettings.privateMessage.label"
  })), /*#__PURE__*/_react.default.createElement(_material.RadioGroup, {
    "aria-labelledby": "email notification settings",
    value: settings.email_notification_not_qa,
    name: "email_notification_not_qa",
    onChange: handleChange
  }, /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 1,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.userProfileEditSettings.privateMessage.email",
      defaultMessage: "ui.userProfileEditSettings.privateMessage.email"
    })
  }), /*#__PURE__*/_react.default.createElement(_material.FormControlLabel, {
    value: 0,
    control: /*#__PURE__*/_react.default.createElement(_material.Radio, {
      size: "small"
    }),
    label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.userProfileEditSettings.privateMessage.frontend",
      defaultMessage: "ui.userProfileEditSettings.privateMessage.frontend"
    })
  })))));
}