'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.forceLogout = exports.logout = exports.resetMenu = exports.updateQuickMenu = exports.updateEnterMenuId = exports.updateQuery = exports.fetchEnd = exports.fetchStart = exports.initApp = exports.initDataFromServerNoMenu = exports.initDataFromServer = exports.initDataFromServerDispatch = undefined;

var _reduxActions = require('redux-actions');

var _reactRouterRedux = require('react-router-redux');

var _dictActions = require('../constant/dictActions');

var types = _interopRequireWildcard(_dictActions);

var _fetch = require('../system/fetch');

var _links = require('../constant/links');

var _links2 = _interopRequireDefault(_links);

var _LocalStorage = require('../service/LocalStorage');

var _MenuHelper = require('../utils/MenuHelper');

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

// import { LoginServer } from './login';
// import SessionStorage from '../utils/session-storage';
var STORAGE_KEY_PROFILE = '@AS:profile';

var initDataFromServerDispatch = exports.initDataFromServerDispatch = (0, _reduxActions.createAction)(types.INIT_DATA_FROM_SERVER);

var initDataFromServer = exports.initDataFromServer = function initDataFromServer() {
  return function (dispatch) {
    return (0, _fetch.PFetch)(_links2.default.getMenus, {}).then(function (response) {
      if ((!(0, _LocalStorage.getMenus)() || (0, _LocalStorage.getMenus)().length === 0) && response.menus && response.menus.length > 0) {
        // getMenus();
        var quickMenu = (0, _MenuHelper.getAllLeafs)(response.menus).slice(0, 8).map(function (i) {
          return i.menuCode;
        });
        (0, _LocalStorage.saveMenus)(quickMenu);
        response.quickMenu = quickMenu;
      }
      return response;
    }).then(function (response) {
      dispatch(initDataFromServerDispatch({ menu: response.menus, quickMenu: response.quickMenu }));
      return response.menus;
    }).then(function (menu) {
      (0, _LocalStorage.saveMenuGlobal)(menu);
    });
  };
};

var initDataFromServerNoMenu = exports.initDataFromServerNoMenu = function initDataFromServerNoMenu() {
  return function (dispatch) {
    return new Promise(function (resolve, reject) {
      return resolve();
    }).then(function (response) {
      dispatch(initDataFromServerDispatch({ menu: [] }));
    });
  };
};

var initApp = exports.initApp = function initApp() {
  // const payload = SessionStorage.get(STORAGE_KEY_PROFILE) || {};
  var quickMenu = (0, _LocalStorage.getMenus)();
  return (0, _reduxActions.createAction)(types.INIT_APP)({ quickMenu: quickMenu });
};

var fetchStart = exports.fetchStart = (0, _reduxActions.createAction)(types.FETCH_START);
var fetchEnd = exports.fetchEnd = (0, _reduxActions.createAction)(types.FETCH_END);

var updateQuery = exports.updateQuery = (0, _reduxActions.createAction)(types.UPDATE_QUERY);

var authLogin = (0, _reduxActions.createAction)(types.AUTH_LOGIN);
var authLogout = (0, _reduxActions.createAction)(types.AUTH_LOGOUT);
var forceLogoutAction = (0, _reduxActions.createAction)(types.AUTH_FORCE_LOGOUT);

var updateEnterMenuId = exports.updateEnterMenuId = function updateEnterMenuId(enterMenuId) {
  return (0, _reduxActions.createAction)(types.UPDATE_ENTER_MENU_ID)(enterMenuId);
};

var updateQuickMenu = exports.updateQuickMenu = function updateQuickMenu(quickMenu) {
  (0, _LocalStorage.saveMenus)(quickMenu);
  return (0, _reduxActions.createAction)(types.UPDATE_QUICK_MENU)(quickMenu);
};

var resetMenu = exports.resetMenu = (0, _reduxActions.createAction)(types.MENU_RESET);

// eslint-disable-next-line no-unused-vars
// export const loginServer = (isRemember, params) => dispatch =>
//  PFetch(links.login, params).then(d => {
//    // if (isRemember) {
//    //
//    // }
//    SessionStorage.set(STORAGE_KEY_PROFILE, { token: d.userToken });
//    dispatch(authLogin(d.userToken));
//  }).then(() => {
//    PFetch(links.getMenus, {}).then(menusData => {
//      dispatch(initDataFromServerDispatch({ menu: menusData.menus }));
//    });
//  });

var logout = exports.logout = function logout() {
  return function (dispatch) {
    return (0, _fetch.PFetch)(_links2.default.logout, {}).then(function () {
      // SessionStorage.set(STORAGE_KEY_PROFILE, { token: '' });
      dispatch((0, _reactRouterRedux.replace)('/login'));
      dispatch(authLogout());
    });
  };
};

var forceLogout = exports.forceLogout = function forceLogout() {
  return function (dispatch) {
    // SessionStorage.set(STORAGE_KEY_PROFILE, { token: '' });
    dispatch((0, _reactRouterRedux.replace)('/login'));
    dispatch(forceLogoutAction());
  };
};