import { extend } from '../../shared/utils';
export default function redirect(direction, route, options) {
  var router = this;
  var r = route.route.redirect;

  if (options.initial && router.params.browserHistory) {
    options.replaceState = true; // eslint-disable-line

    options.history = true; // eslint-disable-line
  }

  function redirectResolve(redirectUrl, redirectOptions) {
    if (redirectOptions === void 0) {
      redirectOptions = {};
    }

    router.allowPageChange = true;
    router[direction](redirectUrl, extend({}, options, redirectOptions));
  }

  function redirectReject() {
    router.allowPageChange = true;
  }

  if (typeof r === 'function') {
    router.allowPageChange = false;
    var redirectUrl = r.call(router, route, redirectResolve, redirectReject);

    if (redirectUrl && typeof redirectUrl === 'string') {
      router.allowPageChange = true;
      return router[direction](redirectUrl, options);
    }

    return router;
  }

  return router[direction](r, options);
}