/**
 *
 */
"use strict";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

if (window.DolphyApp) (function () {
  var _DolphyApp = DolphyApp,
      $dolphy = _DolphyApp.$dolphy;
  DolphyApp.component('dolphy-dialog-2fa', '/dialog/2fa', {
    inject: ['$dialogInstance', '$dialogData'],
    data: function data() {
      return {
        form: {}
      };
    },
    methods: {
      remember: function remember() {
        this.form.remember = !this.form.remember;
      },
      submit: function submit(form, e) {
        if (e) e.preventDefault();
        var $dialogInstance = this.$dialogInstance,
            $dialogData = this.$dialogData;
        var $api = $dolphy.$api,
            $preloader = $dolphy.$preloader,
            $dialog = $dolphy.$dialog,
            $message = $dolphy.$message;

        var formData = _objectSpread(_objectSpread({}, form), {}, {
          dplc: $dialogData.dplc
        });

        try {
          if (!form.code) throw 'Two-Factor Authentication code is required!';
          $preloader.start();
          var url = $api.url('2fa');
          $api.post(url, formData).then(function (resp) {
            $dialogInstance.close(true);
          })["catch"](function (error) {
            $message.handle(error);
            $preloader.stop();
          })["finally"](function () {
            $preloader.stop();
          });
        } catch (error) {
          $message.handle(error);
        }
      }
    }
  });
})();
if (window.DolphyApp) (function () {
  var $element = document.querySelector('#forgot-password-page');
  if (!$element) return;
  var app = new DolphyApp({
    inject: ['AccessService'],
    data: function data() {
      return {
        form: {}
      };
    },
    methods: {
      submit: function submit(e) {
        var _this = this;

        e.preventDefault();
        var form = this.form,
            $preloader = this.$dolphy.$preloader,
            AccessService = this.AccessService;
        $preloader.start();
        AccessService.forgotPassword(form).then(function (resp) {
          swal('', resp.data, 'success');
          _this.form = {};
        })["catch"](function (error) {
          swal('', error.response.data, 'error');
        })["finally"](function () {
          $preloader.stop();
        });
      }
    }
  });
  app.mount($element);
})();
if (window.DolphyApp) (function () {
  var _DolphyApp2 = DolphyApp,
      $dolphy = _DolphyApp2.$dolphy;
  var $element = document.querySelector('#login-page');
  if (!$element) return;
  var app = new DolphyApp({
    inject: ['AccessService'],
    data: function data() {
      return {
        form: {}
      };
    },
    setup: function setup(props) {
      var $hooks = $dolphy.$hooks,
          $dialog = $dolphy.$dialog;
      $hooks.addAction('access:login:success', function (resp) {
        var actions = resp.actions || [];
        if (actions.indexOf('2fa') < 0) return;

        if (!('dplc' in resp)) {
          throw 'Unable to proceed with Two-Factor Authentication. Please try again later.';
        }

        var $2fa = $dialog.create({
          backdropClose: false,
          component: 'dolphy-dialog-2fa',
          data: {
            dplc: resp.dplc
          }
        });
        return $2fa.open();
      }, 0);
    },
    methods: {
      submit: function submit(e) {
        e.preventDefault();
        var form = this.form,
            AccessService = this.AccessService;
        var $preloader = $dolphy.$preloader,
            $hooks = $dolphy.$hooks,
            $message = $dolphy.$message;
        $preloader.start();
        var data = null;
        AccessService.login(form).then(function (resp) {
          $preloader.stop();
          data = Object.assign({}, resp.data);
          return $hooks.doAction('access:login:success', resp.data);
        }).then(function () {
          if (!('dplc' in data)) {
            throw 'An error occurred. Please try again later.';
          }

          $preloader.start();
          return AccessService.postLogin(data.dplc);
        }).then(function () {
          var _data = data,
              redirect = _data.redirect;

          if (window.top === window) {
            window.location.href = redirect;
          } else {
            window.top.location.reload();
          }
        })["catch"](function (error) {
          $message.handle(error, 'error');
          $preloader.stop();
        });
      }
    },
    mounted: function mounted() {
      var queryString = window.location.search;
      var params = new URLSearchParams(queryString);

      if (params.has('redirect')) {
        this.form.redirect = params.get('redirect');
      }
    }
  });
  app.mount($element);
})();
if (window.DolphyApp) (function () {
  var $element = document.querySelector('#reset-password-page');
  if (!$element) return;
  var app = new DolphyApp({
    inject: ['AccessService'],
    data: function data() {
      return {
        form: {}
      };
    },
    methods: {
      submit: function submit(e) {
        e.preventDefault();
        var form = this.form,
            $preloader = this.$dolphy.$preloader,
            AccessService = this.AccessService;
        var query = window.location.search;
        var params = new URLSearchParams(query);
        var data = Object.assign({}, form);
        if (params.has('login')) data['login'] = params.get('login');
        if (params.has('key')) data['key'] = params.get('key');
        $preloader.start();
        AccessService.resetPassword(data).then(function (resp) {
          swal('', resp.data.message, 'success').then(function () {
            window.location.href = resp.data.redirect;
          });
        })["catch"](function (error) {
          swal('', error.response.data, 'error');
        })["finally"](function () {
          $preloader.stop();
        });
      }
    }
  });
  app.mount($element);
})();
if (window.DolphyApp) (function () {
  var $element = document.querySelector('#signup-page');
  if (!$element) return;
  var app = new DolphyApp({
    inject: ['AccessService'],
    data: function data() {
      return {
        form: {}
      };
    },
    methods: {
      submit: function submit(e) {
        e.preventDefault();
        var form = this.form,
            $preloader = this.$dolphy.$preloader,
            AccessService = this.AccessService;
        $preloader.start();
        AccessService.signup(form).then(function (resp) {
          swal('', resp.data.message, 'success').then(function () {
            window.location.href = resp.data.redirect;
          });
        })["catch"](function (error) {
          swal('', error.response.data, 'error');
        })["finally"](function () {
          $preloader.stop();
        });
      }
    },
    mounted: function mounted() {// console.log(this);
    }
  });
  app.mount($element);
})();