'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.warning = exports.guide = exports.dashboard = exports.authorize = exports.historyReplaceMenu = exports.historyPushMenu = exports.randomString = exports.handleResponseError = exports.checkPassword = exports.prompt = exports.logout = exports.getMessage = exports.intl = exports.languageChange = exports.removeAccessToken = exports.getAccessToken = exports.setAccessToken = exports.removeCookie = exports.getCookie = exports.setCookie = exports.fileServer = exports.STRING_DEVIDER = exports.WEBSOCKET_SERVER = exports.FILE_SERVER = exports.API_HOST = exports.AUTH_URL = exports.ACCESS_TOKEN = undefined;

var _message2 = require('choerodon-hap-ui/lib/message');

var _message3 = _interopRequireDefault(_message2);

require('choerodon-hap-ui/lib/message/style');

var _url = require('url');

var _url2 = _interopRequireDefault(_url);

var _authorize = require('./authorize');

var _accessToken = require('./accessToken');

var _cookie = require('./cookie');

var _constants = require('./constants');

var _intl = require('./intl');

var _checkPassword = require('./checkPassword');

var _checkPassword2 = _interopRequireDefault(_checkPassword);

var _warning = require('../../common/warning');

var _warning2 = _interopRequireDefault(_warning);

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

// 提示错误信息
function prompt(content) {
  var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'info';
  var duration = arguments[2];
  var placement = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'leftBottom';
  var onClose = arguments[4];

  var messageType = ['success', 'error', 'info', 'warning', 'warn', 'loading'];
  if (messageType.indexOf(type) !== -1) {
    _message3['default'][type](content, duration, onClose, placement);
  }
}

// 处理错误相应
function handleResponseError(error) {
  var response = error.response;

  if (response) {
    var status = response.status;

    switch (status) {
      case 400:
        {
          var mess = response.data.message;
          _message3['default'].error(mess);
          break;
        }
      default:
        break;
    }
  }
}

// 生成指定长度的随机字符串
function randomString() {
  var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 32;

  var code = '';
  var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  var maxPos = chars.length;
  for (var i = 0; i < len; i += 1) {
    code += chars.charAt(Math.floor(Math.random() * (maxPos + 1)));
  }
  return code;
}

function historyPushMenu(history, path, domain) {
  var method = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'push';

  method = 'push';
  if (!domain || _constants.LOCAL) {
    history[method](path);
  } else if (!path) {
    window.location = '' + domain;
  } else {
    var reg = new RegExp(domain, 'g');
    if (reg.test(window.location.host)) {
      history[method](path);
    } else {
      window.location = domain + '/#' + path;
    }
  }
}

function historyReplaceMenu(history, path, uri) {
  historyPushMenu(history, path, uri, 'replace');
}

function fileServer(path) {
  return _url2['default'].resolve(_constants.FILE_SERVER, path || '');
}

exports.ACCESS_TOKEN = _constants.ACCESS_TOKEN;
exports.AUTH_URL = _constants.AUTH_URL;
exports.API_HOST = _constants.API_HOST;
exports.FILE_SERVER = _constants.FILE_SERVER;
exports.WEBSOCKET_SERVER = _constants.WEBSOCKET_SERVER;
exports.STRING_DEVIDER = _constants.STRING_DEVIDER;
exports.fileServer = fileServer;
exports.setCookie = _cookie.setCookie;
exports.getCookie = _cookie.getCookie;
exports.removeCookie = _cookie.removeCookie;
exports.setAccessToken = _accessToken.setAccessToken;
exports.getAccessToken = _accessToken.getAccessToken;
exports.removeAccessToken = _accessToken.removeAccessToken;
exports.languageChange = _intl.intl;
exports.intl = _intl.intl;
exports.getMessage = _intl.getMessage;
exports.logout = _authorize.logout;
exports.prompt = prompt;
exports.checkPassword = _checkPassword2['default'];
exports.handleResponseError = handleResponseError;
exports.randomString = randomString;
exports.historyPushMenu = historyPushMenu;
exports.historyReplaceMenu = historyReplaceMenu;
exports.authorize = _authorize.authorize;
exports.dashboard = _constants.USE_DASHBOARD;
exports.guide = _constants.USE_GUIDE;
exports.warning = _warning2['default'];