"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Password = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _services = require("@mirai/services");
var _ui = require("@mirai/ui");
var _react = _interopRequireWildcard(require("react"));
var _Settings = require("./Settings.l10n");
var style = _interopRequireWildcard(require("./Settings.module.css"));
var _helpers = require("../../../helpers");
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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const Password = others => {
  var _form$password, _formError$password, _form$password2, _form$rePassword, _formError$rePassword, _form$rePassword2;
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      session: {
        club: {
          id: idClub
        } = {},
        email
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const [busy, setBusy] = (0, _react.useState)(false);
  const [form, setForm] = (0, _react.useState)({});
  const [formError, setFormError] = (0, _react.useState)({});
  const [responseError, setResponseError] = (0, _react.useState)();
  (0, _react.useEffect)(() => {
    setResponseError();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [form]);
  const handleSubmit = async form => {
    setBusy(true);
    setResponseError(undefined);
    const {
      password
    } = form;
    const response = await _services.ServiceUser.update({
      password
    }).catch(error => {
      setResponseError(error);
    });
    if (response) {
      _dataSources.Event.publish(_helpers.EVENT.NOTIFICATION, _objectSpread({
        success: true
      }, _Settings.L10N.SUCCESS_PASSWORD));
      await _services.ServiceUser.login({
        email,
        idClub,
        password
      }).catch(error => {
        setResponseError(error);
      });
    }
    setBusy(false);
  };
  const has = {
    errors: Object.keys(formError).length !== 0 || form.password !== form.rePassword,
    responseError
  };
  const inputRequiredProps = {
    required: true,
    requiredText: translate(_Settings.L10N.LABEL_REQUIRED),
    showRequired: true
  };
  return /*#__PURE__*/_react.default.createElement(_ui.Form, _extends({}, others, {
    validateOnMount: true,
    onChange: setForm,
    onError: setFormError,
    onSubmit: handleSubmit,
    className: style.form
  }), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true,
    wide: true,
    className: style.title
  }, translate(_Settings.L10N.DESCRIPTION_PASSWORD)), /*#__PURE__*/_react.default.createElement(_ui.InputText, _extends({
    name: "password",
    error: ((_form$password = form.password) === null || _form$password === void 0 ? void 0 : _form$password.length) && ((_formError$password = formError.password) === null || _formError$password === void 0 ? void 0 : _formError$password.test),
    hint: translate(_Settings.L10N.PASSWORD_HINT),
    label: translate(_Settings.L10N.PASSWORD_NEW_LABEL)
  }, inputRequiredProps, {
    success: ((_form$password2 = form.password) === null || _form$password2 === void 0 ? void 0 : _form$password2.length) && !formError.password,
    test: _helpers.testPassword,
    type: "password",
    value: form.password
  })), /*#__PURE__*/_react.default.createElement(_ui.InputText, _extends({
    name: "rePassword",
    error: ((_form$rePassword = form.rePassword) === null || _form$rePassword === void 0 ? void 0 : _form$rePassword.length) && (((_formError$rePassword = formError.rePassword) === null || _formError$rePassword === void 0 ? void 0 : _formError$rePassword.test) || form.rePassword !== form.password),
    hint: translate(_Settings.L10N.PASSWORD_HINT),
    label: translate(_Settings.L10N.PASSWORD_NEW_CONFIRM_LABEL)
  }, inputRequiredProps, {
    success: ((_form$rePassword2 = form.rePassword) === null || _form$rePassword2 === void 0 ? void 0 : _form$rePassword2.length) && form.rePassword === form.password,
    test: _helpers.testPassword,
    type: "password",
    value: form.rePassword
  })), has.responseError && /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    error: true,
    wide: true,
    className: style.notification
  }, translate(_Settings.L10N.ERROR)), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    busy: busy,
    disabled: busy || has.errors,
    wide: true,
    type: "submit",
    testId: "button-password"
  }, translate(_Settings.L10N.CTA_PASSWORD)));
};
exports.Password = Password;
Password.propTypes = {};
//# sourceMappingURL=Settings.Password.js.map