"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

var _modularityFront = _interopRequireDefault(require("modularity-front"));

var _reactMaterialize = require("react-materialize");

var _API = _interopRequireDefault(require("./API"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }

function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var UserService =
/*#__PURE__*/
function (_Modularity$Service) {
  _inherits(UserService, _Modularity$Service);

  function UserService(props) {
    var _this;

    _classCallCheck(this, UserService);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(UserService).call(this, props));
    _this.state = {
      username: '',
      password: ''
    };
    _this.API = new _API.default('/users', UserService.Manager.getToken());

    _this.getUser(UserService.Manager.getToken());

    return _this;
  }

  _createClass(UserService, [{
    key: "getToken",
    value: function getToken() {
      return UserService.Manager.getToken();
    }
  }, {
    key: "isLogged",
    value: function isLogged() {
      var token = UserService.Manager.getToken();
      return !!token && token !== "undefined" && token !== 'missing';
    }
  }, {
    key: "logout",
    value: function logout() {
      var _this2 = this;

      return this.API.get('/logout').then(function () {
        _this2.User.set({});

        _this2.Token.set('missing');

        UserService.Manager.removeToken();
      });
    }
  }, {
    key: "login",
    value: function login() {
      var _this3 = this;

      var inputdata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state;
      var username = inputdata.username,
          password = inputdata.password;
      return this.API.post('/login', {
        username: username,
        password: password
      }).then(function (data) {
        return _this3.getUser(data.token);
      });
    }
  }, {
    key: "register",
    value: function register() {
      var _this4 = this;

      var inputdata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state;
      var email = inputdata.email,
          username = inputdata.username,
          password = inputdata.password;
      return this.API.post('/register', {
        email: email,
        username: username,
        password: password
      }).then(function (data) {
        return _this4.getUser(data.token);
      });
    }
  }, {
    key: "getUser",
    value: function getUser(token) {
      var _this5 = this;

      this.API.setToken(token);
      return this.API.get('/me').then(function (data) {
        _this5.User.set(data);

        _this5.Token.set(data.token || 'missing');

        UserService.Manager.setToken(data.token || 'missing');
        return data;
      }).catch(function () {
        _this5.User.set({});

        _this5.Token.set('missing');

        UserService.Manager.removeToken();
      });
    }
  }, {
    key: "confirmUser",
    value: function confirmUser(token) {
      return this.API.get("/verifyemail?id=" + token);
    }
  }, {
    key: "changePass",
    value: function changePass(token, cpass, npass) {
      this.API.setToken(token);
      return this.API.post('/changepass', {
        cpass: cpass,
        npass: npass
      }).then(function (data) {
        return data;
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this6 = this;

      return _react.default.createElement("div", {
        className: "UserServiceInfo"
      }, "ID : ", this.__renderID(), _react.default.createElement("div", null, _react.default.createElement(_reactMaterialize.Input, {
        type: "email",
        label: 'Email',
        value: this.state.email,
        onChange: function onChange(e) {
          return _this6.setState({
            email: e.target.value
          });
        }
      }), _react.default.createElement(_reactMaterialize.Input, {
        type: "text",
        label: 'Username',
        value: this.state.username,
        onChange: function onChange(e) {
          return _this6.setState({
            username: e.target.value
          });
        }
      }), _react.default.createElement(_reactMaterialize.Input, {
        type: "password",
        label: 'Password',
        value: this.state.password,
        onChange: function onChange(e) {
          return _this6.setState({
            password: e.target.value
          });
        }
      }), _react.default.createElement(_reactMaterialize.Button, {
        onClick: function onClick() {
          return _this6.login();
        }
      }, "Login"), _react.default.createElement(_reactMaterialize.Button, {
        onClick: function onClick() {
          return _this6.register();
        }
      }, "Register")), _react.default.createElement("div", null, "Token : ", this.Token.value), _react.default.createElement("div", null, this.__factoryOutput(this.User), this.__factoryOutput(this.Token)), _react.default.createElement("div", null, _get(_getPrototypeOf(UserService.prototype), "render", this).call(this)));
    }
  }]);

  return UserService;
}(_modularityFront.default.Service);

var Manager =
/*#__PURE__*/
function () {
  function Manager() {
    _classCallCheck(this, Manager);

    this.token = localStorage.getItem('Token');
  }

  _createClass(Manager, [{
    key: "setToken",
    value: function setToken(token) {
      this.token = token;
      localStorage.setItem('Token', token);
    }
  }, {
    key: "getToken",
    value: function getToken() {
      return this.token;
    }
  }, {
    key: "removeToken",
    value: function removeToken() {
      this.token = 'missing';
      localStorage.setItem('Token', 'missing');
    }
  }]);

  return Manager;
}();

UserService.Inputs = {};
UserService.Outputs = {
  User: {},
  Token: {}
};
UserService.Category = "Services";
UserService.Manager = new Manager();
var _default = UserService;
exports.default = _default;